CCoding Style - Be careful with side effects2

From EdWiki

Revision as of 09:30, 23 June 2015 by Jshankar (Talk | contribs) (1 revision imported: EdWiki -- Emsys pages from Shukra)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Be careful with side effects ...

str[i++] = str[i++] = ' ';

The intent is to store blanks at the next two positions in str. But depending on when i is updated, a position in str could be skipped and i might end up increased only by 1.

Break it into two statements:

str[i++] = ' ';
str[i++] = ' ';

Even though it contains only one increment, this assignment can also give varying results:

array[i++] = i;