块范围链接C标准 [英] Block scope linkage C standard
问题描述
以下标识符没有链接:声明为对象或函数以外的任何标识符;声明为功能参数的标识符; 声明的没有存储类说明符extern的对象的块作用域标识符.
{
static int a; //no linkage
}
对于在可见该标识符的先前声明的范围中用存储类说明符extern声明的标识符,如果该先前声明指定了内部或外部链接,则该标识符在后面的声明中的链接与先前声明中指定的链接相同.如果没有在先声明可见,或者如果在先声明没有指定链接,则然后标识符具有外部链接.
{
static int a; //no linkage
extern int a; //a should get external linkage, no?
}
GCC错误:跟随声明的外部声明没有链接
GCC error: extern declaration of a follows declaration with no linkage
有人可以向我解释为什么会出现此错误吗?
Can somebody explain me why do I get this error?
谢谢
推荐答案
您的假设是正确的:a
的第二个声明具有外部链接.但是,由于代码违反了第6.7节中的约束,您会收到一条错误消息:
Your supposition is correct: the second declaration of a
has external linkage. However, you get an error because your code violates a constraint in §6.7:
3如果标识符没有链接,则最多只能有一个 标识符的声明(在声明符或类型说明符中) 范围和名称空间相同,但标签与 在6.7.2.3.中指定.
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.
也就是说,一旦声明a
没有链接,就无法在同一范围内再次声明它.
That is, once you've declared a
to have no linkage, you can't redeclare it again in the same scope.
此规则被调用的有效示例是:
A valid example of this rule being invoked is:
int a = 10; /* External linkage */
void foo(void)
{
int a = 5; /* No linkage */
printf("%d\n", a); /* Prints 5 */
{
extern int a; /* External linkage */
printf("%d\n", a); /* Prints 10 */
}
}
这篇关于块范围链接C标准的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!