C语言 - 常见的标准库头文件

前言

C 语言的标准库头文件提供了丰富的功能,涵盖了输入输出、字符串处理、数学运算、内存管理等多个方面。下面是对各个头文件的详细介绍,包括功能、常见函数或宏的用法示例。


1. <stdio.h> - 标准输入输出库

提供了标准输入(键盘)、标准输出(屏幕)、文件操作等功能。

常见函数

函数说明
printf()向标准输出设备(屏幕)打印格式化字符串
scanf()从标准输入设备(键盘)读取格式化数据
putchar()输出单个字符
getchar()读取单个字符
fgets()读取字符串(防止缓冲区溢出)
puts()输出字符串
fopen()打开文件
fclose()关闭文件
fread()读取文件数据
fwrite()写入数据到文件
fprintf()向文件输出格式化字符串
fscanf()从文件读取格式化数据

示例

#include <stdio.h>  // 标准输入输出库,用于 printf
int main() {
    printf("Hello, World!\n"); // 输出 Hello, World!
    return 0;
}

2. <stdlib.h> - 标准库

提供了动态内存管理、进程控制、类型转换等功能。

常见函数

函数说明
malloc()分配动态内存
calloc()分配动态内存并初始化
realloc()重新分配动态内存
free()释放动态分配的内存
atoi()字符串转换为整数
atof()字符串转换为浮点数
rand()生成随机数
exit()终止程序
system()执行系统命令

示例

#include <stdlib.h>  // 引入标准库
#include <stdio.h>   // 标准输入输出库

int main() {
    // 动态分配 5 个整数大小的内存
    int *ptr = (int *)malloc(sizeof(int) * 5);
    if (ptr == NULL) {
        printf("Memory allocation failed!\n");
        exit(1);  // 终止程序
    }

    // 赋值并打印数组
    for (int i = 0; i < 5; i++) {
        ptr[i] = i * 10;
        printf("ptr[%d] = %d\n", i, ptr[i]);
    }

    free(ptr);  // 释放内存
    return 0;
}

3. <string.h> - 字符串处理

提供了字符串复制、比较、拼接等操作。

常见函数

函数说明
strlen()计算字符串长度
strcpy()复制字符串
strncpy()复制指定长度的字符串
strcmp()比较字符串
strcat()拼接字符串
strchr()查找字符在字符串中的位置
strstr()查找子字符串
memset()用指定值填充内存
memcpy()拷贝内存数据

示例

#include <stdio.h>   // 标准输入输出库
#include <string.h>  // 引入字符串处理库

int main() {
    char str1[20] = "Hello, ";  // 定义字符串
    char str2[] = "World!";
    
    strcat(str1, str2);  // 拼接 str2 到 str1
    printf("Concatenated String: %s\n", str1);  // 输出拼接后的字符串
    
    printf("Length of string: %lu\n", strlen(str1));  // 计算字符串长度
    
    return 0;
}

4. <math.h> - 数学运算

提供了常见数学计算函数,如三角函数、指数函数等。

常见函数

函数说明
sqrt()计算平方根
pow()计算指数
sin(), cos(), tan()三角函数
log(), exp()对数和指数函数
fabs()计算绝对值

示例

#include <stdio.h> // 标准输入输出库,用于 printf
#include <math.h>  // 引入数学库

int main() {
    double x = 16.0;

    // 计算平方根
    printf("Square root of %.2f is %.2f\n", x, sqrt(x));

    // 计算 2 的 3 次方
    printf("2^3 = %.2f\n", pow(2, 3));

    return 0;
}

5. <time.h> - 时间处理

提供了时间计算、格式化等功能。

常见函数

函数说明
time()获取当前时间(秒数)
clock()获取 CPU 运行时间
difftime()计算两个时间的差值
strftime()格式化时间
localtime()将时间转换为本地时间

示例

#include <stdio.h>   // 标准输入输出库,用于 printf
#include <time.h>    // 时间相关的库,提供 time() 和 localtime()

int main() {
    // 获取当前系统时间(从 1970-01-01 00:00:00 UTC 以来的秒数)
    time_t now = time(NULL);

    // 将 time_t 转换为本地时间(受系统时区影响)
    struct tm *beijing_time = localtime(&now);  

    // 格式化输出当前北京时间
    printf("当前北京时间: %04d-%02d-%02d %02d:%02d:%02d\n",
           beijing_time->tm_year + 1900, // 年份从 1900 开始计数,所以要 +1900
           beijing_time->tm_mon + 1,     // 月份从 0 开始计数,所以要 +1
           beijing_time->tm_mday,        // 日期 (1-31)
           beijing_time->tm_hour,        // 小时 (0-23)
           beijing_time->tm_min,         // 分钟 (0-59)
           beijing_time->tm_sec);        // 秒 (0-59)

    return 0;  // 程序正常退出
}

6. <ctype.h> - 字符处理

提供了检查字符类型的函数,如字母、数字等。

常见函数

函数说明
isalpha()是否为字母
isdigit()是否为数字
islower()是否为小写字母
isupper()是否为大写字母
tolower()转换为小写
toupper()转换为大写

示例

#include <stdio.h>  // 标准输入输出库
#include <ctype.h>  // 引入字符处理库

int main() {
    char c = 'A';

    // 将字符转换为小写
    printf("Lowercase of %c is %c\n", c, tolower(c));

    return 0;
}

7. <assert.h> - 断言

用于调试时检查程序状态。

常见宏

说明
assert(expression)如果表达式为假,程序终止

示例

#include <stdio.h>   // 标准输入输出库
#include <assert.h>  // 引入断言库

int main() {
    int x = 10;
    
    // 断言 x 必须大于 0
    assert(x > 0);
    
    printf("断言失败,程序终止...\n");

    return 0;
}

8. <errno.h> - 错误处理

提供了错误码支持。

常见变量

变量说明
errno存储最近的错误代码

示例

#include <stdio.h>  // 标准输入输出库
#include <errno.h>  // 引入错误处理库
#include <string.h>

int main() {
    FILE *fp = fopen("nonexistent.txt", "r");  // 试图打开一个不存在的文件
    if (!fp) {
        printf("Error: %s\n", strerror(errno));  // 输出错误信息
    }
    
    return 0;
}

总结

C 语言的标准库头文件提供了丰富的功能,能够大大提高开发效率。掌握这些库的使用,可以更轻松地处理输入输出、数学运算、字符串处理、文件操作等任务。

(完)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值