You are on page 1of 33

DEVA Sir (NOTES)

OPERATORS
Mallesham Devasane (Code: DEVA10)
www.Unacademy.com/@devagatecse
DEVA Sir (NOTES)
Operators
What is Operators?
• An operator is a symbol that use to perform certain mathematic or logical operators
• Operator is used to manipulate data and variables.
Types of operators:
• Arithmetic operators

• Relational operators

• Logical operators

• Assignment operators

• Increment and Decrement operators

• Conditional operators

• Bitwise operators and Special Operators


DEVA Sir (NOTES)
Operators
Arithmetic operators:
• Arithmetic operators used to perform arithmetic operations such as multiplication, addition,
etc.
• *, / , % (Highest precedence, follows left to right)
• +, - (Lowest precedence, follows left to right)
DEVA Sir (NOTES)
Operators
Example:
#include <stdio.h>
int main()
{
int a = 9,b = 4, c;
c = a+b;
printf("a+b = %d \n",c);
c = a-b;
printf("a-b = %d \n",c);
c = a*b;
printf("a*b = %d \n",c);
c=a/b;
printf("a/b = %d \n",c);
c=a%b;
printf("Remainder when a divided by b = %d \n",c);
return 0;
}
DEVA Sir (NOTES)
Operators
Output:
a+b = 13
a-b = 5
a*b = 36
a/b = 2
Remainder when a divided by b=1
DEVA Sir (NOTES)
Operators
Relational Operator:
• Relational Operators compare between two operands and return in terms of true or false.
• There are following relational operators.
DEVA Sir (NOTES)
Operators
Example:
#include <stdio.h>
int main(){
int a = 5, b = 5, c = 10;
printf("%d == %d = %d \n", a, b, a == b); // true
printf("%d == %d = %d \n", a, c, a == c); // false
printf("%d > %d = %d \n", a, b, a > b); //false
printf("%d > %d = %d \n", a, c, a > c); //false
printf("%d < %d = %d \n", a, b, a < b); //false
printf("%d < %d = %d \n", a, c, a < c); //true
printf("%d != %d = %d \n", a, b, a != b); //false
printf("%d != %d = %d \n", a, c, a != c); //true
printf("%d >= %d = %d \n", a, b, a >= b); //true
printf("%d >= %d = %d \n", a, c, a >= c); //false
DEVA Sir (NOTES)
Operators
printf("%d <= %d = %d \n", a, b, a <= b); //true
printf("%d <= %d = %d \n", a, c, a <= c); //true
return 0;
}

Output:
5 == 5 = 1
5 == 10 = 0
5>5=0
5 > 10 = 0
5<5=0
5 < 10 = 1
5 != 5 = 0
5 != 10 = 1
5 >= 5 = 1
5 >= 10 = 0
5 <= 5 = 1
5 <= 10 = 1
DEVA Sir (NOTES)
Operators
Logical Operator:
• A logical operator used to compare or evaluate logical and relational expressions.
• There are following Logical operators.
DEVA Sir (NOTES)
Operators
Example:
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10, result;

result = (a == b) && (c > b);


printf("(a == b) && (c > b) equals to %d \n", result);

result = (a == b) && (c < b);


printf("(a == b) && (c < b) equals to %d \n", result);

result = (a == b) || (c < b);


printf("(a == b) || (c < b) equals to %d \n", result);

result = (a != b) || (c < b);


DEVA Sir (NOTES)
Operators
printf("(a != b) || (c < b) equals to %d \n", result);

result = !(a != b);


printf("!(a == b) equals to %d \n", result);

result = !(a == b);


printf("!(a == b) equals to %d \n", result);

