简体   繁体   English

C 中的变量定义

[英]Variable definition in C

What does following declaration mean in C? C 中的以下声明是什么意思?

char a = (10,23,21);

While printing the value of "a" with "%u" the output is 21. gcc is not giving any error.在使用“%u”打印“a”的值时,output 为gcc没有给出任何错误。 What's this kinda declaration and what's the use of it?这是什么声明,它有什么用?

You are seeing the comma operator at work.您正在看到逗号运算符在工作。 The comma operator a,b evaluates a , throws away the result, then returns b .逗号运算符a,b计算a ,丢弃结果,然后返回b

Since 10 and 23 have no side effects, this is equivalent to char a = 21;由于1023没有副作用,这相当于char a = 21;

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 ).第一个表达式 ( ++a ) 有一个明显的副作用,它首先被评估(在a % 2之前)。 The second expression is the expression that is yielded into the assignment.第二个表达式是在赋值中产生的表达式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM