局部变量和寄存器变量可以声明为extern吗? [英] Can local and register variables be declared extern?

查看:320
本文介绍了局部变量和寄存器变量可以声明为extern吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直想知道是否可以在本地声明外部变量和寄存器变量.如果可以的话,会施加什么限制?

I have been wondering whether an extern can be declared locally and a register variable. If it can be what would be the restrictions imposed?

推荐答案

在某些情况下可以将局部变量声明为外部变量

让我们阅读 C99 N1256标准草案.

该标准将局部变量"称为具有块范围".

The standard calls "local variables" as having "block scope".

6.7.1/5存储类说明符"说:

6.7.1/5 "Storage-class specifiers" says:

具有块作用域的函数的标识符的声明除了extern外,没有其他明确的存储类说明符.

The declaration of an identifier for a function that has block scope shall have no explicit storage-class specifier other than extern.

然后对于将extern添加到局部变量的含义,6.2.2/4标识符的链接"说:

Then for what it means to add extern to a local variable, 6.2.2/4 "Linkages of identifiers" says:

对于在可见该标识符的先前声明的范围中用存储类说明符extern声明的标识符,如果该先前声明指定了内部或外部链接,则该标识符在后面的声明中的链接是相同的作为先前声明中指定的链接.如果没有任何在先声明可见,或者在先声明未指定任何链接,则标识符具有外部链接.

For an identifier declared with the storage-class specifier extern in a scope in which a prior declaration of that identifier is visible, if the prior declaration specifies internal or external linkage, the linkage of the identifier at the later declaration is the same as the linkage specified at the prior declaration. If no prior declaration is visible, or if the prior declaration specifies no linkage, then the identifier has external linkage.

让我们来分解这些情况.

Lets break down those cases.

没有事先声明

void f() {
    extern int i;
}

与以下相同:

extern int i;
void f() {}

除了声明仅在f内部可见.

except that the declaration is only visible inside f.

这是因为i没有可见的先前声明.因此i具有外部链接(与全局变量相同的链接).

This is because i has no prior declaration visible. So i has external linkage (the same linkage as global variables).

事先声明未指定链接

void f() {
    int i;
    extern int i;
}

与以下相同:

void f() {
    extern int i;
}

因为先前的声明int i没有指定链接,因为第6段说:

because the prior declaration int i specifies no linkage because paragraph 6 says:

以下标识符没有链接:声明为对象或函数以外的任何标识符;声明为功能参数的标识符;声明的没有存储类说明符extern的对象的块作用域标识符.

The following identifiers have no linkage: an identifier declared to be anything other than an object or a function; an identifier declared to be a function parameter; a block scope identifier for an object declared without the storage-class specifier extern.

事先声明指定内部或外部链接

extern int i;
void f() {
    extern int i;
}

与以下相同:

extern int i;
void f() {}

和:

static int i;
void f() {
    extern int i;
}

与以下相同:

static int i;
void f() {}

因为在这两种情况下,我们分别都有一个先前可见的外部和内部(static)链接声明.

because in both cases we have a previous visible external and internal (static) linkage declarations respectively.

初始化本地外部

无效的C:

void f() {
    extern int i = 0;
}

因为块范围声明具有初始化.

because the block scope declaration has an initialization.

有效的C:

extern int i = 0;
void f() {}

但可以说是糟糕的样式,因为它等同于较短的样式:

but arguably bad style because equivalent to the shorter:

int i = 0;
void f() {}

因为6.7.8初始化说:

because 6.7.8 Initialization says:

如果标识符的声明具有块作用域,并且标识符具有外部或内部链接,则声明应不具有标识符的初始化程序.

If the declaration of an identifier has block scope, and the identifier has external or internal linkage, the declaration shall have no initializer for the identifier.

这篇关于局部变量和寄存器变量可以声明为extern吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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