⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stdlib.h

📁 C语言库函数介绍
💻 H
📖 第 1 页 / 共 2 页
字号:
@函数名称:     calloc
函数原型:     void * calloc(unsigned n,unsign size);
函数功能:     分配n个数据项的内存连续空间,每个数据项的大小为size
函数返回:     分配内存单元的起始地址,如果不成功,返回0
参数说明:
所属文件:     <stdlib.h>

#include <stdio.h>
#include <stdlib.h>
int main()
{
   char *str=NULL;
   str=calloc(10,sizeof(char));
   strcpy(str,"Hello");
   printf("String is %s",str);
   free(str);
   return 0;
}


@函数名称:     free
函数原型:     void free(void* p);
函数功能:     释放p所指的内存区
函数返回:
参数说明:     p-被释放的指针
所属文件:    <stdlib.h>

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
    char *str;
    str=malloc(10);
    strcpy(str,"Hello");
    printf("String is %s",str);
    free(str);
    return 0;
}


@函数名称:     malloc
函数原型:     void * malloc(unsigned size);
函数功能:     分配size字节的存储区
函数返回:     所分配的内存区地址,如果内存不够,返回0
参数说明:
所属文件:     <stdlib.h>

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
    char *str;
    if((str=malloc(10))==NULL)
    {
        printf("Not enough memory to allocate buffer");
        exit(1);
    }
    strcpy(str,"Hello");
    printf("String is %s",str);
    free(str);
    return 0;
}


@函数名称:     realloc
函数原型:     void * realloc(void * p,unsigned size);
函数功能:     将p所指出的已分配内存区的大小改为size,size可以比原来分配的空间大或小
函数返回:     返回指向该内存区的指针.NULL-分配失败
参数说明:
所属文件:     <stdlib.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
    char *str;
    str= malloc(10);
    strcpy(str,"Hello");
    printf("String is %s Address is %p",str,str);
    str=realloc(str,20);
    printf("String is %s New address is %p",str,str);
    free(str);
    return 0;
}


@函数名称:     rand
函数原型:     int rand(void);
函数功能:     产生0到32767间的随机整数(0到0x7fff之间)
函数返回:     随机整数
参数说明:
所属文件:     <stdlib.h>

#include <stdlib.h>
#include <stdio.h>
int main()
{
    int i;
    printf("Ten random numbers from 0 to 99");
    for(i=0;i<10;i++)
        printf("%d",rand()%100);
    return 0;
}


@函数名称:     abort
函数原型:     void abort(void)
函数功能:     异常终止一个进程.
函数返回:
参数说明:
所属文件:     <stdio.h>,<stdlib.h>                                                                                                                                   

#include <stdio.h>
#include <stdlib.h>
int main()
{
    printf("call abort()");
    abort();
    return 0;
}


@函数名称:     exit
函数原型:     void exit(int state)
函数功能:     程序中止执行,返回调用过程
函数返回:
参数说明:     state:0-正常中止,非0-非正常中止
所属文件:     <stdlib.h>

#include <stdlib.h>
#include <conio.h>
#include <stdio.h>
int main()
{
    int status;
    printf("put a key\n");
    status=getch();
    exit(0);
    return 0;
}


@函数名称:     getenv
函数原型:     char* getenv(const char *name)
函数功能:     返回一个指向环境变量的指针
函数返回:     环境变量的定义
参数说明:     name-环境字符串
所属文件:     <stdlib.h>

#include <stdlib.h>
#include <stdio.h>
int main()
{
    char *s;
    s=getenv("COMSPEC");
    printf("Command processor:%s",s);
    return 0;
}


@函数名称:     putenv
函数原型:     int putenv(const char *name)
函数功能:     将字符串name增加到DOS环境变量中
函数返回:     0:操作成功,-1:操作失败
参数说明:     name-环境字符串
所属文件:     <stdlib.h>

#include <stdio.h>
#include <stdlib.h>
#include <alloc.h>
#include <string.h>
#include <dos.h>
int main()
{
    char *path,*ptr;
    int i=0;
    ptr=getenv("PATH");
    path=malloc(strlen(ptr)+15);
    strcpy(path,"PATH=");
    strcat(path,ptr);
    strcat(path,";c:\temp");
    putenv(path);
    while (environ[i])
        printf("%s",environ[i++]);
    return 0;
}


