Avatar ·

What are sequence points and how are they related to undefined behavior in C++?

Hello everyone! I'm a beginner C++ developer and recently encountered the concept of "sequence points". On many forums, they write that violating sequence point rules leads to undefined behavior, but I don't quite understand what this means in practice.\n\nHere's a simple example that confuses me:\n\n```cpp\nint i = 0;\nint result = i++ + i++;\n```\n\nI read somewhere that such code causes undefined behavior. But why? At first glance, it seems the result would be predictable: first the old value of `i` (0) is taken, then it's incremented, and so on twice. I would expect `result` to become `0 + 1 = 1`, and `i` to become `2`. But the compiler says this is UB.\n\nAnother example:\n\n```cpp\nint arr[5] = {0};\nint i = 0;\narr[i] = i++;\n```