Does this C code produce "undefined behavior"?
I'm reading an int开发者_JS百科eresting article A Guide to Undefined Behavior in C and C++, Part 1 on undefined behavior in C and C++. Often I do the following in my code:
int i = 10;
i = (++i) % 7;
Does this produce undefined behavior? On x86? ARM? Perhaps it depends on the compiler?
It's undefined behavior because i
is modified more than once without an intervening sequence point.
It depends on the compiler only in the sense that there are no requirements about what the code will do, so every compiler can do something different. To be clear - just because even though you get results that seem to make sense (sometimes), the code is a bug.
Yes - as per standard ISO C.
- Is this undefined C behaviour?
- What are all the common undefined behaviours that a C++ programmer should know about?
Though, a compiler is expected to produce consistent result.
精彩评论