A Crash Course in C - copyright 1995 by Anand Mehta - Basic Operators

Examples of the Increment and Decrement Operators

/** examples of the increment and decrement operators **/ int main(void) { int x,y; x=5; /** prefix increment **/ y = ++x; printf("++x: x=%d, y=%d\n", x, y); x=5; /** postfix increment **/ y = x++; printf("x++: x=%d, y=%d\n", x, y); x=5; /** prefix decrement **/ y = --x; printf("--x: x=%d, y=%d\n", x, y); x=5; /** postfix decrement **/ y = x--; printf("x--: x=%d, y=%d\n", x, y); return 0; }

Output:

++x: x=6, y=6 x++: x=6, y=5 --x: x=4, y=4 x--: x=4, y=5

Next slide