快速学C语言——第19章:C语言常用开发库

第19章:C语言常用开发库

C语言的标准库提供了丰富的函数来帮助开发者完成各种常见任务。掌握这些标准库的使用可以大大提高编程效率。

⚠️本章只给出日常开发中常用的函数!

19.1 标准输入输出库(stdio.h)

stdio.h 是最常用的库,提供输入输出功能。

19.1.1 基本输入输出函数

c 复制代码
#include <stdio.h>

int main() {
    int age;
    char name[20];

    // 输出函数
    printf("请输入你的姓名: ");
    scanf("%s", name);

    printf("请输入你的年龄: ");
    scanf("%d", &age);

    printf("你好 %s, 你今年 %d 岁\n", name, age);

    return 0;
}

19.1.2 文件操作

c 复制代码
#include <stdio.h>

int main() {
    FILE *file;

    // 写入文件
    file = fopen("test.txt", "w");
    if (file != NULL) {
        fprintf(file, "这是一行文本\n");
        fclose(file);
    }

    // 读取文件
    file = fopen("test.txt", "r");
    if (file != NULL) {
        char buffer[100];
        fgets(buffer, sizeof(buffer), file);
        printf("文件内容: %s", buffer);
        fclose(file);
    }

    return 0;
}

19.2 字符串处理库(string.h)

string.h 提供字符串操作函数。

c 复制代码
#include <stdio.h>
#include <string.h>

int main() {
    char str1[20] = "Hello";
    char str2[20] = "World";
    char result[40];

    // 字符串长度
    printf("str1长度: %zu\n", strlen(str1));

    // 字符串复制
    strcpy(result, str1);
    printf("复制后: %s\n", result);

    // 字符串连接
    strcat(result, " ");
    strcat(result, str2);
    printf("连接后: %s\n", result);

    // 字符串比较
    if (strcmp(str1, str2) == 0) {
        printf("字符串相等\n");
    } else {
        printf("字符串不相等\n");
    }

    return 0;
}

19.3 字符处理库(ctype.h)

ctype.h 提供字符分类和转换函数。

c 复制代码
#include <stdio.h>
#include <ctype.h>

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

    // 字符类型判断
    printf("%c 是字母: %d\n", ch, isalpha(ch));
    printf("%c 是大写: %d\n", ch, isupper(ch));
    printf("%c 是数字: %d\n", ch, isdigit(ch));

    // 字符转换
    char lower = tolower(ch);
    char upper = toupper('b');

    printf("大写转小写: %c\n", lower);
    printf("小写转大写: %c\n", upper);

    return 0;
}

19.4 数学库(math.h)

math.h 提供数学计算函数。

c 复制代码
#include <stdio.h>
#include <math.h>

int main() {
    double x = 4.0;
    double y = 2.5;

    // 基本数学运算
    printf("平方根: %.2f\n", sqrt(x));
    printf("幂运算: %.2f\n", pow(x, y));
    printf("绝对值: %.2f\n", fabs(-3.14));
    printf("向上取整: %.2f\n", ceil(y));
    printf("向下取整: %.2f\n", floor(y));

    return 0;
}

19.5 标准库(stdlib.h)

stdlib.h 提供通用工具函数。

19.5.1 内存分配

c 复制代码
#include <stdio.h>
#include <stdlib.h>

int main() {
    // 动态内存分配
    int *arr = (int*)malloc(5 * sizeof(int));

    if (arr != NULL) {
        for (int i = 0; i < 5; i++) {
            arr[i] = i * 10;
        }

        for (int i = 0; i < 5; i++) {
            printf("%d ", arr[i]);
        }
        printf("\n");

        free(arr);
    }

    return 0;
}

19.5.2 类型转换

c 复制代码
#include <stdio.h>
#include <stdlib.h>

int main() {
    // 字符串转数字
    char str[] = "12345";
    int num = atoi(str);
    printf("字符串转整数: %d\n", num);

    char float_str[] = "3.14";
    double pi = atof(float_str);
    printf("字符串转浮点数: %.2f\n", pi);

    return 0;
}

19.5.3 随机数

c 复制代码
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main() {
    // 设置随机数种子
    srand(time(NULL));

    // 生成随机数
    for (int i = 0; i < 5; i++) {
        printf("随机数: %d\n", rand() % 100); // 0-99的随机数
    }

    return 0;
}

19.6 时间库(time.h)

time.h 提供时间处理函数。

c 复制代码
#include <stdio.h>
#include <time.h>

int main() {
    // 获取当前时间
    time_t current_time;
    time(&current_time);

    printf("当前时间戳: %ld\n", current_time);

    // 转换为可读格式
    char* time_str = ctime(&current_time);
    printf("当前时间: %s", time_str);

    return 0;
}
相关推荐
微笑星河q1 小时前
C++高阶之【多态】
开发语言·c++
青少儿编程课堂4 小时前
第十八届蓝桥杯青少组图形化真题:校园分形雪花绘制解析(全国青少年信息素养大赛·电子学会图形化考级冲刺)
c++·python·算法·bfs·信息学竞赛
ClkLog-开源埋点用户分析4 小时前
【产品说明】【埋点分析】ClkLog 商业版 Java 运行环境安全更新说明
java·开发语言
yuzhiboyouye5 小时前
《阿里巴巴 Java 开发手册》的 MySQL 核心规范
java·开发语言·mysql
郝学胜-神的一滴5 小时前
Effective STL 条款12:对容器线程安全性的现实期望
c语言·开发语言·c++·qt·网络协议·rpc·numpy
geovindu6 小时前
python: KNeighborsClassifier K
开发语言·后端·python·算法
让我们一起加油好吗7 小时前
【数据结构】线段树详解
数据结构·c++·算法·线段树·洛谷
时间的情敌7 小时前
Three.js 射线拾取进阶:hover 高亮、点击弹窗、多物体过滤、穿透拾取
开发语言·javascript·three.js
凌冰_7 小时前
数据结构 - 队列(五)
数据结构·算法·软件工程