三元运算符错误 [英] Ternary operator error
本文介绍了三元运算符错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#include< stdio.h>
int main()
{
int b,i = 5;
i == 5?b = 2:b = 4;
printf(%d,b);
返回0;
}
语法错误:需要左值
我尝试过:
使用括号(b = 2)和& (B = 4)。我在互联网上找到了答案。但我不知道原因。请告诉我它的原因。
谢谢。
#include <stdio.h>
int main()
{
int b,i=5;
i==5?b=2:b=4;
printf ("%d",b);
return 0;
}
Syntax error: lvalue required
What I have tried:
The issue gets resolved on using brackets (b=2) & (b=4). I found the answer on internet. But i don't know the reason. Please tell me the reason for it.
Thanks.
推荐答案
运算符优先级是问题:
Operator precedence is the problem:
i == 5 ? (b = 2) : (b = 4);
但是写的非常糟糕,几乎没有任何用处。
相反,试试这个:
But that is very badly written, and does little of any use.
Instead, try this:
b = i == 5 ? 2 : 4;
可以更容易地做同样的事情!
which does the same thing by a lot more readably!
这篇关于三元运算符错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文