Sequence Points (2010)
The article discusses the concept of sequence points in C programming and how they affect the behavior of code. It highlights examples where modifying a variable multiple times between sequence points leads to undefined behavior. The author references the C99 standard to explain the implications of such programming practices.
- ▪The output of certain C statements can vary between different compilers due to undefined behavior.
- ▪Modifying a variable more than once between two consecutive sequence points results in undefined behavior.
- ▪The C programming language does not specify the order of evaluation for operands, which can lead to different results.
Opening excerpt (first ~120 words) tap to expand
Sequence Points By Susam Pal on 26 May 2010 Code Examples A particular type of question comes up often in C programming forums. Here is an example of such a question: #include <stdio.h> int main() { int i = 5; printf("%d %d %d\n", i, i--, ++i); return 0; } On my system, the output was 5 6 5 with GCC and 6 6 6 with the C compiler that came with Microsoft Visual Studio. The versions of the compilers with which I got these results are: gcc (Debian 4.3.2-1.1) 4.3.2 Microsoft Visual Studio 2005 32-Bit C/C++ Optimizing Compiler Version 14.00.50727.42 for 80x86 Here is another example of such a question: #include <stdio.h> int main() { int a = 5; a += a++ + a++; printf("%d\n", a); return 0; } In this case, I got the output 17 with both these compiler versions.
…
Excerpt limited to ~120 words for fair-use compliance. The full article is at Susam.