Table of Contents
The increment operator can appear before or after a variable. Both forms increase the variable by 1, but they differ in the value produced when the operation is used inside an expression.
#include <stdio.h>
int main()
{
int a = 5;
int b = 5;
printf("Pre-increment: %d\n", ++a);
printf("Post-increment: %d\n", b++);
printf("a = %d\n", a);
printf("b = %d\n", b);
return 0;
}
Example Output #
Pre-increment: 6
Post-increment: 5
a = 6
b = 6
Explanation #
In pre-increment:
++a
a is incremented before its value is used by the expression. Therefore, ++a produces 6.
In post-increment:
b++
the original value of b is used by the expression first. The increment is then applied, so the expression produces 5, while b becomes 6.
Both forms ultimately increase the variable by 1; the difference is the value produced by the expression.