这个逻辑是如何工作的: k+=(x=5,y=x+2,z=x+y) ;

How does this logic work: k+=(x=5,y=x+2,z=x+y) ;

这个逻辑是如何工作的: k+=(x=5,y=x+2,z=x+y);

How it will give result k == 22. When I initialize the value of k = 10

#include <stdio.h>
int main()
{
    int x,y,z,k=10;
    k+=(x=5,y=x+2,z=x+y);
    printf("value of k %d ",k); //why it will show value of k =22
    return 0;
}

在赋值语句的右侧使用了逗号运算符(从左到右连续两次)。它的值是最后一个操作数的值。所以这个声明

k+=(x=5,y=x+2,z=x+y);

为了清楚起见,可以像这样重写

k+=( ( x = 5, y = x + 2 ), z = x + y );

其实相当于下面这组语句

x = 5;      // x is set to 5
y = x + 2;  // y is set to 7
z = x + y;  // z is set to 12
k += z;     // k is set to 22

来自 C 标准(6.5.17 逗号运算符)

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.