4 ms·
I don’t have gcc available so I can’t test it, but I wonder what it does with int a = 5; int b = a++; if it gives b==5 in this circumstance (which I
by dhosek 5mo ago
I don’t have gcc available so I can’t test it, but I wonder what it does with
int a = 5;
int b = a++;
if it gives b==5 in this circumstance (which I would say is the correct value), then it seems that giving 13 for a++ + ++a is a bug in the compiler. I kind of feel like giving 6 as an answer would also be a bug in the compiler since postfix-++ should return the old value and then increment.
- deleted 5mo ago[deleted]
- compiler-guy 5mo agoThe trick here is that the original expression contains undefined behavior. Your example does not.
- _kst_ 5mo agoYour code: int a = 5; int b = a++; has well defined behavior. The first line initializes a to 5. The second initializes b to 5 and sets a to 6. (The language doesn't specify the order of the two operations of assigning a value to be and incrementing a, but in this case it doesn't matter.) Giving 13 for a++ + ++a is not a bug in the compiler. It's a bug in the code. The correct answer to "what does a++ + ++a do" is "it gets rejected in code review and replaced with code that expresses the actual intent.