为什么在括号之间用逗号分隔的值列表会更改分配? [英] Why does putting parentheses around a list of comma separated values change the assignment?

查看:53
本文介绍了为什么在括号之间用逗号分隔的值列表会更改分配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下代码,

    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);   

输出:

第一次i = 1

第二次i = 5

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

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

请先谢谢。

推荐答案

第一个等价于(i = 1),2,3,4,5; 表示逗号无效。与括号一起使用时,它返回逗号分隔列表中的最后一个值。

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"

这都是由于运算符优先级,您可以查看有关此处

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

这篇关于为什么在括号之间用逗号分隔的值列表会更改分配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