Comp 210 Lab 13: Beware the ++ Operator

Feel free to use i++ or i--, but only if it is the only thing on the line. If you want to do two separate things, use two lines of code. Some abuses are:
   i = i++         /* A common beginner's error: i left unchanged,
                      since i++ means return i, then increment! */

   i = 7;
   a[i] = i++;     /* Does this store in to a[7] or a[8]?
                      It's implementation-dependent! */

   /* A beautiful example of the subtlety of --: */
   int fact( int n ) {
      if (n == 0)
         return 1;
      else
         return n * fact(n--);
         // Infinite loop!  Changing to "--n" would terminate,
         // but the result is still implementation dependent:
         // compilers are free to evaluate "fact(--n)" before "n"
      }

Back to Lab 13
Back to Comp 210 Home