我写了一个程序
#include<stdio.h>
int main()
{
int x=3;
if((x)==1,2,4,5,6)
printf("number found in the list\n");
else
printf("Number not found \n");
return 0;
}
我期望输出是“未找到号码”,但它是“在列表中找到号码”,为什么会这样
最佳答案
==
运算符的优先级高于 ,
,因此 if 子句的计算结果为
if (((x) == 1),2,4,5,6)
自从逗号运算符的最后一个“元素”“计数”(6
) 以来,这始终为真。
来自 C11 标准:
The left operand of a comma operator is evaluated as a
void
expression [...]. Then the right operand is evaluated; the result has its type and value.
关于c - 使用逗号运算符的意外输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32975449/