C语言 表达式中的问号运算符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14803037/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:19:05  来源:igfitidea点击:

question mark operator in expressions

coperators

提问by d0rmLife

K&R Second Edition(page 71)-- I must have missed the explanation:

K&R 第二版(第 71 页)——我一定错过了解释:

sign = (s[i] == '-') ? -1 : 1;

The context of this is a function that converts a string to a double. This part in particular comes after the function skips white space. I infer it ischecking for positive or negative value, and saving it as either -1 or +1 for sign conversion at the end of the function... return sign * val /power;

this 的上下文是一个将字符串转换为双精度值的函数。这部分特别是在函数跳过空白之后。我推断它正在检查正值或负值,并将其保存为 -1 或 +1 以在函数结束时进行符号转换...return sign * val /power;

I would like to do better than infer...I'm particularly unsure of what the ?and : 1are doing here (or anywhere, for that matter).

我想做得比推断更好......我特别不确定这里(或任何地方,就此而言)?: 1正在做什么。

It kind of seems like an abstract ifstatement. Where ?checks for truth and :is else... is that so? Is it limited to if/else?

这有点像一个抽象的if陈述。在哪里?检查真相,:else...是吗?是否仅限于if/else

I am a beginner and I haven't come across this expression syntax before, so I am wondering if there is a particular reason it seems to often be replaced by a formal if/else--besides, perhaps, readability?

我是一个初学者,我以前没有遇到过这种表达式语法,所以我想知道是否有特殊原因它似乎经常被正式的——if/else除了可读性之外?

回答by

It kind of seems like an abstract if statement, where ?checks for truth and :is else... is that so?

它有点像一个抽象的 if 语句,在那里?检查真理,:而 else ... 是这样吗?

Yeah, almost. It's called the "conditional operator" (sometimes not entirely accurately referred to as "the ternary operator", since it's the only ternary operator in C). It's not a statementthough, it's an expression,it has a value. It evaluates to its second argument if the first argument evaluates to true, and to its third argument if it's false. Therefore

是的,差不多。它被称为“条件运算符”(有时不完全准确地称为“三元运算符”,因为它是 C 中唯一的三元运算符)。不过,这不是一个陈述,而是一个表达式,它具有价值。如果第一个参数的计算结果为真,则计算为第二个参数,如果为假,则计算为第三个参数。所以

sign = (s[i] == '-') ? -1 : 1;

is equivalent to

相当于

if (s[i] == '-') {
    sign = -1;
} else {
    sign = 1;
}

回答by Pubby

It kind of seems like an abstract if statement.

这似乎是一个抽象的 if 语句。

That's correct. This is called a "ternary conditional operator".

没错。这称为“三元条件运算符”。

The normal ifworks on statements, while the conditional operator works on expressions.

正常if适用于语句,而条件运算符适用于表达式



I am wondering if there is a particular reason it seems to often be replaced by a formal if/else--besides, perhaps, readability?

我想知道是否有一个特殊的原因,它似乎经常被正式的 if/else 所取代——除了可读性之外?

There are cases where branching on statements is not enough, and you need to work on the expression level.

在某些情况下,对语句进行分支是不够的,您需要在表达式级别上工作。

For instance, consider initialization:

例如,考虑初始化:

const int foo = bar ? 5 : 3;

This could not be written using a normal if/else.

这不能使用普通的if/编写else



Anyway, people who are saying it's equivalent to the if/elseare being imprecise. While the generated assembly is usually the same, they are not equivalent and it should not be seen as a shorthand version of if. Simply put, use ifwhenever possible, and only use the conditional operator when you need to branch on expressions.

无论如何,说它等同于if/ 的else人是不精确的。虽然生成的程序集通常相同,但它们并不等效,不应将其视为if. 简而言之,if尽可能使用,并且仅在需要对表达式进行分支时使用条件运算符。

回答by d0rmLife

This is the ternary operator. (s[i] == '-') ? -1 : 1;returns -1if s[i] == '-'and 1otherwise. This value is then assigned to sign. In other words, a longer way to write this would be:

这是三元运算符。如果和否则(s[i] == '-') ? -1 : 1;返回。然后将该值分配给。换句话说,更长的写法是:-1s[i] == '-'1sign

int sign;

if(s[i] == '-')
{
  sign = -1;
}
else
{
  sign = 1;
}

回答by ouah

?:is the conditional operatorin C.

?:是C 中的条件运算符

In your example it would produce the same result as this ifstatement:

在您的示例中,它将产生与此if语句相同的结果:

if (s[i] == '-')
{
    sign = -1;
}
else
{
    sign = 1;
}

回答by Inisheer

sign = (s[i] == '-') ? -1 : 1;

is shorthand for:

是简写:

if (s[i] == '-')
{
    sign = -1;
}
else
{
    sign = 1;
}