C语言 有什么作用?在 C 中是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4885143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does ? in C mean?
提问by Eric Brotto
What does a question mark (?) in C mean?
C 中的问号 (?) 是什么意思?
回答by Didier Trosset
?is the first symbol of the ?:ternary operator.
?是?:三元运算符的第一个符号。
a = (b==0) ? 1 : 0;
awill have the value 1 if bis equal to 0, and 0 otherwise.
a如果b等于0,则值为 1 ,否则为0。
回答by Benoit
Additionally to other answers, ?can be part of a trigraph.
除了其他答案,?可以是trigraph 的一部分。
回答by Javed Akram
This is a ternary Operatorwhich is conditional operator uses like if-else
这是一个三元运算符,它是条件运算符,如if-else
example
例子
int i=1;
int j=2;
int k;
k= i > j ? i : j;
//which is same as
if(i>j)
k=i;
else
k=j;
Usage:Syntax of ?:is
用法:?: 的语法是
assignment_Variable = Condition ? value_if_true : value_if_false;
回答by zoul
That's probably a part of the ternary operator:
这可能是三元运算符的一部分:
const int numApples = …;
printf("I have %i apple%s.\n", numApples == 1 ? "" : "s");
回答by Stefan Papp
This is a so called conditional operator. You can shorten your if else statement with this operator.
这就是所谓的条件运算符。您可以使用此运算符缩短 if else 语句。
The following link should explain everything
以下链接应该解释一切
http://www.crasseux.com/books/ctutorial/The-question-mark-operator.html
http://www.crasseux.com/books/ctutorial/The-question-mark-operator.html
回答by ckv
It is a conditional operator. For example refer the below link http://en.wikipedia.org/wiki/Conditional_operator
它是一个条件运算符。例如参考以下链接 http://en.wikipedia.org/wiki/Conditional_operator
回答by ismail
回答by bpm
Most likely the '?' is the ternary operator. Its grammar is:
最有可能的“?” 是三元运算符。它的语法是:
RESULT = (COND) ? (STATEMEN IF TRUE) : (STATEMENT IF FALSE)
It is a nice shorthand for the typical if-else statement:
这是典型的 if-else 语句的一个很好的简写:
if (COND) {
RESULT = (STATEMENT IF TRUE);
} else {
RESULT = (STATEMENT IF FALSE);
as it can usually fit on one line and can improve readability.
因为它通常可以放在一行上并且可以提高可读性。
Some answers here refer to a trigraph, which is relevant to the C preprocessor. Take a look at this really dumb program, trigraphs.c:
这里的一些答案是指与 C 预处理器相关的三合字母。看看这个非常愚蠢的程序,trigraphs.c:
# /* preprocessor will remove single hash symbols and this comment */
int main()
{
char *t = "??=";
char *p = "??/"";
char *s = "??'";
??(, ??), ??! ??<, ??>, ??-
return 0;
}
invoking only the c preprocessor by running gcc -E -trigraphs trigraph.cthe output is
通过运行gcc -E -trigraphs trigraph.c输出仅调用 c 预处理器是
int main()
{
char *t = "#"
char *p = "\"";
char *s = "^";
[, ], | {, }, ~
return 0;
}
Hopefully that clarifies a little bit what a trigraphs are, and what a '?' "means" in C.
希望这能澄清一点什么是三合字母,什么是“?” C中的“意思”。

