ALGORITHM
ALGORITHM: An algorithm is a step by step procedure to solve any particular
problem.
For example: ALGORITHM FOR ADDING TWO NUMBERS
Step 1: Start.
Step 2: Declare two variables num1 and num2
Step 3: Declare third variable ‘sum’ to store sum of numbers.
Step 4: Read values of num1 and num2 from the user.
Step 5: Add num1 and num2 and assign the result to the variable sum.
Step 6: Display sum.
Step 7: Exit.
PSEUDOCODE
PSEUDOCODE: Pseudocode is an informal high-level description of algorithm.
• It is written in natural language and with the help of mathematical
notations.
• There is no particular programming language or syntax to follow while
writing pseudocode.
For example: PSEUDOCODE FOR ADDING TWO NUMBERS
Begin:
Set sum = 0.
Read: num1 and num2.
Set sum = num1 + num2.
Print sum.
End.
FLOWCHART
FLOWCHART: Flowchart is a diagrammatical representation of an algorithm.
• Various symbols are used to draw a flowchart.
OVAL Oval shape is used to denote Start and End.
RHOMBUS Rhombus shape is used to denote Input and Output.
DIAMOND Diamond shape is used to denote decision statement.
RECTANGLE Rectangle shape is used to denote a process.
ARROW Arrow shape is used to denote flow of program.
EXAMPLE OF FLOWCHART:
Flowchart to find if the given number is odd or even.
(Also If number is even, increment it by 1 else increment it by 2 after display.)
START
INPUT NUMBER
IF NUMBER% 2 == 0
DISPLAY DISPLAY
“NUMBER IS EVEN” “NUMBER IS ODD”
NUMBER=NUMBER +1 NUMBER=NUMBER +2
STOP
identifier
An identifier is a string of alphanumeric characters that begins with an
alphabetic character or an underscore character.
• Identifiers are used to represent various programming elements such as
variables, functions, arrays, structures, unions and so on.
• Actually, an identifier is a user-defined word.
For Example,
char name[10];
double marks, total;
float average;
Here, name, marks, total and average are identifiers.
char, int, double are keywords.
typedef
typedef: “typedef” is a keyword used in C language to provide some
meaningful names to the already existing variable in the C program.
• It acts as an alias for the existing variable names.
SYNTAX: typedef <Existing_Name> <New_Name>
For Example, typedef int roll_no;
main() main()
{ {
typedef int roll_no; typedef long int lint;
lint a, b, c;
roll_no a, b, c; }
}
BITWISE OPERATORS
Bitwise operators works upon individual bits in C. a = 5 (00000101)
TYPES OF BITWISE OPERATORS: b = 9 (00001001)
& (Bitwise AND): It takes two numbers as operands and apply AND operation on
every bit of two numbers. The result of AND is 1 only if both bits are 1. c=a&b; = 00000001
| (Bitwise OR): It takes two numbers as operands and apply OR operation on
every bit of two numbers. The result of OR is 1 if any of the two bits is 1. c=a|b; = 00001101
^ (Bitwise XOR): It takes two numbers as operands and apply XOR operation on
every bit of two numbers. The result of XOR is 1 if the two bits are different. c=a^b; = 00001100
<< (Left Shift): It takes two operands, left shifts the bits of the first operand, the
second operand decides the number of places to shift. c=a<<1; = 00001010
>> (Right Shift): It takes two operands, right shifts the bits of the first operand,
the second operand decides the number of places to shift.c=a>>1; = 00000010
~ (Bitwise NOT): It takes one number and inverts all bits of it c=~b; = 11110110
CONDITIONAL OPERATOR
Conditional operator (?:) works upon three arguments. That’s why It is also
known as ternary operator.
• It returns one value if condition is true, returns another value if condition is
false.
SYNTAX: EXPRESSION 1 ? EXPRESSION 2 : EXPRESSION 3
Generally, Expression 1 is Condition, Expression 2 is returned if condition is true
and Expression 3 is returned if condition is false.
For Example,
void main() void main() void main()
{ { {
int a = 10, b; int a=10, b=30, int a = 10, b=20, c=30, large;
b = (a == 10 ? 20 : 30); large; large=(a>b)?(a>c?a:c):(b>c?b:c);
} large = (a>b ? a : b); }
OPERATOR PRECEDENCE AND ASSOCIATIVITY
Operator precedence: It defines the order in which various operators will be
evaluated in an expression.
For example, 5 * 4 + 8 = ? 28 or 60
To answer such questions, we need Operator Precedence Table of C.
(High precedence operators will be evaluated before the operators with low precedence.)
Associativity: It determines the direction in which an expression is evaluated.
• It also defines the order in which operators of the same precedence are evaluated
in an expression.
• Associativity can be either from left to right or right to left.
For example, a = b; Another example, a == b != c;
Here value of b is assigned to a, Not Here both “==” and “!=” has same
value of a is assigned to b. precedence and associativity is
This is because “=” has associativity from left to right. So a==b shall be
from right to left. evaluated first.
C OPERATOR PRECEDENCE TABLE & ASSOCIATIVITY OF OPERATORS
Operator Meaning of operator Associativity
() Functional call
[] Array element reference
Left to right
-> Indirect member selection
. Direct member selection
Higher Precedence
! Logical negation
~ Bitwise(1 's) complement
+ Unary plus
- Unary minus
++ Increment
Right to left
-- Decrement
& Dereference (Address)
* Pointer reference
sizeof Returns the size of an object
(type) Typecast (conversion)
* Multiply
/ Divide Left to right
% Remainder
C OPERATOR PRECEDENCE TABLE & ASSOCIATIVITY OF OPERATORS
+ Binary plus(Addition)
Left to right
- Binary minus(subtraction)
<< Left shift
Left to right
Higher Precedence
>> Right shift
< Less than
<= Less than or equal
Left to right
> Greater than
>= Greater than or equal
== Equal to
Left to right
!= Not equal to
& Bitwise AND Left to right
^ Bitwise exclusive OR Left to right
| Bitwise OR Left to right
&& Logical AND Left to right
C OPERATOR PRECEDENCE TABLE & ASSOCIATIVITY OF OPERATORS
|| Logical OR Left to right
?: Conditional Operator Right to left
Higher Precedence
= Simple assignment
*= Assign product
/= Assign quotient
%= Assign remainder
+= Assign sum
-= Assign difference Right to left
&= Assign bitwise AND
^= Assign bitwise XOR
|= Assign bitwise OR
<<= Assign left shift
>>= Assign right shift
, Separator of expressions Left to right
Section B: continue statement in C
“continue statement” is similar to break statement, but it will not jump out of
the loop instead it just skips the remaining statements in the body of the loop
for the current pass.
• Hence, continue is used to skip a particular cycle of loop.
#include <stdio.h>
void main() Output:
{ 1*1=1
int a[5] = {1, -4, 3, -7, 10}; 3*3=9
int i; 10 * 10 = 100
for (i = 0; i < 5; i++ )
{
if (a[i] < 0)
continue;
printf("%d * %d = %d\n", a[i], a[i], a[i] * a[i]);
}
return 0;
}
goto statement in C
“goto statement” is used to transfer the control of execution of the program to
another part of the program where a particular label is defined.
Syntax: goto label; void main()
{void main()
• The label is the identifier which defines where the control { int i; should go on the execution
of 'goto' statement. intfor(i
i = 0;= 0; i < 10; i++)
• label statement block must be written like - label: statementa:or statement Output
block:
Another Example {
For Example if(i < 5)
Use of goto as a loop if(i == 5)
Output: { {
0 printf("%d\n", i);
1 goto out;
i++; }
2
gotoprintf("%d\n",
a; i);
3
4
} }
I will not count now out:
printf("I will not count
now\n");
}