0% found this document useful (0 votes)
3 views50 pages

Operators

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views50 pages

Operators

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

Operators

Copyright © 2021, ABES Engineering College


OPERators
An operator specifies the operation to be applied to its operands.
The values/variables upon which the operation is performed are
called operands.
Combination of operator and operands is called an expression.
a+b
a and b are operands and ‘+’ is a operator

2
Copyright © 2021, ABES Engineering College
OPERators

There are 3 types of operators:-


a. Unary operator
b. Binary Operator
c. Ternary Operator

Further there are more categories of operators.

3
Copyright © 2021, ABES Engineering College
CLASSIFICATION OF OPERATORS
Arithmetic operators (Binary)
Relational Operators (Binary)
Logical Operators (Binary)
Increments and Decrement Operators (unary)
Conditional Operators (ternary)
Bitwise Operators (Binary)
Special Operators.
Assignment Operators
4
Copyright © 2021, ABES Engineering College
CLASSIFICATION OF OPERATORS

5
Copyright © 2021, ABES Engineering College
ARITHMETIC OPERATORS
+,-, /, * AND %
% Operator is used to calculate remainder.
• In case of modulo division, if the numerator is smaller than the
denominator, then answer will be the number itself.
eg, 5%10=5
• In case of modulo division, the sign of the output will always be
equal to sign of numerator.
A=-5%10, A=-5

6
Copyright © 2021, ABES Engineering College
ARITHMETIC OPERATORS

• In case of division, if the number is smaller than the denominator,


then answer will always be truncated towards zero.
A=5/10
A=0

7
Copyright © 2021, ABES Engineering College
Arithmetic operators
//program on arithmetic operations
c = a*b;
#include <stdio.h>
printf("a*b = %d \n",c);
int main() c = a/b;
{ int a = 9,b = 4, c; printf("a/b = %d \n",c);
c = a+b; c = a%b;

printf("a+b = %d \n",c); printf("Remainder


when a divided by b =
c = a-b;
%d \n",c);
printf("a-b = %d \n",c); return 0; }
8
Copyright © 2021, ABES Engineering College }
relational operators
Relational operators are used to compare 2 quantities together.
Relational operator always give output in the form of true or
false (i.e. 1 or 0)
Relational Operators are:
> , < , >= , <=, ==, !=

9
Copyright © 2021, ABES Engineering College
relational operators

10
Copyright © 2021, ABES Engineering College
relational operators
Assume variable A holds 10 and variable B holds 20 then:
• == Checks if the value of two operands is equal or not, if yes then
condition becomes true.
(A == B) is not true.
• != Checks if the value of two operands is equal or not, if values are
not equal then condition becomes true.
(A != B) is true.

11
Copyright © 2021, ABES Engineering College
relational operators
• Checks if the value of left operand is greater (>) than the value of
right operand, if yes then condition becomes true.
(A > B) is not true.
• Checks if the value of left operand is less(<) than the value of right
operand, if yes then condition becomes true.
(A < B) is true.
• >= Checks if the value of left operand is greater than or equal to the
value of right operand, if yes then condition becomes true.
• (A >= B) is not true.
12
Copyright © 2021, ABES Engineering College
relational operators
• <= Checks if the value of left operand is less than or equal to the
value of right operand, if yes then condition becomes true.
(A <= B) is true

13
Copyright © 2021, ABES Engineering College
relational operators
//example
#include<stdio.h> printf(“a<b : %d”,a<=b);
int main () printf(“a==b: %d”,a==b);
{ printf(“a!=b: %d”,a!=b);

int a=10,b=20; return 0;


}
printf(“a>b: %d”,a>b);
printf(“a<b : %d”,a<b);
printf(“a>=b : %d”,a>=b);
14
Copyright © 2021, ABES Engineering College
Logical operators
Logical Operators: &&, ||, !
Logical AND and OR is binary operator , ! Is unary operator
Assume variable A holds 10 and variable B holds 20 then:
• && Called Logical AND operator. If both the operands are non zero
then then condition becomes true.
(A && B) is true.
• || Called Logical OR Operator. If any of the two operands is non
zero then then condition becomes true. (A || B) is true.

15
Copyright © 2021, ABES Engineering College
Logical operators
• ! Called Logical NOT Operator. Use to reverses the logical state of
its operand. If a condition is true then Logical NOT operator will
make false.
!(A && B) is false.

