UMBC CS 201, Fall 05
UMBC CMSC 201 Fall '05
CSEE | 201 | 201 F'05 | 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 F'05 | lectures | news | help

Sunday, 21-Aug-2005 09:53:19 EDT