UMBC CS 201, Spring 02
UMBC CMSC 201 Spring '02 CSEE | 201 | 201 S'02 | lectures | news | help

Increment and Decrement Operators

It is so common to write code like:
  total = total + 1;     /* increment total */
that C provides a "shorthand" way of doing it:
  total++;              /* increment total */



Pre-increment vs. Post-increment (or ++X vs. X++)

--X and X-- work the same way, except 1 is being subtracted from X.

Example:

  x = 3;

  /* prints 4 */
  printf("The value of X is %d\n", ++x);  

  /* and the value of x is now 4 */
  /* prints 4 AGAIN */
  printf("The value of X is %d\n", x++); 

  /* but the value of x is now 5 */

Is all this a good idea?


CSEE | 201 | 201 S'02 | lectures | news | help

Thursday, 17-Jan-2002 13:51:58 EST