I was expecting that in my following code:
#include<stdio.h>
int main(){
int i = 10;
int j = 10;
j = ++(i | i);
printf("%d %d
", j, i);
j = ++(i & i);
printf("%d %d
", j, i);
return 1;
}
expressions j = ++(i | i);
and j = ++(i & i);
will produce lvalue errors as below:
x.c: In function ‘main’:
x.c:6: error: lvalue required as increment operand
x.c:9: error: lvalue required as increment operand
But I surprised that above code compiled successfully, as below:
~$ gcc x.c -Wall
~$ ./a.out
11 11
12 12
Check the above code working correctly.
While other operators produce error (as I understand). Even bitwise operator XOR causes of an error j = ++(i ^ i);
(check other operators produce an lvalue error at compilation time).
What is the reason? Is this is unspecified or undefined ? or bitwise OR AND operators are different?
compiler version:
gcc version 4.4.5 (Ubuntu/Linaro 4.4.4-14ubuntu5)
But I believe compiler version shouldn't reason for non-uniform behavior. If ^
not compiled then |
and &
also not. otherwise should work for all
Its not an error with this compiler in c99 mode: gcc x.c -Wall -std=c99
.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…