I've been working on embedded C project, and I found code:
x = ++x % 5;
Now, first of all there are 2 side effect operators on variable x in one expression, assignment and prefix increment operator.
According to C99 standard (ISO/IEC 9899:TC3):
Section 6.5 Expressions
- Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be read only to determine the value to be stored.
this should be considered as undefined behavior, but I haven't succeeded to prove that in practice. Tried few compilers on windows (mingw32-gcc, msvc) and on linux:
gcc version 7.3.0 (Ubuntu 7.3.0-27ubuntu1~18.04)
Ubuntu 18.04 4.15.0-36-generic
So my question is, is this considered undefined behavior in embedded, and is it safe to use this in embedded?