一个易犯的条件运算符错误
#include<stdio.h>
int main() {
int a=1,b=0;
a=1?a=3:b=4;
printf("%d,%d\n",a,b);
return 0;
}
~/myprotects$ gcc hello.c -o hello
hello.c: 在函数‘main’中:
hello.c:5:12: 错误: 赋值运算的左操作数必须是左值份额符
先算a=1?:b,然后将b赋值
hello.c: 在函数‘main’中:
hello.c:5:12: 错误: 赋值运算的左操作数必须是左值份额符
分析:
因为?:运算符优先于赋值运算符,
hello.c:5:12: 错误: 赋值运算的左操作数必须是左值先算a=1?:b,然后将b赋值
浙公网安备 33010602011771号