`
yaojingguo
  • 浏览: 208191 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

C external declaration/definition

阅读更多

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.

 

 

 

0
0
分享到:
评论

相关推荐

    ANTLR写的ANSI C的语法

    - `translation_unit`:定义了整个C程序的顶级单位,由一个或多个`external_declaration`组成。 - `@init`:初始化符号表,确保每个文件有自己的作用域。 - **External Declaration:** ```antlr external_...

    C语言 子集 的BNF文法描述

    &lt;external-declaration&gt; ::= &lt;function-definition&gt; | &lt;declaration&gt; ``` 2. **函数定义**:函数定义包括函数头和函数体,如: ```bnf &lt;function-definition&gt; ::= &lt;declaration-specifiers&gt; &lt;declarator&gt; ...

    C语言文法文档

    在这篇文档中,我们详细介绍了C语言的文法,包括translation-unit、external-declaration、function-definition、declaration-specifier、type-specifier、struct-or-union-specifier、struct-declaration、...

    C语言文法1

    external_declaration -&gt; function_definition | declaration 3. 函数定义(Function Definition) 函数定义由函数类型、函数名和函数体组成。函数类型可以是void、char、int或float等。函数名是函数的标识符,...

    C语言(子集)的BNF文法描述

    `external_declaration`可以是函数定义(`function_definition`)或者声明(`declaration`)。函数定义包括类型说明符、声明符和复合语句,而声明可以是变量声明或函数声明。 `type_specifier`定义了数据类型,包括...

    编译原理:C语言文法

    首先,C语言的程序由一个或多个外部声明(external_declaration)组成。外部声明可以是函数定义(function_definition)或者声明(declaration)。函数定义由类型说明符(type_specifier)、声明符(declarator)和...

    C BNF grammar

    - external-declaration - struct-or-union - struct-declaration-list - specifier-qualifier-list - struct-declarator-list - enumerator-list - init-declarator-list - direct-declarator - type-...

    C89文法描述PDF

    - **规则**:`&lt;external-declaration&gt;::=&lt;function-definition&gt;|&lt;declaration&gt;` - **解释**:在C语言中,外部声明既可以是一个函数定义,也可以是一组变量声明。 ##### 2.3 Function Definition (函数定义) - **...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    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 ...

    c grammar cgrammar

    - **外部声明 (External Declaration)**:可以是函数定义或声明。 - **函数定义 (Function Definition)**:包括可选的声明限定符、声明符、声明列表等组成部分。 ### 6. 语法细节 - 语法中定义了未确定的终端符号,...

    explain DTD definition

    **DTD(Document Type Definition)** 是一种用于定义XML(Extensible Markup Language)文档结构的规范。它为XML文档设定了规则,确保了文档的一致性和有效性。DTD通过定义元素、属性、实体等来规范XML文档的结构,...

    C Programming

    **: 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 ...

    c reference card

    - **外部变量声明 (External Variable Declaration):** 使用 `extern` 关键字声明外部变量。 - **静态局部变量 (Static Local Variable):** 使用 `static` 关键字声明局部变量,使其在函数调用之间保持其值不变。 - ...

    ReferenceCard

    "Parameter information/context info"(Ctrl + P/Alt + Q)显示参数信息,"Quick definition"(Ctrl + Shift + I)查看定义,"Quick/external documentation"(Ctrl + Q/Shift + F1)获取文档。...

    XML入门教程:文档类型声明-XML/XSLT

    在XML中,文档类型声明(DOCTYPE Declaration)扮演着关键角色,它用于指定一个XML文档所遵循的语法规则,即文档类型定义(DTD,Document Type Definition)。 DTD是一种规范,用于定义XML文档的结构,包括元素、...

    C++完整解析XML

    - XML结构:XML文档由元素(Element)、属性(Attribute)、文本内容(Text Content)和声明(Declaration)组成。元素是XML的最基本单位,通过嵌套形成树状结构。 - 名空间(Namespace):用于避免元素名冲突,...

    类定义与实现如何相互关联

    - 定义(Definition):提供了标识符的完整实现,比如变量的存储空间分配、函数的代码体或类的成员变量的初始化。如`int a = 1;`是变量`a`的定义,`void func(int x, int y) { ... }`是函数`func`的定义。 **内部...

    LCTF软件备份VariSpec™ Liquid Crystal Tunable Filters

    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 ...

Global site tag (gtag.js) - Google Analytics