I'm confused about direct assignment and ternary conditional operators precedence:
#include<stdio.h>
int main(void)
{
int j, k;
j = k = 0;
(1 ? j : k) = 1; // first
printf("%d %d
", j, k);
j = k = 0;
1 ? j : k = 1; // second
printf("%d %d
", j, k);
return 0;
}
I would expect the output to be:
1 0
1 0
But it happens to be:
1 0
0 0
Plus I get this warning:
main.cpp:20: warning: statement has no effect
which is about the line I commented as second.
Since the direct assignment operator has less precedence than the ternary conditional operator, I was expecting lines commented as first and second to be equivalent. But alas it is not the case.
I tried this with g++ --version (Ubuntu 4.4.3-4ubuntu5) 4.4.3
See Question&Answers more detail:os