C语言外部变量extern

本文介绍了C语言中的四种存储类型:extern、auto、static、register,并通过实例详细解释了它们的作用域与链接特性。特别强调了extern关键字如何实现外部变量在不同函数间的共享。

http://blog.youkuaiyun.com/pipisorry/article/details/30311253

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);
}

from: http://blog.youkuaiyun.com/pipisorry/article/details/30311253

ref: 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值