Post- and Pre-Operators in Complex Expressions

Writing a complex program results in complex code. In general it is the job of the compiler to optimize complex functions. Some rules may help the compiler to generate efficient code.

If the target does not support pre- or post-increment or pre- or post-decrement instructions, using the ++ and -- operators in complex expressions is not recommended. Post-increment and post-decrement particularly may result in additional code:

  a[i++] = b[--j];

  

Write the above statement as:

  j--; a[i] = b[j]; i++;

  

Use it in simple expressions as:

  i++;

  

Avoid assignments in parameter passing or side effects (as ++ and --). The evaluation order of parameters is undefined (ANSI-C standard 6.3.2.2) and may vary from Compiler to Compiler, and even from one release to another:

  i = 3;

  
  myfun(i++, --i);

  

In the above example, myfun() is called either with myfun (3,3) or with myfun(2,2).