Pop M1
Pop M1
Unambiguous − Algorithm should be clear and unambiguous. Each of its steps (or phases),
and their inputs/outputs should be clear and must lead to only one meaning.
Input − an algorithm should have 0 or more well-defined inputs.
Output − an algorithm should have 1 or more well-defined outputs, and should match the
desired output.
Finiteness − Algorithms must terminate after a finite number of steps.
Feasibility − should be feasible with the available resources.
Independent − an algorithm should have step-by-step directions, which should be
independent of any programming code.
Control Structures Used in Algorithms
Sequence – each step of the algorithm is executed in the specified order.
Decision – used when the outcome of the process depends on the condition. A condition
in this context is any statement that may evaluate either to a true or false value.
Repetition – involves executing one or more steps for a number of times, can be
implemented using constructs such as the while, do-while, and for loops. These loops
execute one or more steps until some condition is true.
Advantages :
Good communication tools to explain the logic of a system.
Help to analyse the problem in more effective manner.
They are used for program documentation.
They act as a guide or blueprint for the programmers to code the solution in any programming
language.
They can be used to debug programs that have errors.
Disadvantages :
Drawing flowchart is a time-consuming activity.
Flowchart of a complex program becomes complex and clumsy.
If changes are needed, the flowchart may need to fully redrawn.
As the flowchart symbols cannot be typed, reproduction of flowchart becomes a problem.
Factors that affect the sequence are not included.
PSEUDO-CODE: The pseudo code in C consists of words and phrases that make pseudo
code looks similar to the program but not a program. Pseudo codes are written with respect to a
programming language, but programming language syntax or grammar is not strictly followed.
Pseudocode is made up of two words, ‘pseudo’ and ‘code’. Pseudo means imitation and code
refer to instructions, written in a programming language.
Link section - The link section provides instructions to the compiler to link functions
from the system library.
Global declaration section - There are some variables that are used in more than one
function. Such variables are called global variables and are declared in the global
declaration section that is outside of all the functions. This section also declares all the
user-defined functions.
main () function section - Every C program must have one main function section. This
section contains two parts; declaration part and executable part. The declaration part
declares all the variables used in the executable part. There must be at least one
statement in the executable part. These two parts must appear between the opening and
closing braces. The program execution begins at the opening brace and ends at the
closing brace. The closing brace of the main function is the logical end of the program.
All statements in the declaration and executable part end with a semicolon.
}
Keywords - Every word used in a C program is classified as either a keyword or as an
identifier. A keyword in C is a reserved word which has a specific meaning. Keywords
in C cannot be used as identifiers. Keywords serve as the basic building blocks for
program statements
Keywords in C are always in lowercase. ANSI C supports 32 keywords which are
listed below:
Identifiers - refer to the names of variables, functions and arrays. These are user-
defined names and consist of sequence of letters and digits, with a letter as a first
character. Both uppercase and lowercase letters can be used, although lowercase letters
are generally used. The underscore character is also permitted in identifiers.
There are certain rules while writing identifiers. They are as follows:
1) First character must be an alphabet or underscore.
2) Must consist of only letters, digits or underscore.
3) Only first 31 characters are significant.
4) Cannot use a keyword.
5) Must not contain white space.
Numeric Variables: it can be used to store either integer values or floating point
values. It can also be associated with modifiers, such as short, long, signed and
unsigned. Unsigned variable indicates a variable that can hold only positive numbers.
Signed variable indicates that a variable can hold negative and positive values.
Character Variables: Single character enclosed within single quotes. These characters
could be any character from the ASCII character set - letters ( ‘a’ , ‘A’ ), numerals
(‘1’) or special characters (‘&’).
Declaring Variables:
Before using a variable in the program, we have to declare the variable. The syntax for
declaring a variable in a program is as shown below:
datatype variable-name;
The “type” in the above syntax represents the data type. The “variable-name” is the
identifier. There are certain rules that must be followed while writing the variable name.
Rules for Variables Name –
A variable name must always start with an alphabet (letter) or an underscore ( _ ).
The variable name must not be more than 31 characters. The suggested length of a
variable name is 8 characters.
C is case sensitive. So, the variable name “average” is different from
“AVERAGE”.
Keywords must not be used for declaring variables.
White spaces are not allowed within the variable name.
In c variables are declared at three basic places as follows:
When a variable is declared inside a function it is known as a “local variable”.
When a variable is declared in the definition of function parameters it is known as a “formal
parameter”.
When a variable is declared outside all functions it is known as a “global variable”.
In ANSI C, the data types are divided into three categories. They are:
User-Defined data types - ANSI C allows the users to define identifiers as their own data
types, based on the already existing primitive or fundamental data types. This concept is
known as “type definition” and the data types thus created are known as user-defined data
types.
We can create user-defined data types in two ways:
enum days{sun,mon,….sat}
Derived data types - The data types which are created using the already existing primitive or
fundamental types are known as derived data types. Examples of derived data types in C are:
Arrays, Functions, Structures, Unions & Pointers.
Basic Datatypes in C:
Detailed Datatypes in C:
Operand1 Op Operand2
Operand1 and operand2 can be either data or variables or expressions. Op is the operator. In C,
based on the number of operands on which an operator can operate, the operators are divided
into three types namely:
1) Unary, 2) Binary, 3) Ternary
Different Types of Operators: In C, based on the functionality, operators are classified into
8 categories. They are:
Relational Operators(<,>,==,!=,>=,<=)
Logical Operators(&&,||,!)
Sizeof Operator
Arithmetic Operators - C provides all the basic arithmetic operators as shown below. The
arithmetic operators can operate on any built-in data type in C.
Relational Operators - In C, whenever there is a need to compare two values and make a
decision based on the outcome of the comparison, we use relational operators. The relational
operators are generally used in decision making statements like if, else if and in looping
statements like for, while, do while etc. Relational operators always evaluates to 0 (false) or 1
(true).
Logical Operators - The relational operators are used to compare at most two values i.e.
testing one condition. To test more than one condition, we use logical operators along with
relational operators. The logical operators always evaluates to either 0 or 1 like relational
operators.
The logical AND operator is represented as the '&&' double ampersand symbol. It
checks the condition of two or more operands by combining in an expression, and if
all the conditions are true, the logical AND operator returns the Boolean value true or
1. Else it returns false or 0.
A B A&&B
0 0 0
0 1 0
1 0 0
1 1 1
The Logical OR returns a false value if both the operands are false. Otherwise it
returns a true value.
A B A||B
0 0 0
0 1 1
1 0 1
1 1 1
The Logical NOT takes a single expression and negates the value of the expression.
A !A
0 1
1 0
Assignment Operators - The assignment operators are used to assign value of an expression
to a variable. The general assignment operator is = (equal). In C, there are some special
assignment operators known as shorthand operators. The syntax of shorthand operators is as
shown below:
var op=exp;
Increment/Decrement Operators - The increment and decrement operators provided by C
are used to increment or decrement the operand by a value of one. Both the increment and
decrement operators are unary operators. There are two variations in increment/decrement
operators
1) pre-increment/decrement(++var/--var)
2) post-increment/decrement(var++/var--)
When pre increment is applied, the value of the variable is incremented by one first
and then that value is used for evaluation of the expression.
Example: int x =10,y;
y = ++x;
can be written as
x = x+1;
y = x;
When post increment is applied, the value of the variable is used in the evaluation of
the expression and after the expression is evaluated, the value of the variable is
incremented by a value of one.
Example: int x=10,y;
y = x++;
can be written as
y = x;
y = x+1;
Unary minus: The Unary Minus operator is represented using the symbol (-). The
unary operator is used to change the sign of any positive value to a negative value. It
means it changes the positive number to the negative, and a negative number becomes
the positive number using the unary minus operator.
Example: int a,b=10;
a= -(b);
exp1?exp2:exp3;
In the above syntax, exp1, exp2 and exp3 refer to expressions.It evaluates the exp1 first and then
based on the result of the exp1 it evaluates either exp2 or exp3. If the result of exp1 is true or
non-zero, then exp2 is executed or if the result of exp1 is false or zero, then exp3 is executed.
Example: large = (a>b) ? a:b
The conditional operator is used to find the larger of two given numbers. First expression1, i.e,
(a>) is evaluated. If a is greater than b, then large = a, else large = b. hence large is equal to either
a or b but not both.
Bitwise Operator’s - C supports a set of operators which operate at bit-level. These operators
are known as bitwise operators. The bitwise operators are used for testing a bit, setting a bit,
complementing a bit or for shifting the bits to left or right.
Bitwise AND operator is denoted by the single ampersand sign (&). Two integer
operands are written on both sides of the (&) operator. If the corresponding bits of both
the operands are 1, then the output of the bitwise AND operation is 1; otherwise, the
output would be 0
The bitwise OR operator is represented by a single vertical sign (|). Two integer operands
are written on both sides of the (|) symbol. If the bit value of any of the operand is 1, then
the output would be 1, otherwise 0.
Bitwise exclusive OR operator is denoted by (^) symbol. Two operands are written on
both sides of the exclusive OR operator. If the corresponding bit of any of the operand is
1 then the output would be 1, otherwise 0.
The bitwise complement operator is also known as one's complement operator. It is represented
by the symbol tilde (~). It takes only one operand or variable and performs complement operation
on an operand. When we apply the complement operation on any bits, then 0 becomes 1 and 1
becomes 0.
Two types of bitwise shift operators exist in C programming. The bitwise shift operators will shift
the bits either on the left-side or right-side. Therefore, we can say that the bitwise shift operator is
divided into two categories:
Left-shift operator: It is an operator that shifts the number of bits to the left-side.
Right-shift operator: It is an operator that shifts the number of bits to the right side.
Example: x is an integer expression with data 1111.
After performing shift operation the result will be:
x << 2 (left shift) = 1111<<2 = 1100
x>>2 (right shift) = 1111>>2 = 0011
Equality operator – c language supports two kinds of equality operators to compare their
operands for strict equality or inequality. They are equal to (==) and not equal to (!=)
operators.
returns 1 if both operands are equal, others it returns 0.
(!=) – returns 1 if operands do not have the same value, otherwise it returns 0.
Sizeof operator - The sizeof operator computes the size of an expression or variable or
constant or a data type. It is a unary operator. It is used to determine the amount of memory
space that the variable/expression/data type will take.
The general syntax of sizeof operator is as shown below:
sizeof(operand);
Operator Precedence Chart: Expressions are evaluated based on operator precedence and
associativity rules when an expression contains more than one operator. Every C operator has a
precedence (priority) associated with it. This precedence is used to determine how an expression
involving more than one operator is evaluated.
The operators at the higher level of precedence are evaluated first. The operators in the same
level of precedence are evaluated from left to right or from right to left, based on
the associativity property of an operator.
In the below table we can look at the precedence levels of operators and also the associativity of
the operators within the same level. Rank 0 indicates the lowest precedence and Rank 14
indicates highest precedence.