加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

它是C语句的声明和初始化语句中的逗号运算符吗?

发布时间:2020-12-16 10:03:55 所属栏目:百科 来源:网络整理
导读:我正在阅读C编程语言,发现这句话: The commas that separate … variables in declarations … are not comma operators,and do not guarantee left to right evaluation. 如果是这样,他们是否在此代码中使用逗号运算符? int a=1,b=a+1,c=b+a+1,d=c+b+a+1;
我正在阅读C编程语言,发现这句话:

The commas that separate … variables in declarations … are not comma operators,and do not guarantee left to right evaluation.

如果是这样,他们是否在此代码中使用逗号运算符?

int a=1,b=a+1,c=b+a+1,d=c+b+a+1;

我很确定它会起作用.但是如果它们不是逗号运算符并且不保证从左到右的顺序,则上述语句可能会失败,对吧?

解决方法

声明中的逗号不是表达式中的逗号运算符(声明不是表达式,尽管声明中的初始化是表达式).如果单独声明的逗号不是逗号运算符,则问题中的引用是准确的.

但是,每个声明符都以其后面的逗号或分号完成,因此问题中的变量定义是完全定义的行为.当它暗示从无法保证从左到右的评价时,引用是不正确的 – 虽然它是一个精巧的语言剖析.如果逗号是逗号运算符,则该事实将保证从左到右的评估;因为它们不是逗号运算符,所以从左到右的保证不会出现在逗号运算符的定义中.但是,因为每个声明符后面都有序列点,所以从左到右的估值是单独保证的.

在标准中找到正确的措辞来证明这种说法是比我预期的更难.它实际上是关于声明者的部分.

§6.7 Declarations

Syntax

declaration:
??????????declaration-specifiers init-declarator-listopt ;
??????????…

init-declarator-list:
??????????init-declarator
??????????init-declarator-list,init-declarator

init-declarator:
??????????declarator
??????????declarator = initializer

?6 The declaration specifiers consist of a sequence of specifiers that indicate the linkage,storage duration,and part of the type of the entities that the declarators denote. The init-declarator-list is a comma-separated sequence of declarators,each of which may have additional type information,or an initializer,or both. The declarators contain the identifiers (if any) being declared.

?7 If an identifier for an object is declared with no linkage,the type for the object shall be complete by the end of its declarator,or by the end of its init-declarator if it has an initializer; in the case of function parameters (including in prototypes),it is the adjusted
type (see 6.7.6.3) that is required to be complete.

§6.7.6 Declarators

?3 A full declarator is a declarator that is not part of another declarator. The end of a full declarator is a sequence point.

AFAICS,§6.7.9初始化程序不会添加任何相关内容.

序列点至关重要;这意味着在继续之前,左边的所有内容都得到了充分评估,副作用已经完成;它意味着从左到右排序,因此问题中的初始化是完全定义的.

序列点在完整的声明符之后,而不是初始化器,这有点奇怪;不过,我认为这不重要.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读