C语言的常用库函数

1 calloc() realloc()

calloc()函数也可以从堆中分配内存,这个函数会对分配好的内存进行清0。calloc()如果调整失败则返回NULL。
realloc()函数可以调整堆中分配的内存大小,realloc的调整不一定在原地完成,调整完成后只需要释放新的内存空间。

#include <stdio.h>
#include <stdlib.h>

int main() {
    int pos = 0;
    //int *p_num = (int *)calloc(5, sizeof(int));
    int *p_num = (int *)malloc(5 * sizeof(int));
    if (!p_num) {
        printf("内存分配失败\n");
        return 0;
    }
    for (pos = 0;pos <= 4;pos++) {
        printf("%d ", *(p_num + pos));
    }
    printf("\n");
    free(p_num);
    p_num = NULL;
    return 0;
}
#include <stdio.h>
#include <stdlib.h>

int main() {
    int *p_num = (int*)malloc(3 * sizeof(int));
    int *p_num1 = NULL;
    if (!p_num) {
        printf("分配内存失败\n");
        return 0;
    }
    p_num1 = (int *)realloc(p_num, 5 * sizeof(int));
        if (p_num1) {
        p_num = p_num1;
    }
    free(p_num);
    p_num = NULL;
    return 0;
}

2 函数指针

  函数指针用来记录代表函数的地址数据,函数指针可以用来直接调用一个函数。把函数声明中的函数名改成指针变量声明就得到函数指针的声明语句,函数名称代表函数的首地址。函数指针可以作为函数的参数使用,这样可以提高函数的灵活性。

#include <stdio.h>

typedef struct stru {
    int num;
    void (*p_func)(struct stru *);
} stru;

void inc(stru *p_stru) {
    p_stru->num++;
}

void dec(stru *p_stru) {
    p_stru->num--;
}

void neg(stru *p_stru) {
    p_stru->num = 0 - p_stru->num;
}

int main() {
    stru arr[] = {{2, inc}, {2, dec}, {2, neg}};
    int pos = 0;
    for (pos = 0;pos <= 2;pos++) {
        arr[pos].p_func(arr + pos);
    }
    for (pos = 0;pos <= 2;pos++) {
        printf("%d ", arr[pos].num);
    }
    printf("\n");
    return 0;
}
#include <stdio.h>

int comp(int num, int num1) {
    if (num > num1) {
        return 1;
    }
    else if (num < num1) {
        return -1;
    }
    else {
        return 0;
    }
}

int comp1(int num, int num1) {
    return 0 - comp(num, num1);
}

void sort(int *p_num, int size, int (*p_func)(int, int)) {
    int pos = 0, pos1 = 0;
    for (pos = size - 1;pos >= 1;pos--) {
        for (pos1 = 0;pos1 <= pos - 1;pos1++) {
            if (p_func(*(p_num + pos1), *(p_num + pos1 + 1)) < 0)
            {
                int temp = *(p_num + pos1);
                *(p_num + pos1) = *(p_num + pos1 + 1);
                *(p_num + pos1 + 1) = temp;
            }
        }
    }
}

int main() {
    int num[] = {3, 7, 5, 9, 1}, pos = 0;
    sort(num, 5, comp);
    for (pos = 0;pos <= 4;pos++) {
        printf("%d ", num[pos]);
    }
    printf("\n");
    sort(num, 5, comp1);
    for (pos = 0;pos <= 4;pos++) {
        printf("%d ", num[pos]);
    }
    printf("\n");
    return 0;
}

3 qsort()

  qsort()函数可以采用快速排序算法把一个数组中的所有数据按照从小到大的顺序排列好,需要提供一个用来比较两个数字的函数,这个比较函数的两个参数各代表数组中的一个变量的地址。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int comp(const void *p_num, const void *p_num1) {
    const int *p_int = (const int*)p_num;
    const int *p_int1 = (const int*)p_num1;
    if (*p_int > *p_int1) {
        return 1;
    }
    else if (*p_int < *p_int1) {
        return -1;
    }
    else {
        return 0;
    }
}

int strcomp(const void *p_num, const void *p_num1) {
    const char ** pp_str = (const char**)p_num;
    const char ** pp_str1 = (const char**)p_num1;
    return strcmp(*pp_str, *pp_str1);
}

int main() {
    int num[] = {5, 9, 3, 8, 1}, pos = 0;
    char *strs[] = {"China", "Russia", "France", "England", "America"};
    qsort(num, 5, sizeof(int), comp);
    for (pos = 0;pos <= 4;pos++) {
        printf("%d ", num[pos]);
    }
    printf("\n");
    qsort(strs, 5, sizeof(char*), strcomp);
    for (pos = 0;pos <= 4;pos++) {
        printf("%s\n", strs[pos]);
    }
    return 0;
}

4 pow() sqrt()

pow()函数可以计算幂,需要包含头文件math.h,编译时需要加选项-lm。
sqrt()函数可以计算平方根。

5 atoi() atof()

atoi()函数可以把字符串中的整数提取出来放在返回值变量中。
atof()函数可以把字符串中的浮点数提取出来放在double类型的返回值变量中。
需要包含头文件stdlib.h。

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main() {
    printf("pow(2,3)是%lg\n", pow(2, 3));
    printf("sqrt(36)是%lg\n", sqrt(36));
    printf("atoi(\"123 5\")是%d\n", atoi("123 5"));
    printf("atof(\"12.3 5\")是%lg\n", atof("12.3 5"));
    return 0;
}

6 printf scanf fprintf fscanf sprintf sscanf

printf/scanf这两个函数可以格式化的从键盘读数据或向屏幕写数据
fprintf/fscanf这两个函数可以格式化的从文件读数据或向文件写数据,这两个函数的第一个参数都是文件指针
sprintf/sscanf这两个函数可以格式化的从字符串中读数据或向字符串中写数据。这两个函数的第一个参数都是一个字符串

7 sleep()

sleep()函数可以让程序休眠一段时间 sleep(1)//让电脑休息一分钟

8 remove()

remove()函数可以删除一个文件

#include <stdio.h>
int main() {
    remove("b.bin");
    return 0;
}

9 srand() time()

  srand()标准函数用于设置随机数种子,只使用一次,rand标准函数用于获得下一个随机数,这两个标准函数都需要包含stdlib.h文件。
  time()标准函数用于获得从1970年1月1日00:00:00时刻开始到现在所经过的秒数,该系统时间可以作为随机数种子使用,这个标准函数需要包含time.h文件。

#include <stdio.h>
#include <time.h>
#include <unistd.h>

int main() {
    int start = time(0);
    while (1) {
        printf("%04ld\r", time(0) - start);
        fflush(stdout);
        sleep(1);
    }
    return 0;
}

10 输出缓冲区

输出缓冲区显示条件:
  1、遇到\n
  2、函数结束时
  3、输出缓冲区满啦
  4、fflush(stdout) 刷新输出缓冲区,把输出缓冲区的内容打印到标准输出设备上

#include <stdio.h>

int main() {
    printf("Hello");
    fflush(stdout);
    while (1);
    return 0;
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值