简体   繁体   English

是extern是写一个不是定义的声明的唯一方法吗?

[英]is extern the only way to write a declaration that is not also a definition?

Is that sane to conclude that the only type of variable declaration is "extern declaration" ? 是否可以得出结论,唯一的变量声明类型是“外部声明”?

It's known that the following code define (not declare ) x , because it allocate memory for variable x (KR chapter 4). 众所周知,以下代码定义 (不声明x ,因为它为变量x分配内存(KR第4章)。 Typically though, we would normally just say that we are declaring x (but not initializing it). 通常情况下,我们通常会说我们声明x (但不是初始化)。

int x; // <-- definition NOT declaration

The only declaration example I can find is to use with the keyword extern : 我能找到的唯一声明示例是使用关键字extern

extern int x;
extern int a[];

Is there any other variable declaration situation apart from extern declaration ? 除了extern声明之外还有其他变量声明情况吗?

A definition is a declaration with storage. 定义是带存储的声明。 From standard: 从标准:

6.7 Declarations 3/ A declaration specifies the interpretation and attributes of a set of identifiers. 6.7声明3 / A声明指定一组标识符的解释和属性。 A definition of an identifier is a declaration for that identifier that: 标识符的定义是该标识符的声明:

— for an object, causes storage to be reserved for that object; - 对于一个对象,导致为该对象保留存储;

— for a function, includes the function body;119) - 对于一个功能,包括功能体; 119)

— for an enumeration constant, is the (only) declaration of the identifier; - 对于枚举常量,是标识符的(唯一)声明;

— for a typedef name, is the first (or only) declaration of the identifier. - 对于typedef名称,是标识符的第一个(或唯一)声明。

Then a function prototype is a declaration, which may not be qualified extern if used for a forward declaration. 然后函数原型是一个声明,如果用于前向声明,它可能不是extern限定的。 A 一种

For more details, read the standard, section 6.7. 有关更多详细信息,请阅读标准,第6.7节。

extern is used , mostly for functions when the function exists in another file , either sourcecode or an object library. 当函数存在于另一个文件(源代码或对象库)中时,主要用于函数。

auto is used for stack variables, but since it is the default few people wite it explicitly. auto用于堆栈变量,但由于它是默认的,很少有人明确地使用它。

static is for static variables, kept even when a function exits. static用于静态变量,即使函数退出也会保留。 auto variables are lost when a function exits, so static variables are often used for total counts, user settings, settings of state, which may change, but are used throughout the whole time the program is running. 当函数退出时,自动变量会丢失,因此静态变量通常用于总计数,用户设置,状态设置,可能会更改,但在程序运行的整个过程中都会使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM