Operators in C
Operators in C
Lecture Number :- 3
“Operators in C "
Delivered by:
Mr. Neeraj Garg
Dept. of Computer Science & Engineering
Swami Keshvanand Institute of Technology,
Management & Gramothan, Jaipur
Learning Objectives
Types of operators
List of operators in C
Conditional
Arithmetic
Relational
Logical
Assignment
Increment/decrement
Bitwise
Special operators
2
TYPES OF OPERATORS
3
LIST OF OPERATORS IN C
FALSE
#include<stdio.h>
#include<conio.h>
void main()
{
int a=10;
a>0? printf(“a is positive”) : printf(“a is negative”);
getch();
}
Output : a is positive 5
ARITHMETIC OPERATOR
Arithmetic operators are used to perform numeric calculations.
• Example: We have two variables x = 5 and y = 2, Results are:
#include<stdio.h>
#include<conio.h> // header file declaration
void main( ) // starting main function
{
int x=5, y=2; Output:
printf(“%d\n”, x<y); 0
printf(“%d\n”, x>y); 1
printf(“%d\n”, x<=y); 0
printf(“%d\n”, x>=y); 1
printf(“%d\n”, x==y); 0
printf(“%d\n”, x!=y); 1
getch();
}
8
LOGICAL OPERATOR
Logical operators are used to combine two expressions. The
expression may be variables, constants and functions.
C language supports three logical operators AND (&&), OR
(| |) , NOT(!)
#include<stdio.h>
A B A && B A || B !A #include<conio.h>
void main( )
{
0 0 0 0 1 int a=10, b=20;
int res1, res2;
0 1 0 1 1 res1 = ( a>=10 && b==20 );
res2 = ( a>=10 || b==20 );
1 0 0 1 0 printf(“Res1:= %d”, res1);
printf(“\nRes2: =%d”, res2);
getch();
1 1 1 1 0
}
Output: Res1:=1
Truth Table Res2:= 1
9
ASSIGNMENT OPERATOR
Assignment operator is used to assign the result of an
expression (Right hand side) to a variable (Left hand side).
The equal sign ( = ) is the Assignment operator in C.
x = x%10 x%=10
Output: 10 20 2.75 M
10
INCREMENT/DECREMENT OPERATOR
Increment (++) operator increases the value of the variable by 1.
Decrement (--) operator decreases the value of the variable by1.
These operators are used either Post or Pre form.
Both are Unary operators.
Example:
Post Increment (X++) Pre Increment (++ X)
X = 10; X = 10;
Y = X; Y = 10 X=X+1; X=10+1=11
X = X + 1; X = 10 + 1 = 11 Y=X; Y=11
X = 10; X = 10;
Y = X; Y = 10 X = X-1; X = 10 -1 = 9
X = X - 1; X = 10 - 1 = 9 Y = X; Y = 9
12
PROGRAM USING ++ AND --
Write a C Program to demonstrate ++ and -- operator.
#include <stdio.h> #include<stdio.h>
void main ( )
{
int X, Y;
X = 10;
Y = ++ X;
-- X;
Y --;
X = Y ++;
Y = -- X;
X = Y ++;
printf(“X=%d Y=%d”, X, Y);
getch();
}
Output: X = ? Y=? 13
PROGRAM USING ++ AND --
Write a C Program to demonstrate ++ and -- operator.
#include <stdio.h> // header file
#include<conio.h>
void main ( )
{
int X, Y; // Variable declarations
X = 10; // Variable initialization
Y = ++ X; // X=X+1=>10+1=>11 and Y=X=>11
-- X; // X=X-1=>11-1=>10
Y --; // Y=Y-1=>11-1=>10
X = Y ++; // X=Y=>10 and Y=Y+1=>10+1=>11
Y = -- X; // X=X-1=>10-1=>9 and Y=X=>9
X = Y ++; // X=Y=>9 and Y=Y+1=>9+1=>10
printf(“X=%d Y=%d”, X, Y);
}
Output: X = 9 Y=10 14
BITWISE OPERATOR
Bitwise operators are used to perform operation at bit level.
These operators are applied only integer data. (Not for float
and double) .
Example: Variables X = 11 and Y = 5
Binary equivalent: X 1011 and Y 0101
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111
15
BITWISE OPERATOR(Cont.)
Left shift (<<) operator shifts the bit pattern to the left side.
It is written as x<<num; which means shifting the bits of x towards
left by num number of times.
0 1 0 1 0 1 0 0
Result: 84
16
BITWISE OPERATOR(Cont.)
Right shift (>>) operator shifts the bit pattern to the right side.
It is written as x>>num; which means shifting the bits of x towards
right by num number of times.
Example: int x = 42;
int result = x>>1; result=?
Binary representation of 42 in 8 bits: 0010 1010
0 0 1 0 1 0 1 0 Discard
insert
0 if num is +ve
and 1 if -ve
0 0 0 1 0 1 0 1
Result: 21
17
SPECIAL OPERATORS
C Supports Some special operators:
20
PRACTICE PROGRAMS
21
Thank You
22