C语言关于头文件的使用

截图:

image

main.c

//
// Created by clou on 2024/4/29.
//

#include <stdio.h>
#include "main.h" //包含自己实现的头文件

//定义全局变量
int cnt;
int sum;

//定义函数
int func(int x, char y) {
    return 0;
}

//定义函数
int func2(int x, int y) {
    return x + y;
}

int main() {
    int result2 = func2(1, 2);
    printf("%f", result2 + PI);
    return 0;
}

main.h

//
// Created by clou on 2024/4/29.
//

#ifndef C_BASICS_PROJECT_MAIN_H
#define C_BASICS_PROJECT_MAIN_H

//宏定义
#define PI 3.14
//结构体定义
typedef struct student {
    char name[32];
    int age;
} Stu;
//全局变量的声明
extern int cnt; //注意这里是声明,而不是定义
int sum; //这里也是声明 编译时编译器会自动添加extern
//函数的声明
int func(int x, char y);

int func2(int, int); //函数声明时,形参只需要指定数据类型即可

#endif //C_BASICS_PROJECT_MAIN_H

posted @ 2024-04-29 19:10  顺心无忧  阅读(7)  评论(0编辑  收藏  举报