C10_C语言-extern和static

1.关于函数

    1)在头文件定义函数


#ifndef _MYH_H_
	#define _MYH_H_

	#include <stdio.h>
	
	extern void funcMY();	//声明自定义函数
	
	extern void funcMY(){
		printf("funcMY:外部函数,内外皆可访问\n");
	}
	
#endif

    2)在源文件定义函数


#include <stdio.h>

static void funcStatic();

extern void funcFunc() {
	printf("funcFunc:外部函数,内外皆可访问\n");
	//funcStatic();	//调用本.c中的内部函数
}

static void funcStatic(){
	printf("funcStatic:myFunc.c的内部函数,仅本.c可访问\n");
}

    3)引用函数

/**
 * 外部函数:可以被其它.c文件访问(默认)。extern,外部,一般省略
 * 		多个.c中不能有重名的外部函数
 * 内部函数:仅在本.c文件访问。static,内部的,不可省略
 * 		多个.c中可以有重名的内部函数
 **/
#include <stdio.h> 
#include "myh.h" 

extern void test(){
	printf("test:外部函数,内外皆可访问\n");
}

static void funcStatic(){
	printf("funcStatic:test.c的内部函数,仅本.c可访问\n");
}

void main() {
	funcFunc();		//直接调用源码文件中的外部函数。未在本.c文件中声明
	funcStatic();	//调用本.c中的内部函数
	funcMY();		//调用头文件中定义的外部函数
	test();			//调用本.c中的外部函数
}


2.关于变量

    1)头文件中的变量

#ifndef _MYH_H_
	#define _MYH_H_

	#include <stdio.h>

	int e;				//默认外部。同名的变量,都是一个内存空间,包含到test.c后重名不报错
	static int s = 45;	//内部变量
	
	void funcH();
	
	void funcH(){
		printf("myh:e=%d, e地址=%p\n", e, &e);
		printf("myh:s=%d, s地址=%p\n", s, &s);
	}
	
#endif

    2)其它源码文件的变量

#include <stdio.h>

int e;					//同名外部变量,在test.c文件中已定义了。
extern int s, num=998;	//外部变量。和test.c中内部变量重名
static int s = 75;		//内部变量

extern void funcFunc() {
	e += 10;
	printf("myFunc:e=%d, e地址=%p\n", e, &e);
	printf("myFunc:s=%d, s地址=%p\n", s, &s);	//优先使用内部变量
	printf("myFunc:num=%d, num地址=%p\n", num, &num);	//
}

    3)引用变量

/**
 * 外部变量:可以被其它.c文件访问(默认)。extern,外部,一般省略
 * 		多个.c中不能有重名的外部函数
 * 内部变量:仅在本.c文件访问。static,内部的,不可省略
 * 		多个.c中可以有重名的内部函数
 **/
#include <stdio.h>
#include "myh.h"	//这个头文件中已经有了static int s

extern int e = 90;		//外部变量
//static int s = 45;	//内部变量。因为自定义头文件导入,已经包含了

void main() {
	funcFunc();		//其它源文件的函数
	/*打印:
		myFunc:e=100, e地址=00402004		//函数的和test.c重名外部变量
		myFunc:s=75,  s地址=0040200c		//函数私有的内部变量
		myFunc:num=998, num地址=00402008	//函数外部变量
	*/

	funcH();		//头文件包含的函数
	/*打印:
		myh:e=100, e地址=00402004	//函数的和test.c重名外部变量
		myh:s=45,  s地址=00402000	//函数私有的内部变量,包含到本.c中
	*/

	printf("test:e=%d, e地址=%p\n", e, &e);
	//test:e=100, e地址=00402004

	printf("test:s=%d, s地址=%p\n", s, &s);
	//test:s=45,  s地址=00402000

	extern int num;	//外部变量。如果省略extern,默认是局部变量,没有static,不是内部变量
	num = 546;		//声明和初始化须两行
	printf("test:num=%d, num地址=%p\n", num, &num);
	//test:num=546, num地址=00402008		//函数外部变量

	printf("-----------------------------------------------------------\n");

	void test(){
		int ii = 0;			//局部变量
		ii++;
		printf("II:%d\,", ii);

		static int iii = 0;	//静态局部变量。只分配一次内存空间。不是内部变量
		iii++;
		printf("III:%d\n", iii);
	}

	test();	//II:1,III:1
	test();	//II:1,III:2
	test();	//II:1,III:3

	void test2();
	test2();
}

void test2(){
	static int iii;//静态局部变量,和其它方法中的不相干
	printf("test2,III:%d\n", iii);	//0
}

- end

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值