?: 运算符在 C 中究竟是如何工作的?
How exactly does the ?: operator work in C?
我有一个问题,编译器如何对以下代码进行操作:
#include<stdio.h>
int main(void)
{
int b=12, c=11;
int d = (b == c++) ? (c+1) : (c-1);
printf("d = %i\n", d);
}
我不知道为什么结果是d = 11
。
因为条件为假,所以会发生 false
情况:c-1
,但是由于您在条件中将 c
增加了 c++
,因此 c
现在是 12
。因此结果 12 - 1 即 11.
编辑:
OP 误解的是 post 增量。
所以实际情况是这样的:
#include<stdio.h>
int main(void)
{
int b=12, c=11;
int d;
if (b == c) { // 12 == 11 ? -> false
c = c + 1;
d = c + 1;
} else { // this executes since condition is false
c = c + 1; // post increment -> c++ -> c = 12 now
d = c - 1; // 12 - 1 = 11 -> d = 11
}
printf("d = %i\n", d);
}
转换为常规 if 语句后,您的代码将如下所示:
int b=12, c=11;
int d;
if (b == c++)
d = c+1;
else
d = c-1;
这里的线索是 c 在条件检查后递增。所以你进入 else
状态但是 c 已经有值 12 了。
语法
condition ? value_if_true : value_if_false
所以,你写了
int d = (b == c++) ? (c+1) : (c-1);
在这种情况下,结果将是 11,因为在 if 检查之后,'c' 值增加(c+1=12),并且仅在此之后它才将 'd' 值设置为 c( 12)-1 即 11.
如果你用过,例如:
int d = (b == ++c) ? (c+1) : (c-1);
"c" 值会在检查语句之前增加,所以它会是真的并且 "d" 值会是 c(12)+1 即 13。
根据 C 标准(6.5.15 条件运算符)
4 The first operand is evaluated; there is a sequence point between
its evaluation and the evaluation of the second or third operand
(whichever is evaluated). The second operand is evaluated only if the
first compares unequal to 0; the third operand is evaluated only if
the first compares equal to 0; the result is the value of the second
or third operand (whichever is evaluated), converted to the type
described below.110)
所以在这个声明的初始化表达式中
int d = (b == c++) ? (c+1) : (c-1);
变量 b
与变量 c
的值进行比较,因为 post 递增运算符 returns 其操作数在递增之前的值。
由于值彼此不相等(b
设置为 12 而 c
设置为 11),因此计算子表达式 (c-1)
。
根据引用,在评估运算符的条件后有一个序列点。这意味着在对变量 c
应用 post 递增运算符后,条件 c
的计算结果为 12
。结果,变量 d 由值 1
(12 - 1
).
初始化
在int d = (b == c++) ? (c+1) : (c-1);
中:
c++
的值为c
的当前值,11。另外,c
递增为12。
b == 11
是错误的,因为 b
是 12.
- 由于
(b == c++)
为假,所以使用(c-1)
。另外,c
到12的递增必须到此为止。
- 因为
c
是12,所以c-1
是11。
d
被初始化为那个值,11.
我有一个问题,编译器如何对以下代码进行操作:
#include<stdio.h>
int main(void)
{
int b=12, c=11;
int d = (b == c++) ? (c+1) : (c-1);
printf("d = %i\n", d);
}
我不知道为什么结果是d = 11
。
因为条件为假,所以会发生 false
情况:c-1
,但是由于您在条件中将 c
增加了 c++
,因此 c
现在是 12
。因此结果 12 - 1 即 11.
编辑: OP 误解的是 post 增量。
所以实际情况是这样的:
#include<stdio.h>
int main(void)
{
int b=12, c=11;
int d;
if (b == c) { // 12 == 11 ? -> false
c = c + 1;
d = c + 1;
} else { // this executes since condition is false
c = c + 1; // post increment -> c++ -> c = 12 now
d = c - 1; // 12 - 1 = 11 -> d = 11
}
printf("d = %i\n", d);
}
转换为常规 if 语句后,您的代码将如下所示:
int b=12, c=11;
int d;
if (b == c++)
d = c+1;
else
d = c-1;
这里的线索是 c 在条件检查后递增。所以你进入 else
状态但是 c 已经有值 12 了。
语法
condition ? value_if_true : value_if_false
所以,你写了
int d = (b == c++) ? (c+1) : (c-1);
在这种情况下,结果将是 11,因为在 if 检查之后,'c' 值增加(c+1=12),并且仅在此之后它才将 'd' 值设置为 c( 12)-1 即 11.
如果你用过,例如:
int d = (b == ++c) ? (c+1) : (c-1);
"c" 值会在检查语句之前增加,所以它会是真的并且 "d" 值会是 c(12)+1 即 13。
根据 C 标准(6.5.15 条件运算符)
4 The first operand is evaluated; there is a sequence point between its evaluation and the evaluation of the second or third operand (whichever is evaluated). The second operand is evaluated only if the first compares unequal to 0; the third operand is evaluated only if the first compares equal to 0; the result is the value of the second or third operand (whichever is evaluated), converted to the type described below.110)
所以在这个声明的初始化表达式中
int d = (b == c++) ? (c+1) : (c-1);
变量 b
与变量 c
的值进行比较,因为 post 递增运算符 returns 其操作数在递增之前的值。
由于值彼此不相等(b
设置为 12 而 c
设置为 11),因此计算子表达式 (c-1)
。
根据引用,在评估运算符的条件后有一个序列点。这意味着在对变量 c
应用 post 递增运算符后,条件 c
的计算结果为 12
。结果,变量 d 由值 1
(12 - 1
).
在int d = (b == c++) ? (c+1) : (c-1);
中:
c++
的值为c
的当前值,11。另外,c
递增为12。b == 11
是错误的,因为b
是 12.- 由于
(b == c++)
为假,所以使用(c-1)
。另外,c
到12的递增必须到此为止。 - 因为
c
是12,所以c-1
是11。 d
被初始化为那个值,11.