2nd Unit
2nd Unit
Types of Operator
A relational operator is used to compare two values and the result of such an operation is either 1 or
0. It is 1 if the relation is true and 0 if the relation is false. Left hand side value is compared with the
right hand side.
If expression: x>y
So, x and y are operands and > is a relational operator.
10 < 20 true or 1
4.5 <= 10 true or 1
-10 >= 4.5 false or 0
10 !=15 true or1
X y x&&y x||y !x !y
0 0 0 0 1 1
0 1 0 1 1 0
1 0 0 1 0 1
1 1 1 1 0 0
Postfix <variable name ++> e.g. m++ (Postfix) Use then Change
e.g. int m=1,t; t = m++; t = 1 and m = 2
Prefix <++ variable name> e.g. ++m (Prefix) Change then Use
e.g. int m=1,t; t = ++m; t = 2 and m = 2
Postfix <variable name -- > e.g. m-- (Postfix) Use then Change
e.g. int m=1,t; t = m--; t = 1 and m = 0
Prefix < -- variable name> e.g. --m (Prefix) Change then Use
e.g. int m=1,t; t = --m; t = 0 and m = 0
It consists of 2 symbols: the question mark ? and colon : . This is the only operator in C that takes
three operands. It is known as ternary operator.
e.g. larger = i>j ? i:j
here ? and : are conditional operator.
If the condition(i>j) is true then i will be the answer otherwise answer is j.
The bitwise operator operates on each bit of data. These operators are used for testing,
complementing or shifting bits to the right or left. These operators are not use in case of float and
double.
Shift left(<<):
e.g. m = j << 1
value of j shifted to left by 1 bit and adding bit 0 to the right of it where i shifted to right by 1 bit and
adding 0 to the right.
e.g.1 j = 00010000
m= j <<1 (shift left by 1 bit)
0 0 0 1 0 0 0 0
0 0 1 0 0 0 0 0
Blank space left. So, add 0
J=00010000 is equal to 16 but after shifting left by 1 bit, it becomes 32. i.e. shifting a variable
to the left by 1 position is equal to multiply it by 2. So shifting 2 positions (2x2) i.e. multiplying
the number by 4. Similarly 3 positions (2x2x2) i.e. multiplying the number by 8 and so on…
e.g.2 a = 00010101
m= a >>2 (shift right by 2 bit)
0 0 0 1 0 1 0 1
0 0 0 0 0 1 0 1
Two blank space left. So, add 0
a is 00010101 is equal to 20 but after shifting right by 2 bit, it becomes 5. i.e. shifting a
variable to the right by 2 position is equal to divide it by 4. So shifting 1 positions (2) i.e.
dividing the number by 2. Similarly 3 positions (2x2x2) i.e. dividing the number by 8 and so
on…
e.g.3
a = 123 & 21 BITWISE AND
0 1 1 1 1 0 1 1 (123)
0 0 0 1 0 1 0 1 (21)
_________________
0 0 0 1 0 0 0 1 (17) which is assign to a
e.g.4
a = 123 | 21 BITWISE OR
0 1 1 1 1 0 1 1 (123)
0 0 0 1 0 1 0 1 (21)
_________________
0 1 1 1 1 1 1 1 (127) which is assign to a
Sizeof operator:
The sizeof operator is a compile time operator and when used with an operand, it returns the no. of
bytes the operand occupies. The operand may be a variable, a constant or a data type.
e.g. int sum;
m = sizeof(sum);
output 2
e.g. float marks;
n = sizeof(marks);
output 4
(a + b) / 4; and array[1] =
() and [] Parentheses, Braces
2;
1 ++ Post-Increment left to right
for( i = 0; i < 10; ++i )
-- Post-Decrement
for( i = 10; i > 0; --i )
Comparison less-than
< Comparison less-than-or- if( i < 42 ) ...
<= equal-to if( i <= 42 ) ...
6 left to right
> Comparison greater-than if( i > 42 ) ...
>= Comparison greater-than-or- if( i >= 42 ) ...
equal-to
e.g. - - 3 * (3+4) / 2 – 5 ++ * 4
= - - 3 * 7 / 2 - (5++) * 4
= (- - 3)* 7 / 2 - 5 * 4
= (2 * 7) / 2 - 5 * 4
= 14 / 2 - 5 * 4
= 7 - (5 * 4)
= 7 - 20
= -13
7 double NOTE:
6 float
5 long SOME ASCII VALUES
4 Int
3 Short int value char value
2 Char
48 to 57 0 to 9
1 Bool
65 to 90 A to Z
1. Example of Promotion in Implicit type conversion:
bool b=true; 97 to 122 a to z
char c=’A’;
int i=1234;
long double d=3458.0004
c=b; // value of c is ASCII OF 1
i=c; // value of i is 65
d=b; // value of d is 1.0
d=i; // value of d is 1234.0
2. Example of Demotion in Implicit type conversion:
bool b=false;
char c=’A’;
short s=78;
int j=32767;
int k=65;
b=c; // value of b is 1(true)
s=j; // value of s is unpredictable or 32767
c=k+1; // value of c is ‘B’
3.2 Explicit type conversion:
It uses unary type operator, which convert data from one type to another. We specify new type in
parenthesis before the value we want to convert.
e.g. (float) a;
Here a is integer but the value of expression is converted to float.
e.g.1 int a;
a = (int)21.3 / (int)4.5
= 21 / 4
= 5
e.g.2 float a;
a = (int) 21.3 / (int) 4.5
= 21 / 4
= 5.000000
4.1 if statements
if (condition)
{
//Block of C statements here
//These statements will only execute if the condition is true
}
if(condition)
{
// Statements inside body of if
}
else
{
//Statements inside body of else
}
if (condition1)
{
//These statements would execute if the condition1 is true
}
else if(condition2)
{
//These statements would execute if the condition2 is true
}
else if (condition3)
{
//These statements would execute if the condition3 is true
}
.
.
else
{
//These statements would execute if all the conditions return false.
}
if (condition1)
{
//These statements would execute if the condition1 is true
if (condition2)
{
//These statements would execute if the condition2 is true
}
}
#include <stdio.h>
void main()
{
int m=40,n=20;
if (m>n)
{
printf(" m is greater than n ");
} Output: m is greater than n
else if(m<n)
{
printf(" m is less than n ");
}
else
{
printf(" m is equal to n ");
}
}
Output:
Enter a Year
1600
1600 is Leap Year
WAP in C to check that the entered character is vowel or not using switch case.
WAP on C to create a calculator to perform + , - , * and / using case control statement.
6 Comments
Make the program more readable.
Compiler ignores the comments when it translates the programs into executable code.
C uses two different formats:-
6.1 Block comments: When we cover more than one lines such as /*…………………*/
6.2 Line comments: When we cover only one line such as //
e.g. int a,b; where, a and b are identifiers which is defined by user.
8 Keyword(Pre-defined Name)
Keywords are those words whose meaning has been fixed. All keywords must be written in lower
case. There are only 32 keywords in C.
e.g. int, if, else, float, char, do, while,for etc.
#include<stdio.h> #include<stdio.h>
int main() int main()
{ {
int a = 5; int a = 5;
int b = ++a * a++; ++ has highest precedence then * switch(a)
printf("%d ",b); 6*6++ {
} default:
OUTPUT: 36 a = 4;
case 6:
a--;
case 5:
a = a+1; // a=5+1=6
case 1:
a = a-1; // a=6-1=5
}
printf("%d \n",a);
}
OUTPUT: 5