C标准库——<stdlib.h>

222 阅读4分钟

常用变量

name comment
size_t sizeof运算符产生的数据类型,一般是一个无符号整数
wchar_t 一个宽字符(多个字节的字符)的大小
NULL
RAND_MAX rand()返回的最大值

内存管理函数

name comment
void *malloc(size_t size) 从堆上动态分配内存空间
void free(void * ptr) 释放之前分配的空间

实操

  • 内存分配在链表中的引用——malloc(),free()
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
typedef struct Student{
    char name[10];
    int id;
    int score;
    struct Student *next;
}st;
st *create() {
    st *head, *current, *new;
    char name[10];
    char flag;
    int id;
    int score;
    printf("输入姓名 ID 分数 #结束\n");
    head = (st *) malloc(sizeof(st));
    scanf("%s%d%d", name, &id, &score);
    strcpy(name, head->name);
    head->id = id;
    head->score = score;
    current = head;
    flag = getchar();
    while (flag != '#') {
        printf("输入姓名 ID 分数 #结束\n");
        new = (st *) malloc(sizeof(st));//为new结构体指针分配内存,即用即分配
        scanf("%s%d%d", name, &id, &score);
        strcpy(name, new->name);
        new->id = id;
        new->score = score;
        current->next = new;
        flag = getchar();
    }
    return head;
}
int main(){
    st * p;
    p = create();
    while (p!=NULL)
    {
        printf("%s%d%d",p->name,p->id,p->score);
        p=p->next;
    }
    return 0;
}

int del_node(st *del_up,st *del){
    del_up->next = del->next;
    free(del);//删除链表空间
}

数学函数

name comment
int abs(int j) 返回int 数据类型的绝对值
long labs(long int j) 返回long int数据类型的绝对值
int rand(void) 返回一个0~RANDMAX质检的随机数
void srand(unsigned int seek) 将参数作为rand函数返回值的起始值
  • 常见用法:srand(time(NULL))意思是以现在的系统时间作为随机数的种子来产生随机数!至于NULL这个参数,很简单因为这个话就返回到现在时间时候的long秒,只有设置成NULL才能获得系统的时间;time(NULL)会返回从1970年1月1日0:0:0至今的秒数.
  • 实操——rand(),srand()
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(){
    srand(time(NULL));
    printf("%d",rand());
    return 0;
}
name comment
int atoi(char *str) 将字符串参数转换为int类型
long atol(char *str) 将字符串参数转换为long int 类型
double atof(char *str) 将字符串参数转换为float 类型

环境函数

name comment
int system(const char *comment) 执行Shell(或命令行)命令
char *getenv (const char *name) 搜索 name 所指向的环境字符串,并返回相关的值给字符串。
int exit(int status) 结束进程,0为正常退出,非0为非正常退出
  • 实操——输出当前目录下的文件"ls"——system()
#include <stdio.h>
#include <string.h>
#include<stdlib.h>
 
int main ()
{
   char command[50];
 
   strcpy( command, "ls" );
   system(command);
 
   return(0);
}
  • 实操——获取环境变量中的值——getnev()
#include <stdio.h>
#include <stdlib.h>

int main ()
{//环境变量(environment variables)一般是指在操作系统中用来指定操作系统运行环境的一些参数

   printf("PATH : %s\n", getenv("PATH"));
   printf("HOME : %s\n", getenv("HOME"));
   printf("ROOT : %s\n", getenv("ROOT"));
//PATH HOME ROOT均为环境变量
   return(0);
}

搜索和排序函数

name comment
void qsort(void *base, size_t nitems, size_t size, int (*compar)(const void *, const void *)) 快速排序算法
void * bsearch(const void * key, const void * base, size_t nmemb, size_t size, int (* compar)(const void *, const void *)) 在数组进行二分法查找某一元素,要求数组预先已排好序

qsort参数解释

  • base -- 指向要排序的数组的第一个元素的指针。
  • nitems -- 由 base 指向的数组中元素的个数。
  • size -- 数组中每个元素的大小,以字节为单位。
  • compar -- 用来比较两个元素的函数。

bsearch参数解释

  • key -- 指向要查找的元素的指针,类型转换为 void*。
  • base -- 指向进行查找的数组的第一个对象的指针,类型转换为 void*。
  • nitems -- base 所指向的数组中元素的个数。
  • size -- 数组中每个元素的大小,以字节为单位。
  • compar -- 用来比较两个元素的函数。

实操——qsort(),bsearch()

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

int values[] = { 88, 56, 100, 2, 25 };

int cmpfunc (const void * a, const void * b)
{
       return ( *(int*)a - *(int*)b );
    }
// 比较大小:如果compar返回值小于0(< 0),那么a所指向元素会被排在b所指向元素的前面;
// &emsp;&emsp;如果compar返回值等于0(= 0),那么a所指向元素与b所指向元素的顺序不确定;
//&emsp;&emsp;如果compar返回值大于0(> 0),那么a所指向元素会被排在b所指向元素的后面。
    int main()
    {
        int n;

        printf("排序之前的列表:\n");
        for( n = 0 ; n < 5; n++ ) {
            printf("%d ", values[n]);
        }

        qsort(values, 5, sizeof(int), cmpfunc);

        printf("\n排序之后的列表:\n");
        for( n = 0 ; n < 5; n++ ) {
            printf("%d ", values[n]);
        }
        printf("\n");
        int *item;
        int key = 56;
        item = bsearch(&key,values,5, sizeof(int),cmpfunc);
        if(item !=NULL){
            printf("%d",*item);
        } else
            printf("find error");

        return(0);
    }