Department of Engineering

IT Services


C veterans use abbreviated forms for expressions. Some are natural and widely adopted, others merely lead to obscurity - even if they produce faster code (and often they don't) they waste future programmers' time.

  • i++ is equivalent to i=i+1. This (and the i- decrementing operator) is a common contraction. The operation can be done after the variable is used, or (by using -i, ++i) before, so
    i = 4;
    printf("i = %d\n", i++)
    i = 4;
    printf("i = %d\n", ++i)
    will both leave i as 5, but in the 1st fragment 4 will be printed out while in the 2nd 5 will.

  • i+=6 is equivalent to i=i+6. This style of contraction isn't so common, but can be used with most of the binary operators.

  • Assignment statements have a value - the final value of the left-hand-side - so j = (i=3+4) will set i then j to 7, and i = j = k = 0 will set k, then j, then i to zero. This feature should be used with caution.

  • The `,' operator is used between 2 expressions if the value of the 1st expression can be ignored. It's a way to put 2 or more statements where normally only one would go. E.g.
      for(init(3),i=0,j+0; i<100; i++,j++)
    This feature is often over-used too.

  • Expressions with comparison operators return 1 if the comparison is true, 0 if false, so while(i!=0) and while(i) are equivalent.

  • The `if (cond) exp1; else exp2;' construction can be abbreviated using `(cond)?exp1:exp2'. The following fragments are equivalent.
    if (a==6)

    This notation should be used with discretion.