C程序设计语言(K&R)学习笔记--5.extern小结

extern

声明变量在外部定义

例1:外部变量定义在文件开头,在调用它的函数的上面,extern可省略

#include <stdio.h>
int global ; 

void main(){

	printf("%d\n",global);

}


例2:外部变量定义在文件中间,在调用它的函数的下面,必须使用extern声明

#include <stdio.h>
void main(){
	extern int global ;
	printf("%d\n",global); 

}
int global=100; 


例3:主文件使用从文件里的外部变量,extern可以省略

主文件为:test01.c

子文件为:test02.c

test01.c

#include <stdio.h>
#include "test02.c" 

void main(){
	printf("%d\n",bbb);// 在test02.c 中定义	
}

test02.c


#include <stdio.h>
int bbb=222; 


例4:从文件使用主文件的外部变量,必须使用extern声明

主文件test01.c

#include <stdio.h>
#include "test02.c"
int aaa=111 ;
void main(){
	printf("%d\n",add());
}

从文件test02.c

#include <stdio.h>
int bbb=222; 
int add(){
	extern int aaa;// 在test01.c中定义
	return bbb+aaa; 
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值