C Programming Part 4
C Programming Part 4
Ex : a+b;
Where a,b are operands and + is the operator.
Types of Operator :
1) Arithmetic Operators.
2) Relational Operators.
3) Logical Operators.
4) Assignment Operators.
5). Unary Operators.
6) Conditional Operators.
7) Special Operators.
8) Bitwise Operators.
9) Shift Operators.
Arithmetic Operators
An arithmetic operator performs mathematical operations such as addition, subtraction and
multiplication on numerical values (constants and variables).
C Program to demonstrate the working of arithmetic operators
#include <stdio.h>
void 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);
C PROGRAMMING Page 31
Output
a+b = 13
a-b = 5
a*b = 36
a/b = 2
Remainder when a divided by b=1
Relational Operators. A relational operator checks the relationship between two operands.
If the relation is true, it returns 1; if the relation is false, it returns value 0.
Operands may be variables, constants or expressions.
#include <stdio.h>
int main()
int a = 5, b = 5, c = 10;
C PROGRAMMING Page 32
printf("%d < %d = %d \n", a, b, a < b); //false
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
C PROGRAMMING Page 33
5 <= 5 = 1
5 <= 10 = 1
Logical Operators.
These operators are used to combine the results of two or more conditions. An expression
containing logical operator returns either 0 or 1 depending upon whether expression results true
or false. Logical operators are commonly used in decision making in C programming.
Truth Table
Op1 Op2 Op1 && Op2
true true true
true false false
false true false
false false false
Logical OR : If any one condition true the complete condition becomes true.
Truth Table
Op1 Op2 Op1 // Op2
true true true
true false true
false true true
false false false
Logical Not : This operator reverses the value of the expression it operates on i.e, it makes a
true expression false and false expression true.
Op1 Op1 !
true false
false true
#include <stdio.h>
C PROGRAMMING Page 34
int main()
return 0;
Output
(a = b) || (c < b) equals to 1
C PROGRAMMING Page 35
(a != b) || (c < b) equals to 0
!(a != b) equals to 1
!(a == b) equals to 0
Assignment Operators. Assignment operators are used to assign a value (or) an expression
(or) a value of a variable to another variable.
Ex : x=10;
y=a+b;
z=p;
„C‟ provides compound assignment operators to assign a value to variable in order to assign a
new value to a variable after performing a specified operation.
#include <stdio.h>
int main()
int a = 5, c;
c = a;
C PROGRAMMING Page 36
printf("c = %d \n", c);
c += a; // c = c+a
c -= a; // c = c-a
c *= a; // c = c*a
c /= a; // c = c/a
c %= a; // c = c%a
return 0;
Output
c=5
c = 10
c=5
c = 25
c=5
c=0
C PROGRAMMING Page 37
Unary operators are having higher priority than the other operators. Unary operators, meaning
they only operate on a single operand.
1. Increment operator is used to increment the current value of variable by adding integer 1.
We have two types of increment operator i.e Pre-Increment and Post-Increment Operator.
Pre-Increment
Pre-increment operator is used to increment the value of variable before using in the expression. In
the Pre-Increment value is first incremented and then used inside the expression.
b = ++y;
In this example suppose the value of variable „y‟ is 5 then value of variable „b‟ will be 6 because
the value of „y‟ gets modified before using it in a expression.
Post-Increment
Post-increment operator is used to increment the value of variable as soon as after executing
expression completely in which post increment is used. In the Post-Increment value is first used
in a expression and then incremented.
b = x++;
In this example suppose the value of variable „x‟ is 5 then value of variable „b‟ will be 5 because
old value of „x‟ is used.
Note :
We cannot use increment operator on the constant values because increment operator operates on
only variables. It increments the value of the variable by 1 and stores the incremented value back
to the variable
C PROGRAMMING Page 38
b = ++5;
or
b = 5++;
Operator Meaning
++x Pre increment
- -x Pre decrement
x++ Post increment
x-- Post decrement
Where
1 : ++x : Pre increment, first increment and then do the operation.
2 : - -x : Pre decrement, first decrements and then do the operation.
3 : x++ : Post increment, first do the operation and then increment.
4 : x- - : Post decrement, first do the operation and then decrement.
Output
++a = 11
--b = 99
++c = 11.500000
++d = 99.500000
C PROGRAMMING Page 39
Multiple increment operators inside printf
#include<stdio.h>
void main() {
int i = 1;
printf("%d %d %d", i, ++i, i++);
}
Output : 3 3 1
Pictorial representation
Explanation of program
I am sure you will get confused after viewing the above image and output of program.
1. Whenever more than one format specifiers (i.e %d) are directly or indirectly related with
same variable (i,i++,++i) then we need to evaluate each individual expression from right
to left.
2. As shown in the above image evaluation sequence of expressions written inside printf
will be – i++,++i,i
3. After execution we need to replace the output of expression at appropriate place
No Step Explanation
Evaluate
1 At the time of execution we will be using older value of i = 1
i++
Evaluate At the time of execution we will be increment value already modified after
2
++i step 1 i.e i = 3
C PROGRAMMING Page 40