关于C中extern的说明

建议大家看原文吧,虽然原文是英文的: http://geeksforgeeks.org/?p=840
首先说一下声明和定义的概念。声明就是在应用程序中说明变量或者函数的存在,但是却不为变量和函数分配内存。声明一个变量后,应用程序知道了变量的数据类型;声明一个函数后,应用程序知道它的参数和数据类型,参数的顺序和函数的返回类型。定义,就是除了拥有声明的作用外,它还为函数和变量分配存储空间。我们可以将定义想成声明的一个父类,由此看出,变量和函数的声明可以有很多次,但是函数的定义却只能有一次,因为同一个函数和变量不能有两个存储区域。

现在看一下函数的extern说明。在默认情况下,函数在声明和定义时就已经在其前面有extern了。例如,当我们声明函数时,

int foo(int arg1, char arg2);
在该函数的前面已经有了一个隐含的extern,并且编译器将它处理为
extern int foo(int arg1, char arg2);

在C语言中函数的定义也是一样。由于函数的声明可以有多次而函数的定义只能有一次,于是我们在多个.h和.c或者在同一.c和.h文件中多次声明同一个函数,但是函数的定义只能有一个。And as the extern extends the visibility to the whole program, the functions can be used (called) anywhere in any of the files of the whole program provided the declaration of the function is known。

再看一下变量的extern说明。用如下方法声明一个变量:
extern int var;
这只是声明一个变量,并不为其分配存储空间。
用如下方法定义一个变量:
int var;
此时一个int型的var变量被声明和定义了。在定义变量时不能像定义函数一样在其前面默认加上extern,因为如果像那样程序就不会为它分配存储空间了。
下面是关于extern变量的声明的例子:

Example 1:

int  var;
int  main( void )
{
    var = 10;
    return  0;
}

Analysis: This program is compiled successfully. Here var is defined (and declared implicitly) globally.

此程序被编译成功。Var 在这里定义 (和隐式声明) 全球。



Example 2:

extern  int  var;
int  main( void )
{
   return  0;
}

Analysis: This program is compiled successfully. Here var is declared only. Notice var is never used so no problems.

此程序被编译成功。在这里只声明了 var。通知 var 是永远不会用所以没有问题。


Example 3:

extern  int  var;
int  main( void )
{
  var = 10;
  return  0;
}

Analysis: This program throws error in compilation. Because var is declared but not defined anywhere. Essentially, the var isn’t allocated any memory. And the program is trying to change the value to 10 of a variable that doesn’t exist at all.

此程序在编译中抛出错误。因为 var 声明但不是定义任何地方。从根本上说,var 不被分配的任何内存。程序正试图将值更改为 10,根本不存在的变量。



Example 4:

#include "somefile.h"
extern  int  var;
int  main( void )
{
  var = 10;
  return  0;
}

Analysis: Supposing that somefile.h has the definition of var. This program will be compiled successfully.

假如那 somefile.h 有变种的定义此程序将编译成功。



Example 5:

extern  int  var = 0;
int  main( void )
{
  var = 10;
  return  0;
}

Analysis: if a variable is only declared and an initializer is also provided with that declaration, then the memory for that variable will be allocated i.e. that variable will be considered as defined. Therefore, as per the C standard, this program will compile successfully and work.

如果只声明一个变量和一个初始值设定项是也提供与该宣言中,然后将分配该变量的内存即该变量将被视为定义。因此,按照 C 标准,此程序会成功编译和工作。


1. Declaration can be done any number of times but definition only once.
2. “extern” keyword is used to extend the visibility of variables/functions().
3. Since functions are visible through out the program by default. The use of extern is not needed in function declaration/definition. Its use is redundant.
4. When extern is used with a variable, it’s only declared not defined.
5. As an exception, when an extern variable is declared with initialization, it is taken as definition of the variable as well.

1.宣言可以做任何数目的时代,但是只有一次的定义。

2."extern"关键字用于扩展 variables/functions() 的可见性。

3.由于功能是通过程序在默认情况下可见。函数的声明定义中不需要使用 extern。它的使用是多余的。

4.当 extern 用一个变量,它只被宣布不定义。

5.作为例外,当 extern 变量声明与初始化,它是作为定义的变量,以及。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值