为什么在逗号分隔值的列表上加上括号会改变赋值?(复制)

时间:2022-05-02 00:25:13

This question already has an answer here:

这个问题已经有了答案:

Please consider following code,

请考虑下面的代码,

    int i;

    i = 1,2,3,4,5;
    printf("First time i = %d\n",i);

    i = (1,2,3,4,5);
    printf("Second time i = %d\n",i);   

Output:

输出:

First time i = 1

第一次i = 1

Second time i = 5

第二次i = 5


Why do the parentheses make the comma operator take last value and without parentheses it takes first value?

为什么圆括号使逗号运算符取最后的值,而没有圆括号,它取第一个值?

Thanks in advance.

提前谢谢。

4 个解决方案

#1


20  

First one is equivalent to (i = 1),2,3,4,5; which means the commas have no effect. When used with parentheses it returns the last value in the "comma delimited list"

第一个等于(i = 1) 2 3 4 5;这意味着逗号没有作用。当使用括号时,它将返回“逗号分隔列表”中的最后一个值

This is all due to operator precedence, which you can view a table about here

这都是由于操作符优先级,您可以在这里查看表

#2


8  

This is due to the operator precedence and the order of evaluation. = binds harder than , and from that we can figure out that the below two expressions are the same:

这是由于运算符的优先级和计算顺序。=比绑扎更困难,由此我们可以得出以下两个表达式是相同的:

i = 1,2,3,4,5 /* <- same as -> */ (i = 1),(2),(3),(4),(5)

side-note: the comma operator is the "weakest" operator of them all

注意:逗号运算符是所有运算符中“最弱”的运算符


Why does the comma operator yield the last value of our list?

为什么逗号运算符会产生列表的最后一个值?

To put it simple this operator evaluate the first operand only to discard it and move on to the next one, it binds left-to-right which means that it will start from the left, and continue walking towards the right.

简单地说,这个运算符对第一个操作数求值,然后将它丢弃并继续到下一个操作数,它从左到右绑定,这意味着它将从左开始,并继续向右走。


Where can I read more about this topic?

我在哪里可以看到更多关于这个话题的信息?

#3


5  

Because = has a higher precedence than , (which has the lowest), the first is the same as

因为=有一个更高的优先级,(有最低的优先级),第一个是相同的。

(i = 1),2,3,4,5;

which assigns 1 to i (i = 1) then evaluating the expressions 2, 3, 4, and 5 through the comma operators (the whole expression actually results in 5, which is not used). In the second one,

它将1赋给i (i = 1),然后通过逗号运算符(整个表达式实际上得到5,不使用)计算表达式2、3、4和5。在第二个,

(1,2,3,4,5)

is parenthesized, therefore it will be first evaluated before =. It results in 5 (the right-most expression; this is the behavior of the comma operator) which is then assigned to i.

是括号括起来的,因此它将在=之前先求值。它的结果是5(最右边的表达式;这是逗号运算符的行为),然后分配给i。

i = (1,2,3,4,5);
    |         |
    \---------\--- results in 5 then is assigned to i

See operator precedence Wikipedia article.

请参阅操作员优先*的文章。

#4


4  

Assignment has higher precedence than comma , hence the result you get in the first case. You can find the entire operator precedence table here .

赋值的优先级高于逗号,因此在第一种情况下会得到结果。您可以在这里找到整个运算符优先表。

Why parenthesis makes comma operator to take last value and without parenthesis it takes first value?

为什么括号使逗号运算符取最后值,而没有括号,它取第一个值?

Because parenthesis is used to override the precedence. The first case is implicitly equivalent to :

因为括号用于覆盖优先级。第一个例子隐含地等价于:

(i = 1),2,3,4,5;

Comma evaluates from left to right and the rightmost value is the value of the entire expression. Read the documentation here.

逗号从左到右计算,最右边的值是整个表达式的值。阅读文档。

The comma operator has left-to-right associativity. Two expressions separated by a comma are evaluated left to right. The left operand is always evaluated, and all side effects are completed before the right operand is evaluated.

逗号运算符具有从左到右的结合律。用逗号分隔的两个表达式由左向右计算。总是对左操作数进行求值,并且在对右操作数求值之前完成所有的副作用。

#1


20  

First one is equivalent to (i = 1),2,3,4,5; which means the commas have no effect. When used with parentheses it returns the last value in the "comma delimited list"

第一个等于(i = 1) 2 3 4 5;这意味着逗号没有作用。当使用括号时,它将返回“逗号分隔列表”中的最后一个值

This is all due to operator precedence, which you can view a table about here

这都是由于操作符优先级,您可以在这里查看表

#2


8  

This is due to the operator precedence and the order of evaluation. = binds harder than , and from that we can figure out that the below two expressions are the same:

这是由于运算符的优先级和计算顺序。=比绑扎更困难,由此我们可以得出以下两个表达式是相同的:

i = 1,2,3,4,5 /* <- same as -> */ (i = 1),(2),(3),(4),(5)

side-note: the comma operator is the "weakest" operator of them all

注意:逗号运算符是所有运算符中“最弱”的运算符


Why does the comma operator yield the last value of our list?

为什么逗号运算符会产生列表的最后一个值?

To put it simple this operator evaluate the first operand only to discard it and move on to the next one, it binds left-to-right which means that it will start from the left, and continue walking towards the right.

简单地说,这个运算符对第一个操作数求值,然后将它丢弃并继续到下一个操作数,它从左到右绑定,这意味着它将从左开始,并继续向右走。


Where can I read more about this topic?

我在哪里可以看到更多关于这个话题的信息?

#3


5  

Because = has a higher precedence than , (which has the lowest), the first is the same as

因为=有一个更高的优先级,(有最低的优先级),第一个是相同的。

(i = 1),2,3,4,5;

which assigns 1 to i (i = 1) then evaluating the expressions 2, 3, 4, and 5 through the comma operators (the whole expression actually results in 5, which is not used). In the second one,

它将1赋给i (i = 1),然后通过逗号运算符(整个表达式实际上得到5,不使用)计算表达式2、3、4和5。在第二个,

(1,2,3,4,5)

is parenthesized, therefore it will be first evaluated before =. It results in 5 (the right-most expression; this is the behavior of the comma operator) which is then assigned to i.

是括号括起来的,因此它将在=之前先求值。它的结果是5(最右边的表达式;这是逗号运算符的行为),然后分配给i。

i = (1,2,3,4,5);
    |         |
    \---------\--- results in 5 then is assigned to i

See operator precedence Wikipedia article.

请参阅操作员优先*的文章。

#4


4  

Assignment has higher precedence than comma , hence the result you get in the first case. You can find the entire operator precedence table here .

赋值的优先级高于逗号,因此在第一种情况下会得到结果。您可以在这里找到整个运算符优先表。

Why parenthesis makes comma operator to take last value and without parenthesis it takes first value?

为什么括号使逗号运算符取最后值,而没有括号,它取第一个值?

Because parenthesis is used to override the precedence. The first case is implicitly equivalent to :

因为括号用于覆盖优先级。第一个例子隐含地等价于:

(i = 1),2,3,4,5;

Comma evaluates from left to right and the rightmost value is the value of the entire expression. Read the documentation here.

逗号从左到右计算,最右边的值是整个表达式的值。阅读文档。

The comma operator has left-to-right associativity. Two expressions separated by a comma are evaluated left to right. The left operand is always evaluated, and all side effects are completed before the right operand is evaluated.

逗号运算符具有从左到右的结合律。用逗号分隔的两个表达式由左向右计算。总是对左操作数进行求值,并且在对右操作数求值之前完成所有的副作用。