首页 > 试题广场 >

若要定义一个只允许本源文件中所有函数使用的全局变量,则该变量

[单选题]
若要定义一个只允许本源文件中所有函数使用的全局变量,则该变量需要使用的存储类型是()
  • extern
  • register
  • auto
  • static
推荐

答案:D

A,外部变量,可供所以源文件使用

B,寄存器变量,放在寄存器而非内存中,效率更高,一般是临时变量

C,自动变量,所有未加 static 关键字的都默认是 auto 变量,也就是我们普通的变量

D,静态变量,在内存中只存在一个,可供当前源文件的所有函数使用

编辑于 2015-01-26 15:36:51 回复(2)
D,对全局变量使用static定义后,只能在自己的源文件中使用

发表于 2015-10-20 13:46:29 回复(0)
       extern是一个关键字,它告诉编译器存在着一个变量或者一个函数,如果在当前编译语句的前面中没有找到相应的变量或者函数,也会在当前文件的后面或者其它文件中定义,来看下面的例子。

   

[cpp] view plain copy
  1. // extern.cpp : Defines the entry point for the console application.   
  2. //   
  3.   
  4. #include "stdafx.h"   
  5. #include <iostream>   
  6. using   namespace  std;  
  7.   
  8. extern   int  i;  
  9. extern   void  func();  
  10. int  _tmain( int  argc, _TCHAR* argv[]) //typedef wchar_t     _TCHAR;#define _tmain      wmain   
  11. {  
  12.     i = 0;  
  13.     func();  
  14.     return  0;  
  15. }  
  16.   
  17. int  i;  
  18.   
  19. void  func()  
  20. {  
  21.     i++;  
  22.     cout << "i = "  << i << endl;  
  23. }  

 

    上面代码中变量i和函数func在文件末尾定义,所以变量需要使用extern关键字告诉编译器,变量在别的地方定义。extern int i我原来以为extern i就可以,结果编译器报错,仔细想下确实应该,否则编译器不知道i是什么类型的数据,又怎么能判断i = 0是否是一个正确的赋值语句呢?

 

    那么定义在其他文件中的函数和变量,如何通过extern关键字调用呢?

    首先,定义在其它文件中的函数和变量,可以使用两种方法调用:

        一、使用头文件调用,这时候,函数和变量必须在头文件中定义和声明。

        二、使用extern关键字调用,这时候函数和变量在.cpp或者.c文件中定义和声明。

    看下面两个例子:

    devVar.cpp函数中定义:

       

[cpp] view plain copy
  1. #include "stdafx.h"   
  2.   
  3. int  i;  


 

    extern.cpp中

[cpp] view plain copy
  1. // extern.cpp : Defines the entry point for the console application.   
  2. //   
  3.   
  4. #include "stdafx.h"   
  5. #include <iostream>   
  6. using   namespace  std;  
  7.   
  8. extern   int  i;  
  9. extern   void  func();  
  10. int  _tmain( int  argc, _TCHAR* argv[]) //typedef wchar_t     _TCHAR;#define _tmain      wmain   
  11. {  
  12.     i = 0;  
  13.     func();  
  14.     return  0;  
  15. }  
  16.   
  17. void  func()  
  18. {  
  19.     i++;  
  20.     cout << "i = "  << i << endl;  
  21. }  


 

   编译工程,程序输出:i = 1,这里使用extern关键字声明在其它cpp文件中定义的变量和函数。

 

    #include <filensme> --- 将filename文件中的内容插入到新的文件中。

    deVar.h文件中代码为

[cpp] view plain copy
  1. #include <stdio.h>   
  2.   
  3. int  i = 1;  
  4.   
  5. void  func()  
  6. {  
  7.     printf("%d" ,i++);  
  8. }  

     函数func修改全局变量i的值并输出。

    extern.cpp文件内容为:

[cpp] view plain copy
  1. #include "stdafx.h"   
  2. #include <stdio.h>   
  3. #include <iostream>   
  4. using   namespace  std;  
  5. #include "devVar.h"   
  6. //extern int i;   
  7. //extern void func();   
  8.   
  9. int  main( void )  
  10. {  
  11.     for  ( int  x = 0;x < 10; x++)  
  12.     {  
  13.         func();  
  14.     }  
  15. }  

程序输出1,2,3,4,5,6,7,8,9,10,这里#include <filname.h> 包含定义在其它头文件中的函数和变量,在来看一个例子。

   

[cpp] view plain copy
  1. // extern.cpp : Defines the entry point for the console application.   
  2. //   
  3.   
  4. #include "stdafx.h"   
  5. #include <iostream>   
  6. using   namespace  std;  
  7.   
  8. extern   int  i;  
  9. extern   int   func( int ); //这里extern必需的,函数定义在其它cpp文件中   
[cpp] view plain copy
  1. int  _tmain( int  argc, _TCHAR* argv[]) //typedef wchar_t     _TCHAR;#define _tmain      wmain   
  2. {  
  3.     i = 100;  
  4.     func(i);  
  5.     return  0;  
  6. }  

    devVar.cpp文件中内容为:

   

