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

Is it a comma operator in declaration and initialization statement for C?

我在看《C Programming Language》时发现了这句话:

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;

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

不,它可能不会,因为在声明中逗号充当序列点,保证将按照提到的顺序执行评估。 您可以在此处找到更多信息:Sequence point

声明中的逗号不是表达式中的逗号运算符(声明不是表达式,尽管声明中的初始化是表达式)。问题中的引用是准确的,当它说分隔声明的逗号不是逗号运算符时。

但是,每个声明符在它后面的逗号或分号处都是完整的,因此问题中的变量定义是完全定义的行为。当它暗示不能保证从左到右的评估时,引用是不正确的——尽管这是一种微妙的语言剖析。如果逗号是逗号运算符,那么这个事实将保证从左到右的评估;因为它们不是逗号运算符,所以从左到右的保证不会从逗号运算符的定义中产生。但是,因为每个声明符后面都有顺序点,所以从左到右的估值是分开保证的。

在标准中找到正确的措辞来证明这一说法比我预期的要难。它实际上在声明符部分。

§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 Initializers 没有添加任何相关内容。

顺序点很关键;这意味着在继续之前,左边的一切都得到了充分的评估,副作用也已经完成;它意味着从左到右的顺序,因此问题中的初始化已完全定义。

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