您的位置:首页 > 编程语言 > C语言/C++

C语言的外部变量extern

2014-05-09 00:15 211 查看
C语言的存储类型可分为:extern、auto、static、register。

外部变量定义在函数之外,通过同一个名字对外部变量的所有引用(即使这种引用来自于单独编译的不同函数),实际上都是引用同一个对外部变量的所有引用(C标准中把这一性质称为外部链接)。因此外部变量可以在全局范围内访问。

getChar.c:
#include <stdio.h>

#include <stdlib.h>

extern char str[];//头文件中不用声明

int index_str = 0;

char getChar(){

return str[index_str++];

}

getStr.c:
#include <stdio.h>

#include <stdlib.h>

char str[255];

/*get the testfile string */

void getStr(char* filename){

int index = 0;

FILE* fp;

if( (fp = fopen(filename,"r") ) == NULL){

printf("open test_file fail !!!\n");

exit(1);

}

while((str[index++] = getc(fp))!=EOF);

str[index] = '\0';/*结尾标志*/

fclose(fp);

}
http://blog.csdn.net/pipisorry/article/details/25346379
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: