Post-increment and Pre-increment Operator: C Program


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 Link: https://www.youtube.com/watch?v=zdd7_hOorn0 [Watch the Video In Full Screen.]


Pre-increment Operator: C Program

  1. #include < stdio.h >  
  2.   
  3. int main()  
  4. {  
  5.     int a = 10, b;  
  6.   
  7.     b = ++a;  
  8.   
  9.     printf("b = %d\n\n", b);  
  10.     printf("a = %d\n", a);  
  11.   
  12.     return 0;  
  13. }  

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.

  1. #include < stdio.h >  
  2.   
  3. int main()  
  4. {  
  5.     int a = 10;  
  6.   
  7.     printf("a = %d\n\n", ++a);  
  8.     printf("a = %d\n", a);  
  9.   
  10.     return 0;  
  11. }  

Output:
a = 11
a = 11

Here in the first printf statement a value gets incremented and then printed out.

Post-increment Operator: C Program

  1. #include < stdio.h >  
  2.   
  3. int main()  
  4. {  
  5.     int a = 10, b;  
  6.   
  7.     b = a++;  
  8.   
  9.     printf("b = %d\n\n", b);  
  10.     printf("a = %d\n", a);  
  11.   
  12.     return 0;  
  13. }  

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.

  1. #include < stdio.h >  
  2.   
  3. int main()  
  4. {  
  5.     int a = 10;  
  6.   
  7.     printf("a = %d\n\n", a++);  
  8.     printf("a = %d\n", a);  
  9.   
  10.     return 0;  
  11. }  

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

Leave a Reply

Your email address will not be published. Required fields are marked *