Module-2
Module-2
Module-2
Faculty:
Dr. Vikhyath K B
9.15 Operators
• An operator is a symbol that specifies the mathematical, logical or relationship
operation to be performed.
• C supports a lot of operators to be used in expressions.
• An expression is a combination of operators, constants and variables.
• An expression may consist of one or more operands, and zero or more operators to
produce a value.
#include <stdio.h>
int main()
{
int x=10, y=20;
printf(" \n %d < %d = %d", x, y, x<y);
printf(" \n %d == %d = %d", x, y, x==y);
printf(" \n %d != %d = %d", x, y, x!=y);
printf(" \n %d > %d = %d", x, y, x>y);
printf(" \n %d >= %d = %d", x, y, x>=y);
printf(" \n %d <= %d = %d", x, y, x<=y);
return 0;
}
10 == 20 = 0
10 != 20 = 1
10 > 20 = 0
10 >= 20 = 0
10 <= 20 = 1
They are equal to (==) and not equal to (! =) operators. The equality operators have
lower precedence than the relational operators.
The equal to operator returns true (1) if both the sides of the operator have the same
value otherwise, it returns false (0). On the contrary side, the not equal to operator
returns true if the operands do not have the same value else it returns false.
If the expressions on both the sides (left and right side) of the logical
operator is true, then the whole expression is true.
If the one or both the expressions on both the sides (left and right side) of the logical
operator is true, then the whole expression is true.
The truth table of logical OR is given in the following table example: a= 5, b =10
(a>b) || (b<30) will be TRUE(1)
int x=10, y;
y=x++; is equivalent to writing as
y=x;
x=x+1;
x=x+1;
y=x;
exp1 is evaluated first. If it is true, then exp2 is evaluated and becomes the result of the
expression, otherwise exp3 is evaluated and becomes the result of the expression.
Example:
large= (a>b)? a: b
They include bitwise AND, bitwise OR, bitwise XOR and shift operators.
Bitwise operators expect their operands to be integers and they treat them as sequence of bits.
¨ It is the smaller version of Boolean AND (&&) as it performs on the bits instead of bytes,
characters, integers etc.,
¨ When we use bitwise AND, the bit in the first operand is ANDed with the corresponding bit in
the second operand.
¨ The truth table is same as the logical AND operation. If both the bits are 1, the corresponding bit
in the result is 1 or 0 otherwise.
Here, c = a & b
= 00001010 & 00001100
c = 00001000
Therefore c = 8
¨ It is the smaller version of Boolean OR (||) as it performs on the bits instead of bytes,
characters, integers etc.,
¨ When we use bitwise OR, the bit in the first operand is ORed with the corresponding bit in the
second operand.
¨ The truth table is same as the logical OR operation. If one or both bits are 1, the corresponding
bit in the result is 1 or 0 otherwise.
The bitwise XOR operator compares each bit of its first operand with
the corresponding bit its second operand. If one of the bits is 1, the
corresponding bit in the result is 1 or 0 otherwise.
c = a ^ b;
Here, c = a ^ b
00001010 ^ 00001100
c = 00000110
Therefore c = 6
Bitwise NOT operator sets the bit to 1 if it was initially 0 and sets to 0 if
1
Example:
~10101010 = 01010101
¨ C supports two bitwise shift operators. They are shift-left (<<) and shift-right (>>). These
operations are simple and are responsible for shifting bits either to left or to right.
operand op num
¨ where the bits in operand are shifted left or shifted right depending upon the operator by the
number of places denoted by num.
x = 00011101 then
x << 1
Produces: 00111010
x<<4
Produces: 11010000
¨ While the equal sign (=) is the fundamental assignment operator, C also supports other
assignment operators that provides simpler ways to represent the common variable
assignments.
¨ When an equal sign is encountered in an expression, the compiler processes the statement on
the right side of the sign and assigns the result to the variable on the left side.
¨ (a=(b=(c=10)));
¨ First 10 is assigned to c, then value of c is assigned to b and finally the value of b is assigned to a.
¨ The operand to the left of assignment operator must always be a variable name. C does not
allow any expression, constant or function to be placed to the left of the assignment operator.
¨ It is evaluated in left-to-right sequence with the right most value yielding the result of the
expression.
¨ Among the operators, the comma operator is having the lowest precedence.
Example:
Example:
¨ int a=10;
¨ int result;
¨ result = sizeof(a);
¨ Then result is 2 or 4, that is the space required to store the variable in memory. Since “a” is an
integer, it requires 2 or 4 bytes of storage space.
¨ When an expression has more than one operator, then it is the relative priorities of
the operator with respect to each other which determines the order in which
expression is evaluated.
¨ Associativity refers to the direction in which operator having the same precedence
acts as operands. it can be either left-to right associativity or from right-to-left
associativity.
¨ If parentheses are nested, the evaluation begins with the innermost sub expression.
¨ Arithmetic expressions are evaluated from left to right using the rules of
precedence.
1. x=3*4+5*6
= 12 + 5 * 6
= 12 + 30
42
¨ 2. x =3 * ( 4 + 5 ) * 6
=3*9*6
= 27 * 6
162
3. x = 3 * 4 % 5 / 2
= 12 % 5 /2
=2/2
1
¨ 10. a && b
=0
¨ 12. b + c || ! A
= ( b + c ) || ( !a)
0 || 1
1
int main() {
char ch;
printf(" \n Enter any character \n ");
scanf("%c", &ch);
printf( " \n The ASCII value of %c is: %d", ch, ch);
return 0;
}
Output:
Enter any character
a
The ASCII value of a is: 97
int main() {
char ch;
printf(" \n Enter the character in upper case: \n ");
scanf("%c", &ch);
printf( " \n The character in lower case is %c",ch+32);
return 0;
}
Output: Enter the character in upper case:
A
The character in lower case is a
Type conversion is done when the expression has variables of different data types.
To evaluate the expression, the data type is promoted from lower to higher level.
The hierarchy of data types (from higher to lower) are given as: Double, float, int,
short, and, char.
Rules:
When a float value is converted to an int value, the fractional part is truncated.
When a double value is converted to a float value, rounding of digit is done.
When a long int is converted into int, the excess higher order bits are dropped.
It is done when the value of the higher data type has to be converted into the value of the
lower data type. This is completely under the programmer control not under the compiler
control.
When floated point number are converted to integer, the digits after the decimal are
truncated.
Although this is true, but in some cases we want only selected statements to be
executed. Such type of conditional processing extends the usefulness of
programs. It allows the programmers to build programs that determine which
statement of the code should be executed and which should be ignored.
if-statement
if-else statement
is-else-if statement
switch statement
10.2.1 if-statement
The if statement is the simplest form of decision control statement that is
frequently used in decision-making.
if (test expression)
{
statement 1;
…
statement n;
}
statement x;
}
10.2.4 switch case
A switch case statement is a multi-way decision statement that is a simplified version of an if-
else block that evaluates only one variable.
Syntax:
switch (variable)
{
case value 1: statement block 1;
break;
case value 2: statement block 2;
break;
…………….
case value n: statement block n;
break;
default: statement block D;
break;
}
statement x;
When there are many conditions to test, using the if and else-if
constructs becomes a bit complicated and confusing.
Therefore, switch case statements are often used as an alternative to long
if statement that compare a variable to several integral values.
switch(day)
{
case 1: printf(" \n Sunday");
break;
case 2: printf(" \n Monday");
break;
case 3: printf(" \n Tuesday");
break;
while loop
do-while loop
for loop
Syntax:
statement x;
while (condition)
{
statement block;
}
statement y;
¨ In while loop, the condition is tested before any of the statements in the statement block is
executed.
¨ If the condition is true, only then the statement will be executed otherwise if the condition is
false, the control will jump to statement y.
Output:
1 2 3 4 5 6 7 8 9 10
while (i<=10)
{
sum = sum + i;
i = i +1;
}
SUM = 55
Output:
*********************
The test condition is enclosed in parenthesis and followed by a semicolon. The statement in the
statement blocks are enclosed within curly brackets. The curly brackets are optional if there is
only one statement in the body of the do-while loop.
Syntax: statement x;
do
{
statement block;
}
while(condition);
statement y;
10
Dr H N National College of Engineering 86
Write a program to calculate the average of first n
numbers.
#include<stdio.h>
int main()
{
int n, i=1, sum=0;
float avg = 0.0;
printf(" \n Enter the Value of n:");
scanf("%d", &n);
do
{
sum = sum + i;
i = i + 1;
}
while(i<=n);
Output:
◦ Enter the Value of n:18
◦ The sum of first 18 numbers = 171
◦ The average of first 18 numbers = 9.50
For loop is usually known as a determinate or definite loop because the programmer knows
exactly how many times the loop will repeat.
Syntax:
If the condition is true, then the statement block of the loop is executed,
else the statements comprising the statement block of the for loop are
skipped and the control jumps to the immediate statement following the
for loop body.
Output:
Enter the value of n: 4
1
2
3
4
Output:
10
Although this feature will work with any loop such as while, do-while, and for,
it is most commonly used with the for loop, because it is easiest to control.
#include <stdio.h>
int main()
{
int i,j,n;
scanf("%d",&n);
for(j=1; j<=n-i;j++)
printf(" ");
for(j=1;j<=i;j++)
printf(" %d",j);
/* Display number in reverse order after middle */
for(j=i-1;j>=1;j--)
printf(" %d ",j);
printf("\n");
}
}
1
121
1232 1
12343 2 1
The break statement is widely used with for loop, while loop and do-while
loop. When the compiler encounters a break statement, the control passes to
the statement that follows the loop in which the break statement appears.
Syntax: break;
In switch statement if the break statement is missing then every case from the
matched case label till the end of the switch, including the default, is executed.
Dr H N National College of Engineering 98
while(….)
{
if(condition)
break;
……..
}
…….
Transfers the control out of the loop while
do
{
….
if(condition)
break;
….
} while(…..);
……
Transfers the control out of the do-while loop
When the compiler encounters a continue statement then the rest of the
statement in the loop are skipped and the control is unconditionally transferred
to the loop-continuation portion of the nearest enclosing loop.
Syntax: continue;
Example:
#include<stdio.h>
int main()
{
int i;
for(i=1;i<=10;i++)
The code given here is meant to print numbers from 1 to 10. But as soon as
i becomes equal to 5, the continue statement is encountered, so rest of the
statements in the for loop are skipped and the control passes to the
expression that increments the value of i.
Output:
1 2 3 4 6 7 8 9 10
Syntax:
Forward jump
goto label
……….
……….
label:
statements
label:
statements
…………
…………
goto label