使用逗号运算符的意外输出

kap*_*pil -1 c if-statement

我写了一个程序

#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;
}
Run Code Online (Sandbox Code Playgroud)

我期待输出为"未找到数字",但它是"在列表中找到的数字"为什么会这样

cad*_*luk 5

==运营商的优先级高于,,因此,如果子句计算结果为

if (((x) == 1),2,4,5,6)
Run Code Online (Sandbox Code Playgroud)

由于逗号运算符"count"(6)的最后一个"元素",它始终为true .

从C11标准:

逗号运算符的左操作数被计算为void 表达式[...].然后评估右操作数; 结果有它的类型和价值.