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

被折叠的 条评论
为什么被折叠?



