Alpha's Tech Space

一个关于软件开发的技术交流空间,希望和大家共同进步!(.net/C/C++/Java)

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

如何使用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声明,并且在自己的代码中引用的时候,也不会成功,编译出错。

 
这下,应该清楚了。

posted on 2007-01-22 18:08  Lexrate  阅读(1551)  评论(0编辑  收藏  举报