为什么这个程序的输出是 4?
Why output of this program is 4?
#include<stdio.h>
int main(void) {
int i=(1,2,3,4);
printf("%d",i);
return 0;
}
当我进行在线技术模拟测试时,我想到了这个问题。首先,我认为 int i=(1,2,3,4);
行存在编译错误,但我错了。不知道为什么这道题的输出是4
.
此处,(1,2,3,4);
是一个表达式序列,由 commas
分隔,计算结果为最后一个表达式。
C11 §6.5.17 逗号运算符:
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.
() 的优先级高于 = 并且 () 的结果是最后一个元素,在您的示例中为 4。比=操作将完成
#include<stdio.h>
int main(void) {
int i=(1,2,3,4);
printf("%d",i);
return 0;
}
当我进行在线技术模拟测试时,我想到了这个问题。首先,我认为 int i=(1,2,3,4);
行存在编译错误,但我错了。不知道为什么这道题的输出是4
.
此处,(1,2,3,4);
是一个表达式序列,由 commas
分隔,计算结果为最后一个表达式。
C11 §6.5.17 逗号运算符:
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.
() 的优先级高于 = 并且 () 的结果是最后一个元素,在您的示例中为 4。比=操作将完成