@函数名称:     labs
函数原型:     long labs(long num)
函数功能:     求长整型参数的绝对值
函数返回:     绝对值
参数说明:
所属文件:     <stdlib.h>

#include <stdio.h>
#include <math.h>
int main()
{
    long result;
    long x=-12345678L;
    result= labs(x);
    printf("number: %ld abs value: %ld",x,result);
    return 0;
}


@函数名称:     atof
函数原型:     double atof(char *str)
函数功能:     将字符串转换成一个双精度数值
函数返回:     转换后的数值
参数说明:     str-待转换浮点型数的字符串
所属文件:     <stdlib.h>

#include <stdlib.h>
#include <stdio.h>
int main()
{
    float f;
    char *str="12345.67";
    f=atof(str);
    printf("string=%s float=%f",str,f);
    return 0;
}


@函数名称:     atoi
函数原型:     int atoi(char *str)
函数功能:     将字符串转换成一个整数值
函数返回:     转换后的数值
参数说明:     str-待转换为整型数的字符串
所属文件:     <stdlib.h>

#include <stdlib.h>
#include <stdio.h>
int main()
{
    int n;
    char *str ="12345.67";
    n=atoi(str);
    printf("string=%s integer=%d",str,n);
    return 0;
}


@函数名称:     atol
函数原型:     long atol(char *str)
函数功能:     将字符串转换成一个长整数
函数返回:     转换后的数值
参数说明:     str-待转换为长整型的字符串
所属文件:     <stdlib.h>

#include <stdlib.h>
#include <stdio.h>
int main()
{
    long l;
    char *str ="98765432";
    l=atol(lstr);
    printf("string=%s integer=%ld",str,l);
    return(0);
}


@函数名称:     ecvt
函数原型:     char *ecvt(double value,int ndigit,int *dec,int *sign)
函数功能:     将浮点数转换为字符串
函数返回:     转换后的字符串指针
参数说明:     value-待转换底浮点数,ndigit-转换后的字符串长度
所属文件:     <stdlib.h>

#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main()
{
    char *string;
    double value;
    int dec,sign;
    int ndig=10;
    clrscr();
    value=9.876;
    string=ecvt(value,ndig,&dec,&sign);
    printf("string=%s dec=%d sign=%d",string,dec,sign);
    value=-123.45;
    ndig= 15;
    string=ecvt(value,ndig,&dec,&sign);
    printf("string=%s dec=%d sign=%d",string,dec,sign);
    value=0.6789e5;
    ndig=5;
    string=ecvt(value,ndig,&dec,&sign);
    printf("string=%s dec=%d sign=%d",string,dec,sign);
    return 0;
}


@函数名称:     fcvt
函数原型:     char *fcvt(double value,int ndigit,int *dec,int *sign)
函数功能:     将浮点数变成一个字符串
函数返回:     转换后字符串指针
参数说明:     value-待转换底浮点数,ndigit-转换后底字符串长度
所属文件:     <stdlib.h>

#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main()
{
    char *string;
    double value;
    int dec,sign;
    int ndig=10;
    clrscr();
    value=9.876;
    string=fcvt(value,ndig,&dec,&sign);
    printf("string=%s dec=%d sign=%d",string,dec,sign);
    value=-123.45;
    ndig=15;
    string=ecvt(value,ndig,&dec,&sign);
    printf("string=%s dec=%d sign=%d",string,dec,sign);
    value=0.6789e5;
    ndig=5;
    string=fcvt(value,ndig,&dec,&sign);
    printf("string=%s dec=%d sign=%d",string,dec,sign);
    return 0;
}


@函数名称:     gcvt
函数原型:     char * gcvt(double value,int ndec,char *buf)
函数功能:     将数值value转换为长度为ndec的字符串
函数返回:     指向buf的指针
参数说明:     value-要转换的浮点数值,ndec-转换后的长度
所属文件:     <stdlib.h>

#include <stdlib.h>
#include <stdio.h>
int main()
{
    char str[25];
    double num;
    int sig=5;
    num=9.876;
    gcvt(num,sig,str);
    printf("string=%s",str);
    num=-123.4567;
    gcvt(num,sig,str);
    printf("string=%s",str);
    num=0.678e5;
    gcvt(num,sig,str);
    printf("string=%s",str);
    return(0);
}


@函数名称:     ltoa
函数原型:     char *ltoa(long value,char *string,int radix)
函数功能:     将长整形数转换为等价的字符串

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -