In this video tutorial we show the differences and working of post-increment and pre-increment operators.
Note: In pre-increment, first the value of the variable is incremented after that the assignment or other operations are carried. In post-increment, first assignment or other operations occur, after that the value of the variable is incremented.
Post-increment and Pre-increment Operator: C Program
[youtube https://www.youtube.com/watch?v=zdd7_hOorn0]
Pre-increment Operator: C Program
#include < stdio.h > int main() { int a = 10, b; b = ++a; printf("b = %d\n\n", b); printf("a = %d\n", a); return 0; }
Output:
b = 11
a = 11
Here first the value of a increments and then is assigned to variable b. So both a and b value will be 11.
#include < stdio.h > int main() { int a = 10; printf("a = %d\n\n", ++a); printf("a = %d\n", a); return 0; }
Output:
a = 11
a = 11
Here in the first printf statement a value gets incremented and then printed out.
Post-increment Operator: C Program
#include < stdio.h > int main() { int a = 10, b; b = a++; printf("b = %d\n\n", b); printf("a = %d\n", a); return 0; }
Output:
b = 10
a = 11
Here first value of a(i.e., 10) is assigned to b and then value of a is incremented. So b = 10 and a = 11 is printed.
#include < stdio.h > int main() { int a = 10; printf("a = %d\n\n", a++); printf("a = %d\n", a); return 0; }
Output:
a = 10
a = 11
Here in the first printf statement a value gets printed after that its value gets incremented, which is shown in second printf statement.
For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List
For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert