参见英文答案 >
What does the question mark and the colon (?: ternary operator) mean in objective-c?13个
我听说过哪种if语句使用?并且:在C中
我不知道如何使用它,我无法找到任何相关的东西.
我需要使用它来缩短我的代码
任何帮助,将不胜感激.
我听说过哪种if语句使用?并且:在C中
我不知道如何使用它,我无法找到任何相关的东西.
我需要使用它来缩短我的代码
任何帮助,将不胜感激.
解决方法
?:在C中是
ternary operator(也称为条件运算符).你可以缩短你的代码
if(condition) expr1; else expr2;
至
condition ? expr1 : expr2;
看看它怎么运作:
C11:6.5.15条件运算符:
The first operand is evaluated; there is a sequence point between its evaluation and the
evaluation of the second or third operand (whichever is evaluated). The second operand
is evaluated only if the first compares unequal to0
; the third operand is evaluated only if the first compares equal to0
; the result is the value of the second or third operand (whichever is evaluated),