惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

C
Check Point Blog
罗磊的独立博客
量子位
Microsoft Azure Blog
Microsoft Azure Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
J
Java Code Geeks
M
MIT News - Artificial intelligence
月光博客
月光博客
IT之家
IT之家
D
DataBreaches.Net
A
About on SuperTechFans
博客园 - 三生石上(FineUI控件)
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Last Week in AI
Last Week in AI
D
Docker
The GitHub Blog
The GitHub Blog
B
Blog
V
Visual Studio Blog
博客园 - Franky
N
Netflix TechBlog - Medium
博客园 - 【当耐特】
Martin Fowler
Martin Fowler
博客园 - 聂微东
U
Unit 42

博客园 - Lexrate

[IT] 关闭笔记本的蜂鸣提示 新的工作 [TOOL] 小巧但强大的反删除工具restoration 元宵节快乐 [work] 累就一个字 test again 复杂的“人" 关于项目管理 测试2 测试 测试 测试 测试 [设想] DOORS的替代方案 [随笔]艰辛而难忘的7个月 [系统]不要随意关闭WINDOWS Time服务 [代码]大家来动动脑筋吧 - Lexrate - 博客园 这几天的工作 [杂谈]很意味深长的话
[基础] 如何使用extern和static限定符
Lexrate · 2007-01-22 · via 博客园 - Lexrate

如何使用extern以及static

extern 和static的使用方法,应该属于C语言的基础知识了。可是,在实际工作中,还是经常会在代码里看到关于这两个限定符使用的问题,总结一下,大致有两类:其一,对于模块中的变量或者函数,不了解到底加不加static 或者 extern修饰符;其二,在加限定符的时候,不知道正确的使用方法。因此,有必要旧话重提,说明一下。

简单的说,记住两句话即可,

1 Static表示:被修饰的变量或者函数不可以被本模块以外的其他任何模块使用;而extern恰恰相反,表示同意其被被本模块以外的其他模块使用;

2 当变量或者程序没有被static或者extern 修饰的时候,变量或者函数可以被其他模块使用。

这么说可能还是比较空洞,看程序吧。

假定有两个模块 A B 模块B希望使用在模块A中定义的一些变量和函数,我们应该怎么做呢?

1 A不“表态”,B了解A的实现细节,自行取用.

/*----------------------------------------------

 Moudle_A.c

 This module contains the variable and function

 that user defined

 ----------------------------------------------*/

//declaration of the variable and function

int index;

void UserFunction(void);

//implementation of the function

void UserFunction(void)

{

    .....

    //use the index

    iRecord = index;

    ....

}

/*----------------------------------------------

 Moudle_B.c

 This module contains the variable and function

 that user defined

 ----------------------------------------------*/

extern int index;

extern void UserFunction(void);

//operate on the 'index' and 'UserFunction()'

...

/*----------------------------------------------

 Moudle_B.c

 This module contains the variable and function

 that user defined

 ----------------------------------------------*/

extern int index;

extern void UserFunction(void);

//operate on the 'index' and 'UserFunction()'

...

这里,index UserFunction 在模块A中声明的时候,A没有使用任何限定符,所以编译器理解,不做限制。所以,当模块B希望使用的时候,是允许的。但是,这不是一个很好的风格,有点不问自取的意思,不推荐。

2 A主动公开,B正常使用;

/*----------------------------------------------

 Moudle_A.h

 This module contains the prototype information

 ----------------------------------------------*/

//declaration of the variable and function

extern int index;

extern void UserFunction(void);

/*----------------------------------------------

 Moudle_A.c

 This module contains the variable and function

 that user defined

 ----------------------------------------------*/

#include "Moudle_A.c"

//initialize the variable

...

//implementation of the function

void UserFunction(void)

{

    .....

    //use the index

    iRecord = index;

    ....

}

/*----------------------------------------------

 Moudle_B.c

 This module contains the variable and function

 that user defined

 ----------------------------------------------*/

#include "Moudle_A.h"

//operate on the 'index' and 'UserFunction()'

...

这个例子中,模块A 提供了一个头文件,公开了index UserFunction的信息,明确的提供给其他模块。当模块B包含了模块A的头文件的时候,就可以使用index UserFunction了。模块AB互相配合,BA中变量和函数的使用,合理合法,推荐。

3 A主动保护,B无法使用;

/*----------------------------------------------

 Moudle_A.c

 This module contains the variable and function

 that user defined

 ----------------------------------------------*/

//declaration of the variable and function

static int index;

static void UserFunction(void);

//initialize the variable

...

//implementation of the function

void UserFunction(void)

{

    .....

    //use the index

    iRecord = index;

    ....

}

/*----------------------------------------------

 Moudle_B.c

 This module contains the variable and function

 that user defined

 ----------------------------------------------*/

#include "Moudle_A.h"

extern int index;

extern void UserFunction(void);

//operate on the 'index' and 'UserFunction()'

...

这个例子中,模块A static 限定了index UserFunction的作用范围,明确表示了不希望他们被别的模块引用。此时,即使模块B了解index UserFunction在模块A中的具体信息,试图用extern声明,并且在自己的代码中引用的时候,也不会成功,编译出错。

 
这下,应该清楚了。