Arithmetic, Unary, Relational & Logical Operators
Arithmetic, Unary, Relational & Logical Operators
Operator
2
Operator in c
🞂 In C, an operator is a symbol(+, -, *, /) which operates on a
certain data type to perform mathematical, logical,
relational operations.
🞂 An operator indicates an operation to be performed on
data results into a value.
🞂 An operand is a data item (variable or constant) on which
operators can perform the operations.
🞂 An expression is a combination of variables, constants and
operators.
🞂 In C, every expression results(evaluates) into a value of
certain data type
3
Example
4
Operators Classifications
5
Comma Operator
Used to separate two or more expression.
Example:
#include <stdio.h>
int main() {
,
printf("Addition of 4 + 4 is %d " 4+4);
return 0;
}
OUTPUT:
Addition of 4 + 4 is 8
6
Arithmetic Operators
🞂 Used to perform arithmetic operations such as Addition,
Subtraction, Multiplication and Division.
🞂 Most commonly used operators in computer programming
languages.
🞂 The operands involved in arithmetic operations must
represent numerical value.
🞂 Thus, the operands can be an integer type, floating point
types, double and char(In C, characters represent
numerical value according to ASCII table).
7
8
Types
9
Unary Operators in C
🞂 act upon a single operand to produce a new value
10
Example – Unary minus
#include <stdio.h>
int main()
{
int x = -5;
int y = -x;
printf("The value of x = %d \nThe value of y = %d ",x, y);
return 0;
}
OUTPUT:
The value of x = - 5
The value of y = 5
11
Increment Operator
🞂 to add one to their operand
🞂 'Post' means after - that is, the increment is done after the
variable is read.
🞂 'Pre' means before - so the variable value is incremented first,
then used in the expression.
Example:
#include <stdio.h> OUTPUT:
int main() { The value of a = 2
int a = 2; The value of a = 3
printf("The value of a = %d ", a);
a = a ++;
printf("\nThe value of a = %d ", a);
return 0;
}
12
Decrement Operator
🞂 to reduce or subtract one to their operand.
🞂 'Post' means after - that is, the decrement is done after the variable
is read.
🞂 'Pre' means before - so the variable value is decremented first, then
used in the expression.
Example:
#include <stdio.h>
int main() {
int a = 2;
printf("The value of a = %d ", a);
a = --a;
printf("\nThe value of a = %d ", a);
return 0;
}
OUTPUT:
The value of a = 2
The value of a = 1
13
Address Operator(&)
🞂 Address operator is used to storing the address of
the variable in C.
🞂 This is denoted by an ampersand (&).
🞂 This is also used for scanning the user input.
Example
#include <stdio.h> OUTPUT:
int main() The value of n = 10
{ Address of n = 2293436
int n = 10;
printf("The value of n = %d ",n);
printf("\nAddress of n = %u ",&n);
return 0;
}
14
sizeof() Operator
• used to compute the size of its operand.
• The result of sizeof is of unsigned integral type which is usually denoted
by size_t.
Example:
#include <stdio.h>
int main()
{
printf("The 'int' datatype is \t\t %lu bytes\n", sizeof(int));
printf("The 'unsigned int' data type is\t %lu bytes\n", sizeof(unsigned int));
printf("The 'short int' data type is\t %lu bytes\n", sizeof(short int));
printf("The 'long int' data type is\t %lu bytes\n", sizeof(long int));
printf("The 'long long int' data type is %lu bytes\n", sizeof(long long int));
printf("The 'float' data type is\t %lu bytes\n", sizeof(float));
printf("The 'char' data type is\t\t %lu bytes\n", sizeof(char));
}
OUTPUT:
The 'int' datatype is 4 bytes
The 'unsigned int' data type is 4 bytes
The 'short int' data type is 2 bytes
The 'long int' data type is 8 bytes
The 'long long int' data type is 8 bytes
The 'float' data type is 4 bytes
The 'char' data type is 1 bytes
15
Binary Operator in C
-act upon a two operands to produce a new value.
Operator Description Example
+ Addition 1+2=3
- Subtraction 2-1=1
* multiplication 2*3=6
/ Division 6/3=2
% Modular 5 % 2 = 1 (remainder)
Division
16
Binary Operators
17
Example
int main() {
int a = 5;
int b = 2;
int l = a + b;
int m = a - b;
int n = a * b;
int o = a / b;
int p = a % b;
printf("Addition of a and b = %d ", l);
printf("\nSubtraction of a and b = %d ", m);
printf("\nMultiplication of a and b = %d ", n);
printf("\nDivision of a and b = %d ", o);
printf("\nRemainder of a and b = %d ", p);
return 0;
}
18
OUTPUT
Addition of a and b = 7
Subtraction of a and b = 3
Multiplication of a and b = 10
Division of a and b = 2
Remainder of a and b = 1
19
Relational Operators
🞂 Relational operators are used to compare
arithmetic, logical and character expressions.
🞂 If the condition is "true" it returns 1, otherwise, it
returns 0.
20
Relational operator Types
Operator Description Exampl Return
e Value
> Greater than 5>3 1
< Less than 5<3 0
<= Less than equal to 5 <= 5 1
>= Greater than equal 6 >= 5 1
to
== Equal to 5 == 3 0
!= Not equal to 3 != 3 0
21
Example
#include <stdio.h>
void main()
{
int a = 21;
int b = 10;
int c ;
if( a == b )
printf("Line 1 - a is equal to b\n" ); OUTPUT:
else
Line 1 - a is not equal to b
printf("Line 1 - a is not equal to b\n" );
if ( a < b )
Line 2 - a is not less than b
printf("Line 2 - a is less than b\n" ); Line 3 - a is greater than b
else
printf("Line 2 - a is not less than b\n" );
if ( a > b )
printf("Line 3 - a is greater than b\n" );
else
printf("Line 3 - a is not greater than b\n" );
}
22
Example
#include <stdio.h>
int main()
{
int a = 5;
int b = 10;
if(a < b)
printf("a is the smallest number");
else
printf("b is the smallest number");
return 0;
}
OUTPUT:
a is the smallest number
23
Logical Operators
🞂 Logical operators are used to check (or) compare
the logical relations between the expressions.
🞂 Logical operator, returns 1 if given condition is
true, 0 if given condition is false.
24
Logical Operator Table:
Note:
| - vertical bar symbol
|| - pipe
25
Logical AND Operator
#include <stdio.h>
int main()
{
int a = 20;
int b = 10;
int c = 15;
if(a<b && b<c)
printf(" C is the greatest number of all");
else
printf(" C is not greatest number of all");
return 0;
}
OUTPUT:
C is not greatest number of all
26
Logical OR
#include <stdio.h>
int main()
{
int a = 20;
int b = 10;
int c = 15;
if(c>a || c>b)
printf(" C is not smallest and may not biggest of all ");
else
printf(" C is smallest of all");
return 0;
}
OUTPUT:
C is not smallest and may not biggest of all
27
Logical NOT
#include <stdio.h>
int main() {
int a = 20;
int b = 10;
if(a != b )
printf("a is not equal to b");
else
printf(" a is equal to b");
return 0;
}
OUTPUT:
a is not equal to b
28
Bitwise Operators
🞂 A bitwise operator which operates on each bit of
data.
🞂 Bitwise operators only operates on integer
operands such as int, short int, long int.
29
Bitwise Operators Types
Operator Description
& Bitwise AND
| Bitwise OR
^ Bitwise XOR
>> Right shift
<< Left shift
~ One's Complement
Note:
•& - ambersand
•| - vertical bar
• ^ - caret or circumflex
•~ - tilde 30
Example- Bitwise AND
#include <stdio.h>
INPUT OUTPUT
int main()
X Y Z
{
0 0 0
int a = 10, b = 2;
0 1 0
printf("a & b = %d ", a & b); 1 0 0
return 0; 1 1 1
}
OUTPUT: Bitwise AND operator table
a&b=2
31
Example- Bitwise OR
#include <stdio.h>
int main() INPUT OUTPUT
{ X Y Z
int a = 10, b = 2; 0 0 0
printf("a | b = %d ", a | b); 0 1 1
return 0; 1 0 1
} 1 1 1
OUTPUT:
Bitwise OR operator table
a | b = 10
32
Example – Bitwise XOR
#include <stdio.h> INPUT OUTPUT
int main() { X Y Z
int a = 10, b = 2; 0 0 0
printf("a ^ b = %d ", a ^ b); 0 1 1
return 0; 1 0 1
} 1 1 0
33
Right shift Operator (>>)
🞂 also requires two operands.
🞂 Takes two numbers, right shifts the bits of the first
operand, the second operand decides the number of places
to shift.
🞂 In other words right shifting an integer “x” with an integer
“y” denoted as ‘(x>>y)‘ is equivalent to dividing x with
2^y.
34
Example
#include <stdio.h>
int main() {
int a = 8,b;
a>>= 1; //a=a>>1
b = a;
printf("The Right shifted data of 8 by 1 = %d ",b);
return 0;
}
OUTPUT:
The Right shifted data of 8 by 1 = 4
35
Left shift Operator (<<)
🞂 requires two operands.
🞂 Takes two numbers, left shifts the bits of the first
operand, the second operand decides the number of
places to shift.
🞂 In other words left shifting an integer “x” with an
integer “y” denoted as ‘(x<<y)’ is equivalent to
multiplying x with 2^y (2 raised to power y).
36
Example
#include <stdio.h>
int main() {
int a = 4, b;
a<<= 1;
b = a;
printf("The Left shifted data of 4 by 1 = %d ",b);
return 0;
}
OUTPUT:
The Left shifted data of 4 by 1 = 8
37
One's Complement
🞂 a unary operator
🞂 always precedes to the variable or an operand. One's
Complement operator inverts bits of its operand. So,
1s becomes 0s and 0s becomes 1s.
Input Output
X Y
0 1
1 0
38
Example
#include <stdio.h>
int main()
{
unsigned int a = 10;
a = ~a;
printf("After One's Complement a = %u", a);
return 0;
}
OUTPUT:
After One's Complement a = 65525
Note:
10 – 0000 0000 0000 1010
~10 – 1111 1111 1111 0101
39
Conditional or Ternary Operator
🞂 sometimes called as ternary operator because it
involves three operands.
🞂 It is best understood by considering the following
example
younger = son < father ? 18 : 40;
40
Syntax
🞂 The conditional operator contains a condition
followed by two statements or values.
🞂 If the condition is true the first statement is
executed, otherwise the second statement or value.
🞂 Syntax:
●condition ? (value 1) : (value 2)
41
How Conditional Operator Works
🞂 True
◦ younger = true ? 18 : 40;
🞂 False
◦ younger = false ? 18 : 40;
42
Program without Conditional Operator
#include <stdio.h>
int main() {
int son = 18;
int father = 40;
int younger_age;
if(son < father)
younger_age = son;
else
younger_age = father;
printf("%d is the younger age", younger_age);
return 0;
}
43
OUTPUT
🞂 18 is the younger age
44
Program Using Conditional Operator
#include <stdio.h>
int main() {
int son = 18;
int father = 40;
int younger_age;
younger_age = son < father ? son : father;
printf("%d is the younger age", younger_age);
return 0;
}
OUTPUT:
18 is the younger age
45
Operator Precedence
47
Operator Operation Clubbing Priority
() Function call Left to Right 1st
[] Array " "
→ Structure Operator " "
. Structure Operator " "
+ Unary plus Right to Left 2nd
- Unary minus " "
++ Increment " "
-- Decrement " "
! Not Operator " "
~ One's Complement " "
* Pointer Operation " "
& Address Operator " "
sizeof Size of an data type " "
(typecast) Type Cast " " 48
Operator Operation Clubbing Priority
* Multipication Left to Right 3rd
/ Division " "
% Modular Division " "
+ Addition Left to Right 4th
- Subtraction " "
<< Left shift Left to Right 5th
>> Right shift " "
< Less than Left to Right 6th
<= Less than or " "
equal to
> Greater than " "
>= Greater than or " "
Equalto
49
Operator Operation Clubbing Priority
= Equality Left to Right 7th
!= InEquality " "
& Bitwise AND Left to Right 8th
^ Bitwise XOR Left to Right 9th
| Bitwise OR Left to Right 10th
&& Logical AND Left to Right 11th
|| Logical OR Left to Right 12th
?: Conditional Right to Left 13th
Operator
^=, !=, <<=, > Assignment Right to Left 14th
>= Operator
, comma Right to Left 15th
operator 50
1. Arithmetic operators: *, /, %, +, -
2. Relational operators: >, <, >=, <=, ==, !=
3. Logical operators : !, &&, ||
4. Assignment operators: =
51
Question
🞂 10-3%8+6/4
52