C语言基础库函数

C语言基础库函数大全主要包括以下类别和函数:

  1. 标准输入输出函数
  • printf:输出函数,用于格式化输出数据。
printf("Hello, World!\n");
  • scanf:输入函数,用于格式化输入数据。
int num;
scanf("%d", &num);
  1. 内存管理函数
  • malloc:动态分配内存。
int *ptr = (int*)malloc(10 * sizeof(int));
  • free:释放之前分配的内存。
free(ptr);
  1. 数学函数
  • sqrt:计算平方根。
double x = 16.0;
double result = sqrt(x);
  • pow:计算x的y次幂。
double base = 2.0;
double exponent = 3.0;
double result = pow(base, exponent);
  1. 字符串处理函数
  • strcpy:复制字符串。
char dest[50];
char src[] = "Hello";
strcpy(dest, src);
  • strcat:连接字符串。
char dest[50] = "Hello";
char src[] = "World";
strcat(dest, src);
  1. 字符处理函数
  • islower:检查字符是否为小写字母。
char ch = 'a';
if (islower(ch)) {
    // ch 是小写字母
}
  • isupper:检查字符是否为大写字母。
char ch = 'A';
if (isupper(ch)) {
    // ch 是大写字母
}
  1. 转换函数
  • atoi:将字符串转换为整数。
char str[] = "12345";
int num = atoi(str);
  1. 时间和日期处理函数
  • time:获取当前时间。
time_t currentTime;
currentTime = time(NULL);
  • localtime:将时间转换为本地时间。
struct tm *localTime;
localTime = localtime(&currentTime);
  1. 其他常用函数
  • rand:生成随机数。
int random_num = rand();

除了上述函数,C语言标准库还提供了许多其他函数,如文件操作函数、错误处理函数等。

  1. stdio.h库测试例子
#include <stdio.h>

int main() {
    // 测试 printf() 函数
    printf("Hello, World!\n");

    // 测试 scanf() 函数
    int num;
    printf("请输入一个整数: ");
    scanf("%d", &num);
    printf("您输入的整数是: %d\n", num);

    // 测试 fclose() 函数
    FILE *file = fopen("test.txt", "w");
    if (file != NULL) {
        fprintf(file, "This is a test.\n");
        fclose(file);
    } else {
        printf("无法打开文件.\n");
    }

    // 测试 fgetc() 函数
    FILE *inputFile = fopen("test.txt", "r");
    if (inputFile != NULL) {
        int c;
        while ((c = fgetc(inputFile)) != EOF) {
            putchar(c);
        }
        fclose(inputFile);
    } else {
        printf("无法打开文件.\n");
    }

    // 测试 fputs() 函数
    FILE *outputFile = fopen("output.txt", "w");
    if (outputFile != NULL) {
        fputs("This is an output test.\n", outputFile);
        fclose(outputFile);
    } else {
        printf("无法打开文件.\n");
    }

    // 测试 fread() 函数
    int buffer[5];
    FILE *dataFile = fopen("data.bin", "rb");
    if (dataFile != NULL) {
        fread(buffer, sizeof(int), 5, dataFile);
        fclose(dataFile);
        for (int i = 0; i < 5; i++) {
            printf("%d ", buffer[i]);
        }
    } else {
        printf("无法打开文件.\n");
    }

    return 0;
}
  1. stdio.h常用的函数

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值