c的一些函数(转)

来源:互联网 发布:瑞典隐身战斗机知乎 编辑:程序博客网 时间:2024/06/06 03:14

转:http://www.cppblog.com/lulqs/

stdlib.h


@函数名称:      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)


  

--------------------------------------------------------------------------------

2 函数学习-转  
函数功能:     返回一个指向环境变量的指针
函数返回:     环境变量的定义
参数说明:      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);



--------------------------------------------------------------------------------

3 函数学习-转  
      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)
函数功能:     将长整形数转换为等价的字符串
函数返回:     指向string的指针
参数说明:      value-转换的长整形数,radix-数制(如10表示十进制)
所属文件:      <stdlib.h>

#include <stdlib.h>
#include <stdio.h>
int main()
{
     long number=12345;
     char string[25];
     itoa(number,string,10);
     printf("integer=%d string=%s",number,string);
     return 0;
}


@函数名称:      itoa
函数原型:      char *itoa(int value,char *string,int radix)
函数功能:     将整形数value转换为其等价的字符串
函数返回:     指向string的指针
参数说明:      value-要转化的数值,radix-转换的进制,如10表示按十进制转换         
所属文件:      <stdlib.h>

#include <stdlib.h>
#include <stdio.h>
int main()
{
     int number=123456L;
     char string[25];
     ltoa(number,string,10);
     printf("integer=%ld string=%s",number,string);
     return 0;
}


@函数名称:      strtod
函数原型:      double strtod(const char *s,char **endptr)
函数功能:     将浮点字符串转换成双精度格式数
函数返回:     双精度转换结果
参数说明:      s-待转换的浮点字符串,endptr-转换后的尾数字符串
所属文件:      <stdlib.h>

#include <stdio.h>
#include <stdlib.h>
int main()
{
     char input[80],*endptr;
     double value;
     printf("Enter a floating point number:");
     gets(input);
     value=strtod(input,&endptr);
     printf("The string is %s the number is %lf",input,value);
     return 0;
}


@函数名称:      strtol
函数原型:      long strtol(const char *s,char **endptr,int radix)
函数功能:     将数值字符串转换成长整形格式数

--------------------------------------------------------------------------------

4 @

0 0