C语言外部变量extern
2017-08-14 00:00
337 查看
http://blog.csdn.net/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.csdn.net/pipisorry/article/details/30311253
ref:
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.csdn.net/pipisorry/article/details/30311253
ref:
相关文章推荐
- 请问在C语言中静态变量Staic与外部变量extern有什么区别
- c语言中struct 变量利用extern外部共享数据的方法
- C语言的外部变量extern
- c语言,全局变量,局部变量,外部函数,内部函数,stasic和extern的复习总结
- C语言extern关键字定义外部变量--Redis源码extern使用
- C语言:全局变量、静态变量static、外部变量extern的区别
- C语言外部变量(extern)的使用
- 在C语言中,static静态变量,extern外部变量的区别
- C语言-变量的存储类别(auto, static, register, extern)
- 声明外部变量 extern
- 关于C语言中extern声明外部…
- C语言取extern变量的大小
- 黑马程序员-c语言宏定义以及外部变量
- C语言中局部变量和全局变量的存储类别(static,extern,auto,register)
- c语言中extern和static修饰的全局变量
- 【C语言】20-static和extern关键字2-对变量的作用
- C++全局变量尽量写在stdafx.cpp中 extern 关键字找不到外部定义就等于新定义
- 关于C语言中局部变量、外部变量、静态变量的理解
- C语言外部变量
- c语言变量和函数声明的修饰符static,extern,#define,const