Assignment of a parenthesized expression

Pau*_*l92 1 c expression

Consider the following snippet:

int a, b, c;
a = (b = 3, c = 4, 5, 6);
Run Code Online (Sandbox Code Playgroud)

It turns out that, after those lines are executed, b has the value 3, c has the value 4. Nothing unexpected so far. But a has value 6. Why is that?

Also, does this have an useful use?

Iha*_*imi 6

因为,运算符会丢弃左边的所有操作数,并且因为6它是最右边的操作数,所以它是唯一不被丢弃的操作数.

这是§6.5.17 n1570草案

  1. 逗号运算符的左操作数被计算为void表达式; 它的评估与右操作数之间存在一个序列点.然后评估右操作数; 结果有它的类型和价值.

  2. 示例如语法所示,逗号运算符(如本子条款中所述)不能出现在使用逗号分隔列表中的项目的上下文中(例如函数的参数或初始化程序列表).另一方面,它可以在括号内表达式中使用,也可以在这种上下文中的条件运算符的第二个表达式中使用.在函数调用中

    f(a, (t=3, t+2), c)
    
    Run Code Online (Sandbox Code Playgroud)

    该函数有三个参数,第二个参数的值为5.

你可以在这里阅读更多