我期待在我的以下代码中:
#include<stdio.h>
int main(){
int i = 10;
int j = 10;
j = ++(i | i);
printf("%d %d\n", j, i);
j = ++(i & i);
printf("%d %d\n", j, i);
return 1;
}
表达式j = ++(i | i);
and j = ++(i & i);
将产生如下左值错误:
x.c: In function ‘main’:
x.c:6: error: lvalue required as increment operand
x.c:9: error: lvalue required as increment operand
但令我惊讶的是上面的代码编译成功了,如下:
~$ gcc x.c -Wall
~$ ./a.out
11 11
12 12
Check 上面的代码工作 http://codepad.org/3zJE0DuX正确。
而其他运算符会产生错误(据我了解)。偶数位运算符 XOR 会导致错误j = ++(i ^ i);
(检查其他运算符产生左值错误 http://codepad.org/g08e8GkH在编译时)。
是什么原因?这是未指定还是未定义?或按位或与运算符不同?
编译器版本:
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
在 c99 模式下,该编译器不会出现错误:gcc x.c -Wall -std=c99
.