头文件防卫式声明

有3个文件分别是text1.h text2.h text.c
text1.h

int a=1;

text2.h

#include "text1.h"

int b = 2;

text.c

#include "text1.h"
#include "text2.h"
#include <iotaream>

int main()
{
	return 0;
}//报错 会提示a重复定义

以上text.c可以翻译为如下

int a=1;//#include "text1.h"
int b=2;//#include "text2.h"
int a=1;

#include <iotaream>

int main()
{
	return 0;
}

头文件中常用到

#ifdef, ifndef		条件编译
#ifndef 标识符		//当标识符没有定义过(没有用#define定义过),则对程序进行编译
//程序段
#endif

以上text1.h与text2.h可以改写为如下

#ifdef __text1__
#ifndef __text1__
int a=1;
#endif

#ifdef __text2__
#ifndef __text2__
int b=2;
#endif

这样#inlcude就没问题了,但是通常标识符会后面会加个h__texe1__H_

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值