extern,include .c .h文件区别

extern

  1. A declaration can be done any number of times but definition only once
  2. “extern” keyword can be done any number of times but definition only once.
  3. since functions are visible throughout the progrem by default. The use of extern is not needed in function decaration/definition.Its use is redundant
  4. When extern is used with a variable, it’s only decalrd not defined
  5. As an exception, when an extern variable is decalred with initialization, it is taken as the definition of the variable as well.

c 语言是允许多次定义全局变量的,所以需要用extern 区分是声明还是定义

extern int a; // decalration of global variable
int a; //define a golbal variable a
extern int a = 0; // define a golbal variable a
int a = 0; //define a golbal variable a

include .c

如果include一个.c文件,会造成重复定义,因为#include本质是替换,造成在引用的文件中定义一次,然后在当前文件中再次定义

include.h

.h文件通常保存的是declaration(不分配内存的部分),定义在c文件中定义
#include会在预编译期间直接将.h文件的内容进行替换展开。本质就是在.c文件的前边将所有声明替换对应#include语句
在.h文件中定义,大概率造成重复定义。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值