C语言头文件

.h文件一般放声明,是不放具体函数的

.h文件和.c文件作用

  • xx.h文件 .h中一般放的是同名.c文件中定义的变量、数组、函数的声明,需要让.c外部使用的声明。
  • xx.c文件 .c文件一般放的是变量、数组、函数的具体定义

 

.h文件的格式

     为了防止头文件被重复包含,头文件的编写需要符合一定的格式,比如在开头利用某宏是否被定义来判断是否要包含该头文件。

     假设有一头文件叫example.h,它的编写格式如下:

#ifndef _EXAMPLE_H

#define _EXAMPLE_H

...

...(函数声明)

...

#endif

于此同时,还应该有一个example.c / example.cpp与.h文件对应

例子

function.h

#include<stdio.h>

int add(int a,int b);

function.c

#include<function.h>
 
int add(int a,int b)
{
    return a+b;
}

main.c

# include<stdio.h>
# include<function.h>

int main()
{
   int a = 1,b =2;
   int c = add(a,b);   //这里是对function.c中的add函数的调用
   printf("c=%d",c);

   return 0;   
}

main.c
发布了329 篇原创文章 · 获赞 156 · 访问量 15万+

猜你喜欢

转载自blog.csdn.net/hxxjxw/article/details/104419271
今日推荐