return 0;
}
Output
(a == b) && (c > b) equals to 1
(a == b) && (c < b) equals to 0
(a == b) || (c < b) equals to 1
(a != b) || (c < b) equals to 0
!(a != b) equals to 1
!(a == b) equals to 0
DEVA Sir (NOTES)
Operators
Assignment Operator:
• An assignment operator is used to assign a constant or a value of one variable to another.
• = is a assignment operator.
• += is arithmetic assignment operator. Similarly -=, /=, *=, etc. can be used.
• You can use the assignment for multiple assignments ass follows:
• x=y=z=20; It is same as x=20, y=20, z=20;
• x+=y; It is same as x=x+y;
DEVA Sir (NOTES)
Operators
Example:
#include <stdio.h>
int main()
{
int a = 5, c;
c = a;
printf("c = %d \n", c);
c += a; // c = c+a
printf("c = %d \n", c);
c -= a; // c = c-a
printf("c = %d \n", c);
c *= a; // c = c*a
printf("c = %d \n", c);
c /= a; // c = c/a
printf("c = %d \n", c);
c %= a; // c = c%a
printf("c = %d \n", c);
return 0;
}
DEVA Sir (NOTES)
Operators
Output:
c=5
c = 10
c=5
c = 25
c=5
c=0
DEVA Sir (NOTES)
Operators
Increment and Decrement Operator:
Increment Operator:
• Increment operators are increase the value of sub-sequent
• Increment operator are two types as follows:
• Post Increment : variable++;
• Example:
• x=5; x++; // x is 6 after execution of these two statements.
• x=5; y=x++; //x is 6 and y is 5 after the execution.

• Pre Increment : ++ variable;


• Example:
• x=5; ++x; // x is 6 after execution of these two statements.
• x=5; y=++x; //x is 6 and y is 6 after the execution.
DEVA Sir (NOTES)
Operators
Decrement Operator(++).
• Decrement Operators decreases the value to one, two and so on
• Decrement operators are also two type as:
• Post Increment : variable--;
• Example:
• x=5;x--; // what is value of x after execution?
• x=5; y=x--; // what is the value of x and value of y after execution?
• Pre increment : -- variable;
• Example:
• x=5;-- x;
• x=5;y=-- x;
DEVA Sir (NOTES)
Operators
Example on pre-increment:
#include<stdio.h>
#include<conio.h>

void main()
{
int x,i;
i=10;
x=++i;
printf("x: %d", x);
printf("i: %d", i);
getch();
}
Output: x: 11
i: 11
DEVA Sir (NOTES)
Operators
Example post-increment:
#include<stdio.h>
#include<conio.h>

void main()
{
int x,i;
i=10;
x=i++;
printf("x: %d",x);
printf("i: %d",i);
getch();
}
Output: x: 10
i: 11
DEVA Sir (NOTES)
Operators
Example pre-decrement:
#include<stdio.h>
#include<conio.h>
void main()
{
int x,i;
i=10;
x=--i;
printf("x: %d",x);
printf("i: %d",i);
getch();
}
Output: x: 9
i: 9
DEVA Sir (NOTES)
Operators
Example post-decrement:
#include<stdio.h>
#include<conio.h>
void main()
{
int x,i;
i=10;
x=i--;
printf("x: %d",x);
printf("i: %d",i);
getch();
}
Output: x: 10
i: 9
DEVA Sir (NOTES)
Operators
Example of increment and decrement operator:
#include<stdio.h>
#include<conio.h>
void main()
{
int x,a,b,c;
a = 2;
b = 4;
c = 5;
x = a-- + b++ - ++c;
printf("x: %d",x);
getch();
}
Output: x: 0
DEVA Sir (NOTES)
Operators
Conditional Operator or Ternary Operator:
• Main features of conditional operator as follows:
• There are three operands: condition, true part and false part.
• Each conditional operator works from left to right (First condition then true or false part)
• Nested conditional operators evaluated from right to left (First right end conditional operator)
• Example of ternary operator is conditional operator
• condition? True parts: False parts

Syntax: (Condition? true_statement: false_statement);


DEVA Sir (NOTES)
Operators
Example:
#include <stdio.h>
int main()
{
int x=1, y ;
y = ( x ==1 ? 2 : 0 ) ;
printf("x value is %d\n", x);
printf("y value is %d", y);
}

