Pre-increment Vs Post-increment operators. What’s the difference between pre-increment (++i) and post-increment (i++) operators. In this post, we’ll learn how pre-increment and post-increment operators work in programming.
In this post, You’ll find MCQ which will help you to understand this concept fully.
Pre-increment Vs Post-increment Operators
Pre-increment (++i)
When we use pre-increment operator, The value is first incremented and the used in an expression.
For example –
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
/* variable declaration */ int i , j; /* 5 is assigned in i */ i = 5; j = ++i; /* Print the value of j Output will be 6 */ printf ("%d", j); |
Post-increment (i++)
In post-increment, The current value is used in an expression and after that, it’s incremented.
For example –
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
/* variable declaration */ int i , j; /* 5 is assigned in i */ i = 5; j = i++; /* Print the value of j Output will be 5 */ printf ("%d", j); |
Program to print triangle pattern of number and stars
MCQ on Pre-increment and Post-increment Operators
1.
1 2 3 4 5 6 7 8 9 10 |
#include <stdio.h> int main() { int x = 4, y, z; y = --x; z = x--; printf("%d, %d, %d\n", x, y, z); return 0; } |
a) 4, 3, 3
b) 3, 3, 3
c) 2, 3, 3
d) 4, 4, 3
2.
1 2 3 4 5 6 7 8 9 |
#include <stdio.h> main() { int a = 1, b = 3; b= a++ + a++ + a++ + a++ + a++; printf("a = %d \n b = %d", a, b); } |
a) a = 6, b = 15
b) a =1 , b = 3
c) a = 1 , b =15
d) a = 2 , b = 4
3.
1 2 3 4 5 6 7 8 9 10 11 12 |
#include <stdio.h> main() { int a = 9, b = 9; a = b++; b = a++; b = ++b; printf("%d %d", a, b); } |
a) 9, 9
b) 10, 10
c) 9, 10
d) 10, 9
4.
1 2 3 4 5 6 7 8 9 10 |
#include <stdio.h> main() { int a, b; b = 10; a = ++b + ++b; printf("%d %d", a, b); } |
a) 24, 12
b) 23, 12
c) 23, 10
d) 24, 10
Solutions and Explanation
Ans1. C) 2, 3, 3
int x=4, y, z;
y = –x; . In this statement value of x is first decrement then assigned. So value of y is 3 and after decrement value of x is 3.
z = x–; . Now value of x is 3. This is post-decrement operation value is first assigned and then decrement. So value of z is 3.
Now the current value of x is 2.
Ans 2 a) a = 6, b = 15
Ans 3 b) 10, 10
Ans 4 a) 24, 12
Difference between pre-increment and post-increment video tutorial