C 语言中 static 和 extern 全局变量的使用

一、基础知识

1. 定义和声明

  1. 定义:只能出现在一个地方 ,确定对象的类型并且分配内存,用于创建新的对象。例如 int my_array[100]
  2. 声明:可以多次出现,描述对象的类型,用于指代其他地方定义的对象 ,例如:extern int my_array[]
int a;                   //分配了内存,是定义
extern int a;            //没有分配内存,是声明
int a = 1;               //分配了内存,是定义
extern int a = 1;        //分配了内存,是定义,此时 extern 可省略,与 int a = 1;等价 

2. 一个变量只能定义一次

  1. 在一个源程序中,一个变量不可以定义两次;
  2. 局部变量和全局变量可以重名,这不会导致重复定义错误;因为这种情况不会被认定为同一个变量,局部变量在使用时会屏蔽全局变量;
  3. 当两个定义重名且具有相同的作用域会导致重复定义错误。

3. externstatic 变量

  1. 它们都位于全局 / 静态存储区,即 在程序的整个运行期间都存在;
  2. static 局部变量的作用域为当前代码块,static 全局变量的作用域为当前源文件(.c .cpp文件),虽然他们在整个程序的运行期间都存在,但是脱离作用域后将无法被访问;
  3. extern 全局变量不但可以被其所在的源文件使用,也可以被同一程序的其他源文件使用

二、使用全局变量时易犯的错误

1. 普通全局变量

/*--------------------------head.h---------------------------*/
#pragma once
int a;


/*--------------------------head.c---------------------------*/
#include "head.h"


/*--------------------------main.c---------------------------*/
#include "head.h"
int main()
{
        return 0;
}

以上代码中,在头文件 head.h 中定义了普通全局变量 a ,头文件 head.h 被 源文件 head.cmain.c 同时包含,
执行gcc -Wall head.c main.c -o main编译以上代码失败,报重复定义错误:

/tmp/ccRNAavK.o:(.bss+0x0): multiple definition of `a'
/tmp/ccH2JR3U.o:(.bss+0x0): first defined here
collect2: ld 返回 1

结果分析:
1、全局变量 a 在源文件 head.cmain.c 中各自定义一次,重复定义。
2、这个错误是在链接阶段出现的,单独将head.cmain.c 编译成 .o 文件不会出现错误;
3、但是在链接阶段,编译器会发现全局变量 a 被分配了两次空间,拥有两个内存地址,故报错

2. static 全局变量

static 全局变量在编译时不会报重复定义的错误,是因为它把每个源文件的全局变量作用域限定在当前源文件;
各个源文件互相不知道对方拥有此变量,因此不会报错;
但是——这样导致每个源文件的这个全局变量都是不同的,即使它们拥有相同的变量名。

/*--------------------------head.h---------------------------*/
#pragma once
static int a;
void change_a(int b);
int get_a();
int* get_a_address();


/*--------------------------head.c---------------------------*/
#include "head.h"
void change_a(int b) {
        a = b;
}
int get_a() {
        return a;
}
int* get_a_address()
{
        return &a;
}


/*--------------------------main.c---------------------------*/
#include "head.h"
#include <stdio.h>
int main()
{
        printf("初始:\n");
        printf("main.c 源文件中 static全局变量 a 的值: %d, a 的地址: %p\n", a, &a);
        printf("head.c 源文件中 static全局变量 a 的值: %d, a 的地址: %p\n", get_a(), get_a_address);

        printf("在 main.c 中修改 a 为 1:\n");
        a = 1;
        printf("main.c 源文件中 static全局变量 a 的值: %d, a 的地址: %p\n", a, &a);
        printf("head.c 源文件中 static全局变量 a 的值: %d, a 的地址: %p\n", get_a(), get_a_address);

        printf("在 head.c 中修改 a 为 2:\n");
        change_a(2);
        printf("main.c 源文件中 static全局变量 a 的值: %d, a 的地址: %p\n", a, &a);
        printf("head.c 源文件中 static全局变量 a 的值: %d, a 的地址: %p\n", get_a(), get_a_address);
        return 0;
}

以上代码中,在头文件 head.h 中定义了全局变量 a
头文件 head.h 被 源文件 head.cmain.c 同时包含,因此,static 全局变量 a 在源文件 head.cmain.c 中各自定义,但不是同一个变量。
执行gcc -Wall head.c main.c -o main编译成功,运行结果如下:

初始:
main.c 源文件中 static全局变量 a 的值: 0, a 的地址: 0x600b34
head.c 源文件中 static全局变量 a 的值: 0, a 的地址: 0x400522
在 main.c 中修改 a 为 1:
main.c 源文件中 static全局变量 a 的值: 1, a 的地址: 0x600b34
head.c 源文件中 static全局变量 a 的值: 0, a 的地址: 0x400522
在 head.c 中修改 a 为 2:
main.c 源文件中 static全局变量 a 的值: 1, a 的地址: 0x600b34
head.c 源文件中 static全局变量 a 的值: 2, a 的地址: 0x400522

3. extern 全局变量(错误:未定义)

/*--------------------------head.h---------------------------*/
#pragma once
extern int a;


/*--------------------------head.c---------------------------*/
#include "head.h"


/*--------------------------main.c---------------------------*/
#include "head.h"
int main()
{
		a = 1;     //这里不是定义,只是赋值操作;
        return 0;
}
/tmp/cc5BhtrL.o: In function `main':
main.c:(.text+0x6): undefined reference to `a'
collect2: ld 返回 1

结果分析:
1、head.h 中声明了一个全局变量 a,没有进行定义,这导致——head.cmain.c 源文件中各自包含全局变量 a 的声明,整个源程序将找不到 全局变量 a 的 定义,当 main 函数试图使用变量 a 时,发生报错;
2、如果将 main 函数中的 a = 1;改成int a = 1;,可以正常运行,但这是创建了局部变量 a全局变量 a 仍旧未被定义,无法使用。

4. extern 全局变量(正确使用)

/*--------------------------head.h---------------------------*/
#pragma once
extern int a;

/*--------------------------head.c---------------------------*/
#include "head.h"
int a;                       //定义可以放到main.c,但必须只有一次定义


/*--------------------------main.c---------------------------*/
#include "head.h"
int main()
{
        return 0;
}

这是正确的使用,分析:
1、头文件 head.h#include "head.h"处展开,故源文件 head.cmain.c 都声明了一个 extern 全局变量 a
2、head.c 在声明之后,为 全局变量 a 分配了空间,即定义了 a ;
3、main.c 在声明之后,没有进行定义,但 extern 告知编译器此变量已经在其他地方定义了,故可以正常编译
4、链接阶段时,main.o 将在 head.o 中找到 全局变量 a,编译成功,此时源程序中拥有唯一的、可全局使用的 全局变量 a

三、总结

正确使用全局变量的方式:
1、在头文件中声明全局变量 a;
2、在使用到 全局变量 a的任意一个源文件中,进行一次定义,同时,整个源程序中,不能出现第二次定义。

  • 7
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值