5 ms·
clang gives 7 9 9 7 2 2 as well. gcc gives 7 10 9 7 3 2 when targeting arm. 2,3,7 are all valid possibilities for the 4th and 6th numbers, but apparently gcc i
by yuvi 17y ago
clang gives 7 9 9 7 2 2 as well.
gcc gives 7 10 9 7 3 2 when targeting arm.
2,3,7 are all valid possibilities for the 4th and 6th numbers, but apparently gcc interprets the 5th as 2 + 1 somehow, which I can't see as being valid whatsoever.
- ars 17y agoWhy not? It's doing the x=2 first (before anything else in there), then the ++ of the last expression (and returning 2 for it), then giving you the result of the middle one (which is now three since it ran after the last one). The 7 is because it ran that one before doing anything to the other values. It should be noted that this is illegal C, and compilers are allowed to do absolutely anything they want when they see it. Even things that are totally not logical.
- yuvi 17y agoI re-read C99, and the reason this is allowed is not because of §6.5.2.2 p10, but rather §6.5.16 p4 as far as I can tell. Though it's weird to me that the expression (x = 2) can ever be evaluated as anything but 2. Also, it's not illegal C, it's undefined C and compilers are only allowed to do whatever they want within the undefined area (which is larger than I expected.)
- ars 17y agoUndefined C is illegal C, because the compiler is not even required to do the same thing from run to run. Undefined is not the same as implementation defined.
- JoeAltmaier 17y agoNormally C compilers push arguments right-to-left, so probably evaluate them that way too. Still can't explain x=2 coming out 3 tho...