马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
为什么这程序运行后的结果是1,而不是7呢?
#include <stdio.h>
int main(void)
{
int a = 1;
printf("%d\n", (a + 5, a++));
return 0;
}
参考标准
6.3.2.2 void
1 The (nonexistent) value of a void expression (an expression that has type void) shall not be used in any way, and implicit or explicit conversions (except to void) shall not be applied to such an expression. If an expression of any other type is evaluated as a void expression, its value or designator is discarded. (A void expression is evaluated for its side effects.)
6.5.17 Comma operator
...
2 The left operand of a comma operator is evaluated as a void expression; there is a sequence point between its evaluation and that of the right operand. Then the right operand is evaluated; the result has its type and value.
简单的说,逗号分隔的表达式从左向右进行求值,逗号运算符处有一个顺序点(注意过之前的讨论,相信您了解顺序点的含义)。逗号左侧求得的值被丢弃,仅有其副作用生效,整个表达式的结果为右侧求得的值和类型。
此处 a + 5 在左侧,没有副作用且求值结果被丢弃;随后完成 a++ 的求值,整个表达式的值为 1 。
|