Output: x value is 1
y value is 2
DEVA Sir (NOTES)
Operators
Bitwise Operator:
One of the c powerful features is a set of bit manipulation operators.
There are various bitwise operators in C as following table

Example:
#include <stdio.h>
int main()
{
int m = 40,n = 80,AND_opr,OR_opr,XOR_opr,NOT_opr ;
AND_opr = (m&n);
OR_opr = (m|n);
NOT_opr = (~m);
XOR_opr = (m^n);
DEVA Sir (NOTES)
Operators
printf("AND_opr value = %d\n",AND_opr );
printf("OR_opr value = %d\n",OR_opr );
printf("NOT_opr value = %d\n",NOT_opr );
printf("XOR_opr value = %d\n",XOR_opr );
printf("left_shift value = %d\n", m << 1);
printf("right_shift value = %d\n", m >> 1);
}
OUTPUT:
AND_opr value = 0
OR_opr value = 120
NOT_opr value = -41
XOR_opr value = 120
left_shift value = 80
right_shift value = 2
DEVA Sir (NOTES)
Operators
Special Operator:
There are many special operators use in Programming
• Comma Operators
• Size of operator

Comma Operator:
Evaluate of comma operator - Left to Right
Uses of comma operator a following:
• Multiple declaration
• Multiples initialisation
• Multiples statements
DEVA Sir (NOTES)
Operators
Example of comma Operator:
• x=12, y=10, z=18;
• for( i=0,j=0;i<5,i++, j++)

Sizeof() operator:
• sizeof operator calculate the size of data i.e. how many bytes a
specific data having
• sizeof(int); sizeof(float); sizeof(char);
• sizeof(void); is compilation error.
• int x; sizeof(x); It is also valid statement.
• sizeof() operator can be used to find size of basic data types and user
defined data types.
DEVA Sir (NOTES)
Operators
Precedence relations of Operators:
• When expression has more than one operator, operator precedence determines decides the
order of operators evaluation.
• Certain operators have higher precedence than others;
• Example:
• x = 7 + 3 * 2;
• It is evaluated as x = 7+(3*2); because * has higher precedence than +
DEVA Sir (NOTES)
Operators
List of Operator Precedence:
DEVA Sir (NOTES)
Operators
Example:
#include <stdio.h>
main()
{
int a = 20;
int b = 10;
int c = 15;
int d = 5;
int e;
e = (a + b) * c / d; // ( 30 * 15 ) / 5
printf("Value of (a + b) * c / d is : %d\n", e );
e = ((a + b) * c) / d; // (30 * 15 ) / 5
printf("Value of ((a + b) * c) / d is : %d\n" , e );
e = (a + b) * (c / d); // (30) * (15/5)
printf("Value of (a + b) * (c / d) is : %d\n", e );
e = a + (b * c) / d; // 20 + (150/5)
printf("Value of a + (b * c) / d is : %d\n" , e );
return 0;
}
DEVA Sir (NOTES)
Operators
Output:
Value of (a + b) * c / d is : 90
Value of ((a + b) * c) / d is : 90
Value of (a + b) * (c / d) is : 90
Value of a + (b * c) / d is : 50
DEVA Sir (NOTES)
Operators
Expression Evaluation in C:
• Evaluation of expression depends on mainly operator precedence relations.
• Operator precedence relations will help to find out the order of operators evaluations
• Precedence relations are three types: Highest, lowest, and equal precedence relations.
• Highest and lowest are easy to evaluate in any expression.
• When two operators have equal precedence, based on associativity rules it may evaluate left
to right or right to left order in the given expression.
DEVA Sir (NOTES)
Operators
• Example:
• x = 10 + 4 * 3 / 2 – 1;
• It will be evaluated as x = ( (10 + ( (4*3)/2 ) ) – 1 );
• 4*3 is 12, 12/2 is 6, 10+6 is 16, 16-1 is 15, 15 will be assigned to x.
• So x has value 15 after the execution of the above statement.

You might also like