在条件中做赋值有特殊的名称吗?这是我在 C 中询问的示例:
// Assume a and b have been previously defined and are compatible types
if( (a = b) ) { // <- What do you call that?
//do something
}
我和我的几个 friend 确信它有一个名字,其他一些人也同意有一个名字,但我们到处都找不到。这里有人听过这个词吗?
最佳答案
假设赋值是有意的,对此没有特殊的名称。 C 语言规范对 if
语句的控制表达式提出了非常弱的要求:
6.8.4.1-1: The controlling expression of an if statement shall have scalar type.
赋值表达式满足这个要求,只要a
和b
是标量。对这个赋值的结果执行与零的隐式比较:
6.8.4.1-2: In both forms, the first substatement is executed if the expression compares unequal to 0. In the else form, the second substatement is executed if the expression compares equal to 0.
请注意,编译器在看到这样的赋值时会发出警告,因为缺少第二个 =
是常见的错误来源。 You can prevent these warnings using parentheses, as described in this Q&A .
关于objective-c - 你怎么称呼在有条件的情况下做作业?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46816589/