c语言自己写一个.h的头文件

首先放上三段简单的源码

main.c 里面的内容

#include"stdio.h"
#include "lib.h"
int main(){
 int a,b,c;
 printf("请输入a:");
 scanf("%d",&a);
 printf("\n请输入b:");
 scanf("%d",&b);
 printf("\n请输入c:");
 scanf("%d",&c);
 printf("\n%d",max(a,b,c));
}


lib.h  里面的内容

#ifndef LIB_H
#define LIB_H

void displayNarcissistic(void);

int max(int a,int b,int c);

#endif

lib.c  里面的内容

#include "lib.h"
#include "stdio.h"

 int max(int a,int b,int c){
 int temp;
 if (a>b){
  temp = a;
  a = b;
  b = temp;
  if (a>c){
   temp = a;
   a = c;
   c = temp;
  }else if (b>c){
   temp = b;
   b = c;
   c = temp;
  }
 }else if (a<b){
  if (a>c){
   temp = a;
   a = c;
   c = temp;
  }else if (b>c){
   temp = b;
   b = c;
   c = temp;
  }
 }
 return c;
 }


当然你可忽略函数里面写的是什么,然而这样在devc++上的编译结果是这样的


自然,它的意思是说没有找到max这个函数,咦?这是我们的函数写错了吗?当然不是了,我们需要在devc++中新建项目,将三个文件在一个项目下编译运行。过程如图:

《1》

《2》



至此,我们就成功的写了一个自己的头文件,并且用main函数来测试它。




猜你喜欢

转载自blog.csdn.net/Wolf_xujie/article/details/78882617