一个易犯的条件运算符错误

#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: 错误: 赋值运算的左操作数必须是左值份额符
 
分析:
 因为?:运算符优先于赋值运算符,
hello.c:5:12: 错误: 赋值运算的左操作数必须是左值
先算a=1?:b,然后将b赋值
posted @ 2013-03-29 08:38  源景  阅读(197)  评论(0)    收藏  举报