简体   繁体   English

逗号运算符和赋值运算符 - 返回值

[英]Comma operators and assignment operators - return values

以下代码段得到32的输出,我有点混淆为什么?

 int i=(j=4,k=8,l=16,m=32); printf(“%d”, i); 

Start reading inside the first set of parentheses. 开始在第一组括号内阅读。

The comma operator evaluates each of several expressions subsequently. 逗号运算符随后会计算几个表达式中的每一个。 It returns the return value of the last expression - in this case, it is 32, because the return value of an assignment is the value assigned. 它返回最后一个表达式的返回值 - 在本例中,它是32,因为赋值的返回值是赋值。

http://en.wikipedia.org/wiki/Comma_operator http://en.wikipedia.org/wiki/Comma_o​​perator

int i=(j=4,k=8,l=16,m=32); printf(“%d”, i); // Will give you 32
int i=(j=4,k=8,l=16); printf(“%d”, i); // Will give you 16
int i=(j=4,k=8,l=16,m=32,n=64); printf(“%d”, i); // Will give you 64

See the pattern? 看模式?

Basically, i is being set to whatever the value of the last assignment in the braces is, since the , operator will evaluate each assignment in sequence but return the value of the last assignment made in your case above. 基本上, i被设置为大括号中最后一个赋值的值,因为,运算符将按顺序计算每个赋值,但返回上面案例中最后一个赋值的值。

More generally, the , operator ( comma operator ) will evaluate a series of expressions in sequence and return the value of the last expression. 更一般地, ,操作符( 逗号运算符 )将评估一系列序列表达式并返回最后一个表达式的值。 So in your case, i is being assigned the value being assigned last in braces (since the return from an assignment, is the value being assigned), which is 32. 所以在你的情况下, i被分配了最后在大括号中分配的值(因为从赋值返回,是被赋值的值),即32。

The comma operator is left associative. 逗号运算符是左关联的。

It evaluates j=4 followed by k=8 , followed by l=16 and finally m=32 and returns 32. Hence i gets the value of 32 . 它评估j=4然后是k=8 ,接着是l=16 ,最后m=32并返回32.因此我得到值32

In other words, whatever in the bracket is evaluated first from left to right; 换句话说,括号中的任何内容首先从左到右进行评估; and the right most expression is returned as an output of the bracket as the result int i gets the decimal value 32. 并且最右边的表达式作为括号的输出返回 ,因为结果int i得到小数值32。

Not really an "answer" but it should be noted that the main use of the comma operator is to sequentially evaluate expressions with side effects such as function calls, assignments, etc. in contexts where multiple statements would not be valid. 不是真正的“答案”,但应该注意的是,逗号运算符的主要用途是在多个语句无效的上下文中依次计算具有副作用的表达式,例如函数调用,赋值等。 The most essential use is in macros where you want the entire macro to "return a value" but perform more than one operation. 最重要的用途是在宏中,您希望整个宏“返回一个值”但执行多个操作。 The only other ways to accomplish this are using the gcc ({ /* multiple statements here */ }) extension or having the macro simply call a static / static inline function. 实现此目的的唯一其他方法是使用gcc ({ /* multiple statements here */ })扩展或让宏只调用static / static inline函数。

Another frequent use I find for the comma operator is with the for statement: 我为逗号运算符找到的另一个常用用法是使用for语句:

for (n=cnt; n; n--, d++, s++)

and when I have an if statement that needs to do two closely-connected operations and I don't want the visual clutter of braces: 当我有一个if语句需要做两个紧密连接的操作时,我不想要大括号的视觉混乱:

if (condition) prefix="0x", len=2;

In these latter uses, the value of the result of the comma operator is not particularly useful, so it doesn't matter so much that it may be confusing to C beginners. 在后面这些用法中,逗号运算符的结果值并不是特别有用,因此它对于C初学者来说可能会让人感到困惑并不重要。

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

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