C 中的变量定义
下面的声明在 C 中意味着什么?
char a = (10,23,21);
当用“%u”打印“a”的值时,输出是21。 gcc
没有给出任何错误。 这种声明是什么?它有什么用?
What does following declaration mean in C?
char a = (10,23,21);
While printing the value of "a" with "%u" the output is 21.gcc
is not giving any error.
What's this kinda declaration and what's the use of it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在看到逗号运算符在起作用。逗号运算符
a,b
计算a
,丢弃结果,然后返回b
。由于
10
和23
没有副作用,因此这相当于char a = 21;
You are seeing the comma operator at work. The comma operator
a,b
evaluatesa
, throws away the result, then returnsb
.Since
10
and23
have no side effects, this is equivalent tochar a = 21;
这是标量逗号运算符的使用。逗号运算符计算左侧的每个表达式并丢弃返回值,最终返回最右侧的值。
在这种情况下,它是没有用的;然而,如果你将它与有副作用的表达式一起使用,那么它就会产生真正的效果。
半“有用”表达式(有副作用)的示例:
第一个表达式 (
++a
) 具有明显的副作用,并且首先对其求值(在a 之前) %2
)。第二个表达式是生成到赋值中的表达式。This is a use of the scalar comma operator. The comma operator evaluates each expression on the left side and throws away the return value, finally returning the rightmost value.
In this case, it's useless; however, if you use it with expressions with side-effects, then it has a real effect.
Example of a semi-"useful" expression (with side-effects):
The first expression (
++a
) has a clear side-effect, and it is evaluated first (before thea % 2
). The second expression is the expression that is yielded into the assignment.