C 语言中的头文件

1、C 语言中 include <> 与include “” 的区别?

#include < > 引用的是编译器的类库路径里面的头文件。

#include " " 引用的是你程序目录的相对路径中的头文件,如果在程序目录没有找到引用的头文件则到编译器的类库路径的目录下找该头文件。

2、C 语言中的标准库头文件

#include <stdio.h>
#include <math.h>
头文件功能简介
<stdio.h>标准输入输出库,包含 printf、scanf 等函数
<stdlib.h>标准库函数,包含内存分配、程序控制等函数
<string.h>字符串操作函数,如 strlen、strcpy 等
<math.h>数学函数库,如 sin、cos、sqrt 等
<time.h>时间和日期函数,如 time、strftime 等
<ctype.h>字符处理函数,如 isalpha、isdigit 等
<limits.h>定义各种类型的限制值,如 INT_MAX 等
<float.h>定义浮点类型的限制值,如 FLT_MAX 等
<assert.h>断言宏 assert,用于调试检查
<errno.h>定义错误码变量 errno 及相关宏
<stddef.h>定义通用类型和宏,如 size_t、NULL 等
<signal.h>处理信号的函数和宏,如 signal 等
<setjmp.h>提供非本地跳转功能的宏和函数
<locale.h>地域化相关的函数和宏,如 setlocale 等

3、C 语言中自定义头文件

3.1、举第1个栗子

在同一目录下编写如下代码测试

vim test.h
===========================
void hello(void);
vim test.c
===========================
#include <stdio.h>
#include "test.h"

void hello(void) {
    printf("hello world\n");
}
vim testDemo.c
===========================
#include "test.h"

int main() {
    hello(); //hello world
    return 0;
}

3.2、举第2个栗子

vim michael_math_operations.h
===================================
#ifndef MICHAEL_MATH_OPERATIONS_H

// 宏定义
#define MICHAEL_MATH_OPERATIONS_H
#define PI 3.14159
#define SQUARE(x) ((x) * (x))

// 函数声明 加法
int michael_add(int a, int b);
// 函数声明 减法
int michael_subtract(int a, int b);
// 函数声明 乘法
int michael_multiply(int a, int b);
// 函数声明 除法
double michael_divide(int a, int b);

#endif // MICHAEL_MATH_OPERATIONS_H
vim michael_math_operations.c
===================================
#include "michael_math_operations.h"

int michael_add(int a, int b) {
    return a + b;
}

int michael_subtract(int a, int b) {
    return a - b;
}

int michael_multiply(int a, int b) {
    return a * b;
}

double michael_divide(int a, int b) {
    if (b == 0) {
        return 0.0; // 避免除以零
    }
    return (double) a / b;
}
vim math_test.c
===================================
#include "math_operations.h"
#include <stdio.h>

int main() {
    int a = 5, b = 3;
    printf("PI = %f\n", PI);
    printf("SQUARE(11) = %f\n", SQUARE(11));
    printf("%d + %d = %d\n", a, b, michael_add(a, b));
    printf("%d - %d = %d\n", a, b, michael_subtract(a, b));
    printf("%d * %d = %d\n", a, b, michael_multiply(a, b));
    printf("%d / %d = %.2f\n", a, b, michael_divide(a, b));
    return 0;
}
===================================
PI = 3.141590
SQUARE(11) = 0.000000
5 + 3 = 8
5 - 3 = 2
5 * 3 = 15
5 / 3 = 1.67
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值