16
Copyright © 2021, ABES Engineering College
Logical operators
//logical operator
#include<stdio.h>
void main()
{
int a=10,b=20;
printf("%d",a&&b);
printf("%d",a||b);
printf("%d",!(a&&b)); }
17
Copyright © 2021, ABES Engineering College
assignment operators
• = Simple assignment operator, Assigns values from Assigns
values from right side operands to left side operand.
• int a=15
(15 is assigned to a)
• Note: there can be single variable at LHS of assignment
operator.
a=a+b; c=d+e;
• A+b=c+d //not allowed

18
Copyright © 2021, ABES Engineering College
assignment operators
• Shorthand assignment operator
• C = A + B will assign value of A + B into C
• += Add AND assignment operator, It adds right operand to the
left operand and assign the result to left operand
C += A is equivalent to C = C + A
• -= Subtract AND assignment operator, It subtracts right operand
from the left operand and assign the result to left operand
C -= A is equivalent to C = C - A

19
Copyright © 2021, ABES Engineering College
assignment operators
• *= Multiply AND assignment operator, It multiplies right operand
with the left operand and assign the result to left operand
C *= A is equivalent to C = C * A
• /= Divide AND assignment operator, It divides left operand with the
right operand and assign the result to left operand
C /= A is equivalent to C = C / A
• %= Modulus AND assignment operator, It takes modulus using two
operands and assign the result to left operand
C %= A is equivalent to C = C % A
20
Copyright © 2021, ABES Engineering College
assignment operators
// Assignment operators
a -= 10;
#include <stdio.h>
printf("Value of a is %d\n", a);
int main()
a *= 10;
{
printf("Value of a is %d\n", a);
int a = 10;
a /= 10;
printf("Value of a is %d\n", a);
printf("Value of a is %d\n", a);
a += 10;
return 0;
printf("Value of a is %d\n", a);
}
21
Copyright © 2021, ABES Engineering College
Conditional operator/ Ternary operator
• It is a ternary operator that works on 3 operands. It is an alternate
of if-else.
• Syntax:
condition ? True: false
Exp1? Exp2: Exp3
If the Exp 1 is true then Exp 2 will be evaluated. If the Exp1 is false
then Exp 3 will be evaluated.
variable= Exp1? Exp2: Exp3

22
Copyright © 2021, ABES Engineering College
Conditional operator/ Ternary operator
• variable= Exp1? Exp2: Exp3

23
Copyright © 2021, ABES Engineering College
Conditional operator
// write a program to check whether 2 numbers are equal or not
using conditional operator
#include<stdio.h>
void main() {
int a,b;
printf("enter 2 numbers");
scanf("%d%d",a,&b);
a==b ? printf("equal") : printf("not equal"); }
24
Copyright © 2021, ABES Engineering College
BITWISE operator

& (bitwise AND): The result of AND is 1 only if both bits are 1.
& (bitwise OR): The result of AND is 1 only if any of the two bits is 1.
^ (bitwise XOR): XOR is 1 if the odd number of 1’s are present or if
the two bits are different.
<< (left shift) : Takes two numbers, the left shifts the bits of the first
operand, and the second operand decides the number of places to
shift.

25
Copyright © 2021, ABES Engineering College
BITWISE operator

>> (right shift): Takes two numbers, right shifts the bits of the first
operand, and the second operand decides the number of places to
shift.
~ (bitwise NOT): Takes one number and inverts all bits of it.

26
Copyright © 2021, ABES Engineering College
BITWISE operator
Truth table of Bitwise operator

27
Copyright © 2021, ABES Engineering College
Example of BITWISE operator
#include <stdio.h>
Output:
int main() { int a = 5, b = 9;
printf("a&b = %d\n", a & b); a&b = 1
a|b = 13
printf("a|b = %d\n", a | b); a^b = 12
printf("a^b = %d\n", a ^ b); ~a = -6
b<<1 = 18
printf("~a = %d\n", a = ~a); b>>1 = 4
printf("b<<1 = %d\n", b << 1);
printf("b>>1 = %d\n", b >> 1);
return 0; }
28
Copyright © 2021, ABES Engineering College
UNARY operator

Increment ( ++ )
Decrement ( — )

29
Copyright © 2021, ABES Engineering College
UNARY operator
Increment ( ++ )
The increment operator ( ++ ) is used to increment the value of the
variable by 1. The increment can be done in two ways:

Preincrement:
The operator precedes the operand (e.g., ++a). The value of the
operand will be changed before execution of the statement or before
it is used.

