Chinaunix首页 | 论坛 | 博客
  • 博客访问: 78006
  • 博文数量: 6
  • 博客积分: 1402
  • 博客等级: 上尉
  • 技术积分: 270
  • 用 户 组: 普通用户
  • 注册时间: 2007-08-04 23:37
文章分类
文章存档

2011年(1)

2008年(5)

我的朋友
最近访客

分类: C/C++

2008-03-24 19:09:20

static and extern are two identifiers for external declaration in programming language C, which is used when linking together multiple source files.

for variable, the usage of these two is clear as following:
1. for local static variable, compiler will allocate it in static data segment, rather than stack
2. for global static variable, it will be discussed later together with global static function
3. for global variable with explicitly "extern", it can be declared anywhere, but only be defined in one source file.(following the general rule, if it's not initialized with any value, compiler will regard this sentence as a declaration; otherwise, a definition)

for function, "extern" is just useless due to the existing of "#include", whereas "static" has the same meanning with as for global variable ---- it prevents any access from somewhere outside current file. As a side effect, it can also sometimes handle the problem of name conflict. see the code below:

// file1
static int i=0;
static void func() {}

// file2
extern int i;
void func();

int main()
{
    func();
    i++;
    return 0;
}



the compilation failed, as expected. You have to remove the two "static" in file1.

Note that there is still some way to access static variable and function remotely, you can simply make a wrapper function as entrance to them, and through the wrapper function extra control can be done, like filtering.

// file1
static int i=0;
static void funcInternal() {}

int func()
{
    funcInternal();
    return i;
}

// file2
int func();

int main()
{
    int i = func();
    i++;
    return 0;
}




If you want to define a global variable, you can define it at one source file, and declare it with explicit "extern" in the global header, then let all other source files using this variable include this header. Note that this is equivalent to declaring it with "extern" in each source files which includes this header.
an example is shown below, the running result is 1.

// xxx.h
extern int i;
void func();

// xxx.c
#include "xxx.h"
int i=0;
void func() { i++; }

// main.c
#include <stdio.h>
#include "xxx.h"
int main()
{
    func();
    printf("%d", i);
    return 0;
}




Normally, you should not define static variables in header, cause that will allocate diferent memory space for EACH source file including this header. If you really want to define such a file-scope variable, you can just simply define it in source files respectively. an ill-coded example is shown below:

// xxx.h
static int i = 0;
void func();

// xxx.c
#include "xxx.h"
void func() { i++; }

// main.c
#include <stdio.h>
#include "xxx.h"
int main()
{
    func();
    printf("%d", i);
    return 0;
}

the running result is 0, rather than 1, because func() just increments the i in xxx.c, and the i in main.c is left intact.

Besides, while defining variable with "static in headers is just a "should-not", doing it without either "extern" or "static" is totally a "must-not". Because this will get linking failed with an error of redefinition (though the compilation can be passed)



Finally, the manner of declaration extern variable in header is widely used in C++, cause extern variable is actually the same thing as static member variable of class. but this kind of analogue has been out of this article.
阅读(1312) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:C++中按值返回对象

给主人留下些什么吧!~~