C 中的变量定义

发布于 2024-11-25 01:40:10 字数 142 浏览 4 评论 0原文

下面的声明在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

捂风挽笑 2024-12-02 01:40:10

您正在看到逗号运算符在起作用。逗号运算符 a,b 计算 a,丢弃结果,然后返回 b

由于 1023 没有副作用,因此这相当于 char a = 21;

You are seeing the comma operator at work. The comma operator a,b evaluates a, throws away the result, then returns b.

Since 10 and 23 have no side effects, this is equivalent to char a = 21;

无悔心 2024-12-02 01:40:10

这是标量逗号运算符的使用。逗号运算符计算左侧的每个表达式并丢弃返回值,最终返回最右侧的值。

在这种情况下,它是没有用的;然而,如果你将它与有副作用的表达式一起使用,那么它就会产生真正的效果。

半“有用”表达式(有副作用)的示例:

int a = 10;
int is_a_odd_after_increment = ++a, a % 2;

第一个表达式 (++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):

int a = 10;
int is_a_odd_after_increment = ++a, a % 2;

The first expression (++a) has a clear side-effect, and it is evaluated first (before the a % 2). The second expression is the expression that is yielded into the assignment.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文