30
Copyright © 2021, ABES Engineering College
UNARY operator
Postincrement:
The operator follows the operand (e.g., a++). The value operand will
be altered after execution of statement or after it is used.

31
Copyright © 2021, ABES Engineering College
UNARY operator
//program on pre-increment
#include <stdio.h> Output:
a=6
int main() { b=6
int a = 5;
int b = ++a;
printf(" a = %d\n", a);
printf(“b= %d", b);
return 0; }
32
Copyright © 2021, ABES Engineering College
UNARY operator
//program on post-increment
#include <stdio.h> Output:
a=6
int main() { b=5
int a = 5;
int b = a++;
printf(" a = %d\n", a);
printf(“b= %d", b);
return 0; }
33
Copyright © 2021, ABES Engineering College
UNARY operator
Decrement ( -- )
The decrement operator ( -- ) is used to decrement the value of the
variable by 1. The decrement can be done in two ways:

Predecrement:
The operator precedes the operand (e.g., --a). The value of the
operand will be changed before execution of the statement or before
it is used.

34
Copyright © 2021, ABES Engineering College
UNARY operator
Postdecrement:
The operator follows the operand (e.g., a--). The value operand will
be altered after execution of statement or after it is used.

35
Copyright © 2021, ABES Engineering College
UNARY operator
//program on pre-decrement
#include <stdio.h> Output:
a=4
int main() { b=4
int a = 5;
int b = --a;
printf(" a = %d\n", a);
printf(“b= %d", b);
return 0; }
36
Copyright © 2021, ABES Engineering College
UNARY operator
//program on post-decrement
#include <stdio.h> Output:
a=4
int main() { b=5
int a = 5;
int b = a--;
printf(" a = %d\n", a);
printf(“b= %d", b);
return 0; }
37
Copyright © 2021, ABES Engineering College
sizeof operator
It is a compile-time unary operator which can be used to compute the
size of its operand.
Syntax:
sizeof(Expression);
where ‘Expression‘ can be a data type or a variable of any type.
Return: It returns the size of the given expression.

38
Copyright © 2021, ABES Engineering College
sizeof operator
// sizeof operator
Output:
#include <stdio.h>
1
int main() { 2
4
printf("%d\n", sizeof(char));
printf("%d\n", sizeof(int)); Note: sizeof() may give
different output
printf("%d\n", sizeof(float)); according to machine,
program on a 16-bit
return 0; }
compiler.
39
Copyright © 2021, ABES Engineering College
Address of operator
This operator returns an integer value which is the address of its
operand in the memory.
Syntax:
&operand

40
Copyright © 2021, ABES Engineering College
Address of operator
// C program to illustrate the use of address operator
Output:
#include <stdio.h>
int main() The address of x is
6422044
{
int x = 100;
printf("The address of x is %u", &x);
return 0; }
41
Copyright © 2021, ABES Engineering College
PRECEDENCE OF OPERATORS
Each operator in C has a precedence associated with it.
In a compound expression, if the operators involved are of
different precedence, the operator of higher precedence is
evaluated first.
Compound expression:
a+b+c*d

42
Copyright © 2021, ABES Engineering College
PRECEDENCE OF OPERATORS
Each operator in C has a precedence associated with it.
In a compound expression, if the operators involved are of
different precedence, the operator of higher precedence is
evaluated first.

43
Copyright © 2021, ABES Engineering College
ASSOCIATIVITY OF OPERATORS
KEY POINTS:
An operator can be either left-to-right associative or right-to-left
associative.
The operators with same precedence always have the same
associativity.
If operators are left-to-right associative, they are applied in left-to-right
order i.e. the operator which appears towards left will be evaluated
first.
44
Copyright © 2021, ABES Engineering College
ASSOCIATIVITY OF OPERATORS
KEY POINTS:
If they are right-to-left associative, they will be applied in the right-to-
left order.
The multiplication and the division operators are left-to-right
associative. Hence, in expression 2*3/5, the multiplication operator is
evaluated prior to the division operator as it appears before the
division operator in left-to-right order.

45
Copyright © 2021, ABES Engineering College
Precedence and associativity table

46
Copyright © 2021, ABES Engineering College
Precedence and associativity table

47
Copyright © 2021, ABES Engineering College
Precedence and associativity table

48
Copyright © 2021, ABES Engineering College
Precedence and associativity table

49
Copyright © 2021, ABES Engineering College
Precedence and associativity table

50
Copyright © 2021, ABES Engineering College

You might also like