如何在 C 中编写自己的头文件?\n


我们将了解如何使用 C 创建自己的头文件。要创建一个头文件,我们必须创建一个名称和扩展名为 (*.h) 的文件。该函数中将没有 main() 函数。在该文件中, możemy 某些变量、某些函数等。

要使用该头文件,它应位于程序所在的同一目录中。现在,使用 `#include`,我们必须放入头文件名。名称将被包含在双引号中。`include` 语法将如下所示。

#include”header_file.h”

让我们来看一个程序来获得这个概念。

示例

int MY_VAR = 10;
int add(int x, int y){
   return x + y;
}
int sub(int x, int y){
   return x - y;
}
int mul(int x, int y){
   return x * y;
}
int negate(int x){
   return -x;
}

示例

#include <stdio.h>
#include "my_header.h"
main(void) {
   printf("The value of My_VAR: %d\n", MY_VAR);
   printf("The value of (50 + 84): %d\n", add(50, 84));
   printf("The value of (65 - 23): %d\n", sub(65, 23));
   printf("The value of (3 * 15): %d\n", mul(3, 15));
   printf("The negative of 15: %d\n", negate(15));
}

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

输出

The value of My_VAR: 10
The value of (50 + 84): 134
The value of (65 - 23): 42
The value of (3 * 15): 45
The negative of 15: -15

更新时间:2019 年 7 月 30 日

14,000+ 浏览量

开启您的 职业生涯

完成课程,获得认证

开始学习
广告