lulqs

2007年5月11日

 

stdlib.h 


@
函数名称:     calloc 
函数原型:     void * calloc(unsigned n,unsign size); 
函数功能:     分配n个数据项的内存连续空间,每个数据项的大小为size 
函数返回:     分配内存单元的起始地址,如果不成功,返回
参数说明
所属文件:     <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字节的存储区 
函数返回:     所分配的内存区地址,如果内存不够,返回
参数说明
所属文件:     <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); 
函数功能:     产生032767间的随机整数(00x7fff之间
函数返回:     随机整数 
参数说明
所属文件:     <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) 
 
 
  
 
作者: dragonspear  2006-2-13 10:16   回复此发言    
 
--------------------------------------------------------------------------------
 
函数学习-  
 
函数功能:     返回一个指向环境变量的指针 
函数返回:     环境变量的定义 
参数说明:     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); 
 
 
  
 
作者: dragonspear  2006-2-13 10:16   回复此发言    
 
--------------------------------------------------------------------------------
 
函数学习-  
     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) 
函数功能:     将数值字符串转换成长整形格式数 
 
 
  
 
作者: dragonspear  2006-2-13 10:16   回复此发言    
 
--------------------------------------------------------------------------------
 
函数学习-  
 
函数返回:     长整形转换结果 
参数说明:     s-待转换的浮点字符串,radix-转换的数制,endptr-转换后的尾数字符串 
所属文件:     <stdlib.h> 

#include <stdlib.h> 
#include <stdio.h> 
int main() 

    char *string="87654321",*endptr; 
    long lnumber; 
    lnumber=strtol(string,&endptr,10); 
    printf("string=%s long=%ld",string,lnumber); 
    return 0; 



@
函数名称:     swab 
函数原型:     void swab(char *from,char *to,int num) 
函数功能:     交换字符串中的相邻的偶数字节和奇数字节的位置 
函数返回
参数说明:     from-待变换的字符串,num转换字节数(必须偶),to-变换后的字符串 
所属文件:     <stdlib.h> 

#include <stdlib.h> 
#include <stdio.h> 
#include <string.h> 
char source[13]="IWy@seyk( )c"; 
char target[13]; 
int main() 

   swab(source,target,strlen(source)); 
    printf("This is target: %s",target); 
    return 0; 



@
函数名称:     lfind 
函数原型:     void *lfind(const void *key,const void *base,unsigned *num, unsigned width,int (*compare)(const void *element1,const void *element2)); 
函数功能:     base指向的数组上进行线性搜索 
函数返回:     返回指向找到元素的指针或空 
参数说明:     key-搜索对象,base-数组,num-元素个数,width-元素长度,compare-指向用户定义的用于比较元素的函数(相等返回0) 
所属文件:     <stdlib.h> 
           
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <search.h> 

static const char *keywords[]={"auto","break","case","char", /* . */ "while"}; 

int main(int argc,const char *argv[]) 

    unsigned num=5; 
    extern int compare(const void*,const void*); 
    if(argc<=1)exit(EXIT_FAILURE); 
    if(lfind(&argv[1],keywords,&num,sizeof(char**),compare)==NULL){ 
      printf("'%s' is not a C keyword\n",argv[1]); 
      exit(EXIT_FAILURE); 
    }else{ 
      printf("'%s' is a C keyword\n",argv[1]); 
      exit(EXIT_SUCCESS); 
    } 
    return 0; 


int compare(const void *op1,const void *op2) 

    const char **p1=(const char**)op1; 
    const char **p2=(const char**)op2; 
    return(strcmp(*p1,*p2)); 



@
函数名称:     lsearch 
函数原型:     void *lsearch(const void *key,void *base,unsigned *num, unsigned width,int (*compare)(const void *element1,const void *element2)); 
函数功能:     base指向数组上进行搜索,如不存在则将key添加到数组结尾 
函数返回:     返回指针,指向找到或添加的元素 
参数说明:     key-搜索对象,base-数组,num-元素个数,width-元素长度,compare-指向用户定义函数 
所属文件:     <stdlib.h> 

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

int main(int argc,const char *argv[]) 

    int i; 
    unsigned num=0; 
    char **array=(char**)calloc(argc,sizeof(char**)); 
    extern int compare(const void*,const void*); 
    for(i=1;i<argc;++i){ 
      lsearch(&argv[i],array,&num,sizeof(char**),compare); 
    } 
    for(i=0;i<num;++i){ 
      printf("%s\n",array[i]); 
    } 
    return 0; 


int compare(const void *op1,const void *op2) 

    const char **p1=(const char **)op1; 
    const char **p2=(const char **)op2; 
    return(strcmp(*p1,*p2)); 



