块范围的变量 - 许多声明 - 标准?

时间:2022-07-19 13:33:32

Where does the C standard specify that I cannot declare the same identifier many times in the same block scope?

C标准在哪里指定我不能在同一块范围内多次声明相同的标识符?

2 个解决方案

#1


5  

Some identifiers can be redeclared, such as struct tags and extern objects. But local variables can't:

某些标识符可以重新声明,例如struct标签和extern对象。但局部变量不能:

void bar(void)
{
    int a;
    int a; // constraint violation, 'a' declared a second time
}

and that is required by C99 section 6.7 paragraph 3:

这是C99第6.7节第3段所要求的:

If an identifier has no linkage, there shall be no more than one declaration of the identifier (in a declarator or type specifier) with the same scope and in the same name space, except for tags as specified in 6.7.2.3.

如果标识符没有链接,则除了6.7.2.3中指定的标记之外,标识符(在声明符或类型说明符中)的声明不应超过一个具有相同作用域和相同名称空间的声明。

This is a "shall" sentence in a "constraints" section, so a program that violates it is ill-formed.

这是“约束”部分中的“应”句子,因此违反它的程序是不正确的。

The C standard never uses the term "local variable," but an "identifier [that] has no linkage" is almost the same thing. (You can see that it's not exactly the same thing, or there would be no need to make an exception for tags.) For the precise definition see section 6.2.2.

C标准从不使用术语“局部变量”,但“没有链接的标识符”几乎是相同的。 (您可以看到它不完全相同,或者不需要为标记做出异常。)有关精确定义,请参阅第6.2.2节。

(C2011 amends this paragraph slightly, to permit the repetition of typedef declarations as well as tags.)

(C2011稍微修改了这一段,允许重复typedef声明和标签。)

#2


0  

You can declare the same identifier many times in block scope:

您可以在块范围内多次声明相同的标识符:

int main()
{
    extern int x;
    extern int x;
    extern int x;
}

Here we have three declarations of x at block scope. This is valid because they all have compatible types (C11 6.7/4) and the same linkage.

这里我们在块范围内有三个x声明。这是有效的,因为它们都具有兼容类型(C11 6.7 / 4)和相同的链接。

I think what you mean to ask is why you cannot define the same identifier as a variable many times. This is covered by C11 6.7/3:

我想您要问的是为什么您不能多次将相同的标识符定义为变量。这由C11 6.7 / 3涵盖:

If an identifier has no linkage, there shall be no more than one declaration of the identifier (in a declarator or type specifier) with the same scope [...]

如果标识符没有链接,则标识符的声明(在声明符或类型说明符中)不得超过一个具有相同范围的声明[...]

The term "no linkage" is covered by the section on linkage, 6.2.2. Variables defined at block scope have "no linkage" (6.2.2/6). In the previous quote, bear in mind that all definitions are declarations; since it says there cannot be multiple declarations with no linkage it follows that there cannot be multiple definitions.

关于连锁的部分,6.2.2涵盖了“无连接”一词。在块范围内定义的变量具有“无链接”(6.2.2 / 6)。在前面的引用中,请记住所有定义都是声明;因为它说不能有多个没有链接的声明,所以不能有多个定义。

#1


5  

Some identifiers can be redeclared, such as struct tags and extern objects. But local variables can't:

某些标识符可以重新声明,例如struct标签和extern对象。但局部变量不能:

void bar(void)
{
    int a;
    int a; // constraint violation, 'a' declared a second time
}

and that is required by C99 section 6.7 paragraph 3:

这是C99第6.7节第3段所要求的:

If an identifier has no linkage, there shall be no more than one declaration of the identifier (in a declarator or type specifier) with the same scope and in the same name space, except for tags as specified in 6.7.2.3.

如果标识符没有链接,则除了6.7.2.3中指定的标记之外,标识符(在声明符或类型说明符中)的声明不应超过一个具有相同作用域和相同名称空间的声明。

This is a "shall" sentence in a "constraints" section, so a program that violates it is ill-formed.

这是“约束”部分中的“应”句子,因此违反它的程序是不正确的。

The C standard never uses the term "local variable," but an "identifier [that] has no linkage" is almost the same thing. (You can see that it's not exactly the same thing, or there would be no need to make an exception for tags.) For the precise definition see section 6.2.2.

C标准从不使用术语“局部变量”,但“没有链接的标识符”几乎是相同的。 (您可以看到它不完全相同,或者不需要为标记做出异常。)有关精确定义,请参阅第6.2.2节。

(C2011 amends this paragraph slightly, to permit the repetition of typedef declarations as well as tags.)

(C2011稍微修改了这一段,允许重复typedef声明和标签。)

#2


0  

You can declare the same identifier many times in block scope:

您可以在块范围内多次声明相同的标识符:

int main()
{
    extern int x;
    extern int x;
    extern int x;
}

Here we have three declarations of x at block scope. This is valid because they all have compatible types (C11 6.7/4) and the same linkage.

这里我们在块范围内有三个x声明。这是有效的,因为它们都具有兼容类型(C11 6.7 / 4)和相同的链接。

I think what you mean to ask is why you cannot define the same identifier as a variable many times. This is covered by C11 6.7/3:

我想您要问的是为什么您不能多次将相同的标识符定义为变量。这由C11 6.7 / 3涵盖:

If an identifier has no linkage, there shall be no more than one declaration of the identifier (in a declarator or type specifier) with the same scope [...]

如果标识符没有链接,则标识符的声明(在声明符或类型说明符中)不得超过一个具有相同范围的声明[...]

The term "no linkage" is covered by the section on linkage, 6.2.2. Variables defined at block scope have "no linkage" (6.2.2/6). In the previous quote, bear in mind that all definitions are declarations; since it says there cannot be multiple declarations with no linkage it follows that there cannot be multiple definitions.

关于连锁的部分,6.2.2涵盖了“无连接”一词。在块范围内定义的变量具有“无链接”(6.2.2 / 6)。在前面的引用中,请记住所有定义都是声明;因为它说不能有多个没有链接的声明,所以不能有多个定义。