I recently came across a question about sequence points in C++ at this site, about
what this code will output:
int c=0;
cout << c++ << c;
It was answered that the output is undefined and << is not a sequence point, but still I want to know why is it undefined when, even if I compile it 25 times, it still always prints 01?
“Undefined” means that the standard doesn’t specify what has to happen in that situation, so anything your compiler does is, by definition, right. If it always prints
01, that’s fine. If it prints a different number every time you run, that would be fine too. If it causes monkeys to fly out of your nose (as illustrated here), that would be fine as well.You might not think so, but the compiler writers are off the hook if it happens.
[Edit: It has been pointed out in the comments that the cannonical reference is “nasal demons”, not “nasal monkeys”. My apologies for any unintended confusion. Any intended confusion I’m proud of and do not apologize for. 🙂 ]