@
函数名称:     qsort 
函数原型:     void qsort(void *base,size_t num,size_t width,int (*fcmp)(const void *,const void *) 
 
 
  
 
作者: dragonspear  2006-2-13 10:16   回复此发言    
 
--------------------------------------------------------------------------------
 
函数学习-  
 
函数功能:     使用快速排序法对数组base进行排序 
函数返回
参数说明:     base-待排序数组,num-数组元素个数,width-每个元素大小,fcmp-用于对数组元素进行比较的函数指针,该函数另外编写,有2个参数
所属文件:     <stdlib.h> 

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
int sort_function( const void *a,const void *b); 
    char list[5][4]={"cat","car","cab","cap","can"}; 
int main() 

    int x; 
    qsort((void *)list,5,sizeof(list[0]),sort_function); 
    for (x=0; x < 5; x++) 
        printf("%s",list[x]); 
    return 0; 


int sort_function( const void *a,const void *b) 

    return( strcmp(a,b) ); 



@
函数名称:     bsearch 
函数原型:     int *bsearch(cost void *key,const void *base,size_t n,size_t size,int(*cmp)(const void*,const void*)); 
函数功能:     二分法检索 
函数返回
参数说明:     bsearch将在数组base(该数组到少有n个指定大小的元素)中检索key所引用的对象.依据比较函数cmp(cmp函数会返回一个整数,根据key所引用的对象是否小于,等于或者大于数组元素,返回的这个整数将大于,等于或小于0),该数组必须按升序排列.如果bsearch发现了一个匹配对象,则返回该对象所在的数组的地址;否则函数返回空指针 

所属文件:     #include<stdlib.h> 

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

static const char *keywords[]={"auto","break","case", "char","/* . */","while"}; 

#define NUM_KW sizeof(keywords)/sizeof(char*) 

int kw_compare(const void *p1, const void *p2) 

    const char *p1c=(const char*)p1; 
    const char **p2c=(const char**)p2; 
    return(strcmp(p1c,*p2c)); 


int keyword_lookup(const char *name) 

    const char **key; 
    key=(char const **)bsearch(name,keywords,NUM_KW,sizeof(char*),kw_compare); 
    if(key==NULL)return(-1); 
    return key-keywords; 


int main() 

    printf("%d\n",keyword_lookup("case")); 
    printf("%d\n",keyword_lookup("yesky")); 
    printf("%d\n",keyword_lookup("auto")); 
    return 0; 



@
函数名称:     div 
函数原型:     div_t div(int num,int denom) 
函数功能:     整除 
函数返回:     返回一个值,类型为 
              typedef struct div_t{ 
                 int quot; 
                 int rem; 
              }div_t; 
参数说明:     该返回值包括num除以denom所得的商及余数 
所属文件:     <stdlib.h> 

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

int print_time(int seconds) 

     div_t  min_sec; 
     min_sec=div(seconds,60); 
     printf("It took %d minutes and %d seconds\n",min_sec.quot, min_sec.rem); 


int main() 

    print_time(130); 
    return 0; 



@
函数名称:     ldiv 
函数原型:     ldiv_t ldiv(long numer,long denom) 
函数功能:     计算numer/denom的商和余数 
函数返回:     商和余数的格式如下: 
        typedef struct{ 
         long x;/*
商值*/ 
         long y;/*
余数值*/ 
        }ldiv_t; 
参数说明
所属文件:     <stdlib.h> 

#include <stdlib.h> 
#include <stdio.h> 
int main() 

    ldiv_t lx; 
    lx=ldiv(100000L,30000L); 
    printf("100000 div 30000=%ld remainder %ld",lx.quot,lx.rem); 
    return 0; 



@
函数名称:     random 
函数原型:     int random(int num) 
函数功能:     得到一个在0和参数num之间的随机数 
函数返回:     一个在0和参数num之间的随机数 
参数说明:     num-最大的随机数取值 
所属文件:     <stdlib.h> 

#include <stdlib.h> 
#include <stdio.h> 
#include <time.h> 
int main() 

    randomize(); 
    printf("Random number in the 0-99 range: %d",random (100)); 
    return 0; 



@
函数名称:     randomize 
函数原型:     int randomize(void) 
函数功能:     初始化随机数发生器  
函数返回:     一个随机数 
参数说明
所属文件:     <stdlib.h>,<time.h> 

#include <stdlib.h> 
#include <stdio.h> 
#include <time.h> 
int main() 

    int i; 
    randomize(); 
    printf("Ten random numbers from 0 to 99"); 
    for(i=0;i<10;i++) 
        printf("%d",rand()%100); 
    return 0; 



@
函数名称:     srand 
函数原型:     void srand(unsigned seed) 
函数功能:     该函数和rand函数配合使用,产生随机数的起始发生数据 
函数返回
参数说明:     seed-任意数值 
所属文件:     <stdlib.h> 

#include <stdlib.h> 
#include <stdio.h> 
#include <time.h> 
int main() 

    int i; 
    time_t t; 
    srand((unsigned) time(&t)); 
    printf("Ten random numbers from 0 to 99"); 
    for(i=0;i<10;i++) 
        printf("%d",rand()%100); 
    return 0; 



@
函数名称:     system 
函数原型:     int system(const char *command) 
函数功能:     调用DOS命令 
函数返回
参数说明
所属文件:     <stdlib.h> 

#include <stdlib.h> 
#include <stdio.h> 
int main() 

    system("dir"); 
    return 0; 
}  

posted @ 2007-05-11 12:33 qq 阅读(413) | 评论 (0)编辑 收藏

2007年5月10日

isdigit
原型:extern int isdigit(int c);
 
  用法:#include <ctype.h>
 
  功能:判断字符c是否为数字
 
  说明:当c为数字0-9时,返回非零值,否则返回零。
 
  举例:

      // isdigit.c
     
      #include <syslib.h>
      #include <ctype.h>

      main()
      {
        int c;
       
        clrscr();        // clear screen
        c='a';
        printf("%c:%s\n",c,isdigit(c)?"yes":"no");
        c='9';
        printf("%c:%s\n",c,isdigit(c)?"yes":"no");
        c='*';
        printf("%c:%s\n",c,isdigit(c)?"yes":"no");
        getchar();
        return 0;
      }
posted @ 2007-05-10 12:11 qq 阅读(4158) | 评论 (1)编辑 收藏
仅列出标题  

导航

<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

统计

常用链接

留言簿(1)

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