一、基本概念
C++存储类-----extern的基本用法
eextern 存储类提供全局变量的用法,但是真正在使用的时候,它可以将定义在第一个文件中的变量的值,使第二个文件能够使用
如:main.cpp中
第一个文件的代码:
#include <iostream>
#include <stdio.h>
using namespace std;
int a;
extern void fun();
int main()
{
a = 5;
fun();
while (1);
}
第二个文件:Practice..cpp的用法:
#include <iostream>
#include <stdio.h>
using namespace std;
extern int a;
void fun(void)
{
cout << "a is " << a << endl;
}
结果见下图: