I know this looks familiar but it is brought to me as a problem in a test by Microsoft to recruit interns. It seems to me that y=++y
is not standard compliant, but I think maybe it would be better to be sure (not sure that I'm better than those who write up these tests at MS). So I'm asking your advice. Do you think expressions like this is standard-compliant and does not result in undefined behaviour?
#include <stdio.h>
int main(){
int a = 10;
int b = 10;
a = ++a; //What ???
b = b++; //What ???
printf("%d %d\n",a,b);
return 0;
}
gcc
complains about it when used to compile with -Wsequence-point
. (It is not explicitly stated whether it is a C or C++ specific problem.)
But only four answers provided:
a) 10 10
b) 11 10
c) 10 11
d) 11 11
Although one is not restricted to select only one answer ( so maybe I should choose all four? )
Well, in my opinion, between self-incrementing and assignment there is no sequence point. So this violates the specification. Doesn't it?