tuc*_*uxi 4 c gcc ternary-operator conditional-operator
这似乎是在没有警告的情况下编译和运行gcc -Wall
(4.9.2)
#include <stdio.h>
int main(int argc, char **argv){
printf("%d\n", argc-1?:42);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果我运行它
argc-1
评价false
),它打印42
;argc-1
评估true
)时,它打印n-1.如果我认为x?:y
在这种情况下相当于x?1:y
?我是对的吗?这是标准的,预期的行为,还是只是GCC的怪癖?
这是条件运算符的简写版本的已知gcc扩展.
argc-1?:42
Run Code Online (Sandbox Code Playgroud)
就像是一样的
(argc-1)?(argc-1):42
Run Code Online (Sandbox Code Playgroud)
使用这种变化的原因是(和我引用)
"当它变得有用时是第一个操作数,或者可能(如果它是一个宏参数),包含副作用.然后在中间重复操作数将执行两次副作用.省略中间操作数使用该值已经计算时没有重新计算它的不良影响."
归档时间: |
|
查看次数: |
140 次 |
最近记录: |