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
外部变量定义在函数之外,通过同一个名字对外部变量的所有引用(即使这种引用来自于单独编译的不同函数),实际上都是引用同一个对外部变量的所有引用(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
相关文章推荐
- c语言中struct 变量利用extern外部共享数据的方法
- C语言extern关键字定义外部变量--Redis源码extern使用
- c语言,全局变量,局部变量,外部函数,内部函数,stasic和extern的复习总结
- C语言外部变量extern
- C语言外部变量(extern)的使用
- C语言:全局变量、静态变量static、外部变量extern的区别
- 在C语言中,static静态变量,extern外部变量的区别
- 请问在C语言中静态变量Staic与外部变量extern有什么区别
- 1、C语言外部说明(变量及函数的作用域扩充)
- 【c基础】全局变量-局部变量 静态变量static-自动变量auto 外部变量extern-内部变量static 常量const-宏定义#difine
- 变量的存储类别、内外部函数【static\extern】
- C语言基础:C语言static和extern(2) - 作用与变量
- c语言中不允许在函数外部给全局变量赋值
- C语言引用外部变量和函数的区别
- Objective-C中使用extern引用外部全局变量(如:创建全局可变字符串)
- C语言外部调用结构体,变量和数组
- 1、C语言外部说明(变量及函数的作用域扩充)
- 黑马程序员————--c语言之static和extern,变量
- C语言的外部变量
- 黑马程序员----C语言内部函数,外部函数,内部变量,外部变量篇