C 中的逗号和赋值运算符

标签 c c99 comma-operator

我有以下内容:

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/

相关文章:

c++ - c/c++中用函数指针模仿OO,如何存储?

c++ - 在一行中删除多个指针。 C++

c++ - 使运算符重载上下文特定

c - 如何在不包含 stdio.h 的情况下使用 scanf()

c - 当 kv 超过 1 项时,kv 执行无限循环

ios - 在 C 中分配/释放结构指针的动态数组

c - 是否保证全局变量总是用c99初始化为0?

c - C 中的 VS 代码问题建议

c - 参数 ‘my 2D-arr’ 已初始化

c - C 中的逗号、圆括号和大括号之间的关系是什么?