我有以下内容:
int a = 1, b = 2, c = 3, d = 4;
a = b, c = d;
printf("%d, %d, %d, %d", a, b, c, d);
输出是:
2, 2, 4, 4
逗号运算符如何与赋值运算符一起使用?据我所知,如果我们有,它将评估并返回第二个表达式,
(exp1, exp2)
那么,它为什么要评估 a = b
?
最佳答案
计算第一个操作数并丢弃结果。然后计算第二个操作数,并将结果作为表达式的总结果返回。
标准说:
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.
关于C 中的逗号和赋值运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13863196/