Practice mode C Language

Why is the C statement i = i++ + 1; a problem?

Correct answer C. It modifies i twice without sequencing, which is undefined behaviour

Explanation

The assignment and the side effect of i++ both modify i, and C11 does not sequence the side effect of i++ relative to the assignment's store. Two unsequenced modifications of the same object are undefined behaviour, so different compilers may give different results. GCC warns with -Wsequence-point under -Wall. Write i = i + 1 or i++ alone instead.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.