问题与条件运算符

use*_*871 3 c gcc ternary-operator

#include<stdio.h>
int main()
{
  printf("%d\n", 4 ?: 8);
}
Run Code Online (Sandbox Code Playgroud)

根据C标准,这个程序是无效的,因为它缺少?:.之间的表达式.但有趣的是,当我编译它正在打印的代码时.如果4它会打印4而不是显示任何编译错误

Yu *_*Hao 6

这是一个gcc扩展.

x ? : y
Run Code Online (Sandbox Code Playgroud)

相当于

x ? x : y
Run Code Online (Sandbox Code Playgroud)

详情请见此处.

  • +1也用于链接gcc文档. (2认同)