As the title says I found such a sentence in some C lecture notes.
I can’t invent any example proving that sentence.
In my opinion every of assignment operations is evaluated once, because when we want it to be evaluated more than once we put in in a loop. What am I missing then?
I’ve searched but couldn’t find an answer here on SO.
C says:
Below are some examples of why it matters:
Example 1:
is the same as:
because the left operand of
+=is evaluated once.If it was not evaluated once it would be the same as:
which is of course different (and undefined behavior BTW).
Example 2:
assuming
foohere returns a pointer to an object of a scalar type and produces side effects (for example it prints a string on the terminal). With the compound assignment operator it will print the string only once and not two times.Example 3:assuming
REGhere is an IO register (something like#define REG (*(volatile uint8_t *) 0x42)) and that every read to this specific IO register triggers a hardware event. The register will be read only once with the compound assignment operator and not two times.EDIT: following @R. comment I striked the example 3. I think most compilers do not perform a read in this expression:
REG = 31or two reads with this expression:REG = REG | 0x01.