C extern is confusing. The C Programming Language gives a simple treatment for it. But its usage is quite convoluted in C99 standard ant various C implementations.For a complete understanding of extern keyword and external declaration/definition, we have to refer to C99 standard which is hard to read and comprehend. For details, refer to 6.2.2 Linkages of identifiers and 6.9 External definitions.
Here is a simple program for me to clarify my thoughts on this topic.
one.c
int extern_val = 100;
two.c
#include <stdio.h>
/*
* extern keyword is option here since one.c contains the definition for extern_val.
*/
extern int extern_val;
/*
* extern keyword can't be put here since no other files contain an external
* defition of def.
*/
int def;
/*
* It is legal according to C99 standard. GCC issues a warning for it.
*/
extern int def_with_extern = 77;
int extern_decl_many_times = 99;
extern int extern_decl_many_times;
int extern_decl_many_times;
/* extern is optional here */
extern void extern_func()
{
}
int main(int argc, const char *argv[])
{
printf("extern_val: %d\n", extern_val);
printf("def: %d\n", def);
printf("def_with_extern: %d\n", def_with_extern);
printf("extern_decl_many_times: %d\n", extern_decl_many_times);
extern_func();
return 0;
}
Use gcc one.c two.c to try them.
分享到:
相关推荐
- `translation_unit`:定义了整个C程序的顶级单位,由一个或多个`external_declaration`组成。 - `@init`:初始化符号表,确保每个文件有自己的作用域。 - **External Declaration:** ```antlr external_...
<external-declaration> ::= <function-definition> | <declaration> ``` 2. **函数定义**:函数定义包括函数头和函数体,如: ```bnf <function-definition> ::= <declaration-specifiers> <declarator> ...
在这篇文档中,我们详细介绍了C语言的文法,包括translation-unit、external-declaration、function-definition、declaration-specifier、type-specifier、struct-or-union-specifier、struct-declaration、...
external_declaration -> function_definition | declaration 3. 函数定义(Function Definition) 函数定义由函数类型、函数名和函数体组成。函数类型可以是void、char、int或float等。函数名是函数的标识符,...
`external_declaration`可以是函数定义(`function_definition`)或者声明(`declaration`)。函数定义包括类型说明符、声明符和复合语句,而声明可以是变量声明或函数声明。 `type_specifier`定义了数据类型,包括...
首先,C语言的程序由一个或多个外部声明(external_declaration)组成。外部声明可以是函数定义(function_definition)或者声明(declaration)。函数定义由类型说明符(type_specifier)、声明符(declarator)和...
- external-declaration - struct-or-union - struct-declaration-list - specifier-qualifier-list - struct-declarator-list - enumerator-list - init-declarator-list - direct-declarator - type-...
- **规则**:`<external-declaration>::=<function-definition>|<declaration>` - **解释**:在C语言中,外部声明既可以是一个函数定义,也可以是一组变量声明。 ##### 2.3 Function Definition (函数定义) - **...
Note: If you use a symbol Foo in your source file, you should bring in a definition for Foo yourself, either via an #include or via a forward declaration. Do not depend on the symbol being brought in ...
- **外部声明 (External Declaration)**:可以是函数定义或声明。 - **函数定义 (Function Definition)**:包括可选的声明限定符、声明符、声明列表等组成部分。 ### 6. 语法细节 - 语法中定义了未确定的终端符号,...
**DTD(Document Type Definition)** 是一种用于定义XML(Extensible Markup Language)文档结构的规范。它为XML文档设定了规则,确保了文档的一致性和有效性。DTD通过定义元素、属性、实体等来规范XML文档的结构,...
**: Definition of structures and their purpose in C. - **How Structures Work**: Explanation of how to define and use structures in C programs. - **Pointers to Structures**: How to use pointers to ...
- **外部变量声明 (External Variable Declaration):** 使用 `extern` 关键字声明外部变量。 - **静态局部变量 (Static Local Variable):** 使用 `static` 关键字声明局部变量,使其在函数调用之间保持其值不变。 - ...
"Parameter information/context info"(Ctrl + P/Alt + Q)显示参数信息,"Quick definition"(Ctrl + Shift + I)查看定义,"Quick/external documentation"(Ctrl + Q/Shift + F1)获取文档。...
在XML中,文档类型声明(DOCTYPE Declaration)扮演着关键角色,它用于指定一个XML文档所遵循的语法规则,即文档类型定义(DTD,Document Type Definition)。 DTD是一种规范,用于定义XML文档的结构,包括元素、...
- XML结构:XML文档由元素(Element)、属性(Attribute)、文本内容(Text Content)和声明(Declaration)组成。元素是XML的最基本单位,通过嵌套形成树状结构。 - 名空间(Namespace):用于避免元素名冲突,...
- 定义(Definition):提供了标识符的完整实现,比如变量的存储空间分配、函数的代码体或类的成员变量的初始化。如`int a = 1;`是变量`a`的定义,`void func(int x, int y) { ... }`是函数`func`的定义。 **内部...
generates 搖nresolved external?at link-time Fixes/features added from previous release: b) added command VsEnableImplicitPalette() to code and documentation added command VsConnect() to code and ...