[cpp] view plain copy
  1. #include "stdafx.h"   
  2. #include <iostream>   
  3. using   namespace  std;  
  4.   
  5. int  i;  
  6.   
  7. int  func( int  a)  
  8. {  
  9.     i = a;  
  10.     cout << "i = "  << i << endl;  
  11.     return  0;  
  12. }  


    这样,同样是输出了i= 100。

    能够使用extern引用其它cpp文件中定义的函数说明了一个问题:

    如果一个工程现编译cpp文件,在把多个目标文件链接成为可执行文件,而两个或多个文件中,定义了相同的全局变量,那么,程序编译的时候不会报错,因为编译器单独编译每个文件,在链接可执行文件的时候,由于多个目标文件中含有相同的全局变量,而生成可执行文件的时候,任何文件中定义的全局变量对其它目标文件都是可见的,此时由于变量定义冲突而发生错误。看下面的代码:

   

[cpp] view plain copy
  1. // extern.cpp : Defines the entry point for the console application.   
  2. //   
  3.   
  4. #include "stdafx.h"   
  5. #include <iostream>   
  6. using   namespace  std;  
  7.   
  8. int  i;  
  9. extern   int   func( int ); //这里extern是必须的函数定义在别的cpp文件中   
  10. int  _tmain( int  argc, _TCHAR* argv[]) //typedef wchar_t     _TCHAR;#define _tmain      wmain   
  11. {  
  12.     i = 100;  
  13.     func(i);  
  14.     return  0;  
  15. }  


 

devVar.cpp文件中,内容为:

[cpp] view plain copy
  1. #include "stdafx.h"   
  2. #include <iostream>   
  3. using   namespace  std;  
  4.   
  5. int  i;  
  6.   
  7. int  func( int  a)  
  8. {  
  9.     i = a;  
  10.     cout << "i = "  << i << endl;  
  11.     return  0;  
  12. }  

    单独compile任何一个cpp文件都是对的,但是 编译工程,生成可执行文件的时候报错:

    1>LINK : D:\vctest\extern\Debug\extern.exe not found or not built by the last incremental link; performing full link
1>devVar.obj : error LNK2005: "int i" (?i@@3HA) already defined in extern.obj
1>D:\vctest\extern\Debug\extern.exe : fatal error LNK1169: one or more multiply defined symbols found

    原因是:两个.cpp文件中都定义了全局变量i,变量重复定义了。

 

    PS:定义在.h文件中的函数和变量不能使用extern变量声明,原因是#include <filename>在预编译的时候将.h文件中的内容插入了cpp文件中,因此编译器找得到在其它.h文件中定义的变量或者函数。编译的时候,只编译cpp文件的内容,.h文件时不参与编译,如果使用extern声明在.h文件中定义的变量或者函数,那么声明为extern的变量和函数在其它.cpp文件中找不到,因此程序编译的时候就发生了错误。
 原文来自:http://blog.csdn.net/sruru/article/details/7951019

 

发表于 2017-05-05 20:03:02 回复(0)

A,外部变量,可供所以源文件使用

B,寄存器变量,放在寄存器而非内存中,效率更高,一般是临时变量

C,自动变量,所有未加 static 关键字的都默认是 auto 变量,也就是我们普通的变量

D,静态变量,在内存中只存在一个,可供当前源文件的所有函数使用

发表于 2015-06-18 14:22:25 回复(0)
static声明的变量只能供该源文件内所有函数使用
发表于 2022-01-11 17:39:27 回复(0)

extern所有源文件使用

auto 除static外

registic 寄存器中,临时变量

static 内存中只存在一个

发表于 2019-03-14 21:29:34 回复(0)
D.static
【存储方式】
    普通/static全局变量:静态存储方式
【作用域】
    ① 普通全局变量:整个源程序(可能多个源文件)
    ② static全局变量:定义该变量的源文件

B.register
    √ 局部变量、形参
    × 全局变量、静态变量
   ▲实际上没有必要用register声明变量

编辑于 2020-07-09 16:50:39 回复(0)
寄存器变量用register 声明,是存在寄存器中而非内存中,所以不能用&取地址。
发表于 2019-08-20 14:05:43 回复(0)
从作用域角度分,可分为局部变量和全局变量。
局部变量:(1)自动变量auto,离开函数后,储存空间立即释放。
                 (2)静态局部变量static,离开函数后,储存空间仍保存。
                 (3)寄存器变量register。效率更高,读取速度远大于内存
全局变量:(1)静态外部变量static,只限制于本文件中使用
                 (2)外部变量extern,可扩展全局变量作用域。
发表于 2017-07-31 16:46:12 回复(0)
D.静态变量,
发表于 2015-04-11 11:19:55 回复(0)
D static声明的变量只能供该源文件内所有函数使用
发表于 2015-04-10 15:13:22 回复(0)
D
必须的

发表于 2015-04-09 17:35:28 回复(0)
D  static  用在全局变量上 表示只允许本文件使用
发表于 2015-04-07 20:54:59 回复(0)
D
若如初见 已经回答的很仔细了,我再添一丢丢吧,那就是在定义全局变量的时候如果不声明关键字,例如直接int a,那这个变量也是可以在所有源文件中使用的.
发表于 2015-03-29 18:45:06 回复(0)
D
发表于 2015-01-12 14:17:13 回复(0)