在用C括号两个值的分配 [英] Assignment of two values in parentheses in C

查看:120
本文介绍了在用C括号两个值的分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是什么片code的用C做的:

What does this piece of code in C do:

p = (1, 2.1);

什么是我们知道的 P

推荐答案

在C中的逗号操作符是的序列点的,这意味着由逗号分隔的前pressions从左侧执行以对。整个前pression的的的是最右边的前pression的价值,你的情况 2.1 ,它被分配到变量 p

The comma operator in C is a sequence point which means that the expressions separated by the comma are executed from left to right. The value of the whole expression is the value of the rightmost expression, in your case 2.1, which gets assigned to the variable p.

由于您的示例前pressions没有副作用,这里使用逗号分隔符的是没有任何意义。

Since the expressions in your example don’t have side effects, the use of the comma separator here makes no sense whatsoever.

在另一方面括号是因为赋值运算符( = )的优先级比逗号运算符更强重要的(它具有更高的precedence),并会得到评估的逗号没有括号运算符之前的。因此,其结果将是点== 1

The parentheses on the other hand are important since the assignment operator (=) binds stronger than the comma operator (it has higher precedence) and would get evaluated before the comma operator without the parentheses. The result would thus be p == 1.

这篇关于在用C括号两个值的分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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