C言語ライブラリstdlib.h操作

20857 ワード

C   stdlib.h  
       stdlib.h         :
1	size_t             ,   sizeof       。
2	wchar_t                   。
3	div_t        div        。
4	ldiv_t       ldiv        。


       stdlib.h      :
1	NULL               。
2	EXIT_FAILURE       exit           。
3	EXIT_SUCCESS       exit           。
4	RAND_MAX             rand         。
5	MB_CUR_MAX                         ,     MB_LEN_MAX。


1. atof(str)     String-->Double                                  
double atof(const char *str)     str                (    double  )
              ,           ,    (0.0)


#include 
#include 
#include 


int main()
{
   float val;
   char str[20];
   
   strcpy(str, "956843");
   val = atof(str);
   printf("     = %s,     = %f
", str, val); strcpy(str, "123abc"); // a 123.000000 val = atof(str); printf(" = %s, = %f
", str, val); strcpy(str, "123.321"); val = atof(str); printf(" = %s, = %f
", str, val); return 0; } : = 956843, = 956843.000000 = 123abc, = 123.000000 = 123.321, = 123.320999
2.atoi(str)                                  
int atoi(const char *str)     str               (    int  )。
            ,           ,    。


#include 
#include 
#include 


int main()
{
   int val;
   char str[20];
   
   strcpy(str, "654321");
   val = atoi(str);
   printf("     = %s,     = %d
", str, val); strcpy(str, "1256a4"); val = atoi(str); printf(" = %s, = %d
", str, val); strcpy(str, "a123"); val = atoi(str); printf(" = %s, = %d
", str, val); return 0; } : = 654321, = 654321 = 1256a4, = 1256 = a123, = 0
3.atol(str)
long int atol(const char *str)     str                (    long int  )
            ,           ,    


#include 
#include 
#include 


int main()
{
   long val;
   char str[20];
   
   strcpy(str, "98993489");
   val = atol(str);
   printf("     = %s,      = %ld
", str, val); strcpy(str, "96avda1"); // 96 val = atol(str); printf(" = %s, = %ld
", str, val); strcpy(str, "a*fai"); val = atol(str); printf(" = %s, = %ld
", str, val); return 0; } : = 98993489, = 98993489 = 96avda1, = 96 = a*fai, = 0
4.double strtod(const char *str, char **endptr)    Str --> Double                   str          
double strtod(const char *str, char **endptr)     str                (    double  )。
   endptr                             endptr      
str --               。
endptr --      char*       ,         str           。
               ,           ,    (0.0)


#include 
#include 


int main()
{
  char str[30] = "20.30300 This is test";
   char *ptr;
   double ret;


   ret = strtod(str, &ptr);
   printf("str   (double)  %lf
", ret); printf("str |%s|
", ptr); char str1[30] = "18.032561A130A98 hello world"; ret = strtod(str1, &ptr); printf("str1 (double) %lf
", ret); printf("str1 |%s|", ptr); char str2[30] = "3.21123456789"; ret = strtod(str2, &ptr); printf("str2 (double) %lf
", ret); printf("str2 |%s|", ptr); return 0; } : str (double) 20.303000 str | This is test| str1 (double) 18.032561 str1 |A130A98 hello world| str2 (double) 3.211235 str2 ||
5.long int strtol(const char *str, char **endptr, int base) 
long int strtol(const char *str, char **endptr, int base)  str--->long int
    str              base         (    long int  ),base      2   36(  )  ,       0
str --            。
endptr --      char*       ,         str           
base --   ,     2   36(  )  ,       0    base             str         


#include 
#include 


int main()
{
   char str[30] = "123456789 9hello8 world";
   char *ptr;
   long ret;


   ret = strtol(str, &ptr, 10);
   printf("str   (      )  %ld
", ret); printf("str |%s|
", ptr); char str1[30] = "11 str"; ret = strtol(str1, &ptr, 9); printf("str1 ( ) %ld
", ret); // 9 11 10 printf("str1 |%s|
", ptr); char str2[30] = "32 str"; ret = strtol(str2, &ptr, 0); printf("str2 ( ) %ld
", ret); printf("str2 |%s|
", ptr); return 0; } : str ( ) 123456789 str | 9hello8 world| str1 ( ) 10 str1 | str| str2 ( ) 32 str2 | str|
6.strtoul(const char *str, char **endptr, int base)        str---> unsigned long int 
unsigned long int strtoul(const char *str, char **endptr, int base)     str        
      base            (    unsigned long int  ),base      2   36(  )  ,       0
base --   ,     2   36(  )  ,       0    base             str         


#include 
#include 


int main()
{
   char str[30] = "315 A315";
   char *ptr;
   long ret;


   ret = strtoul(str, &ptr, 10);
   printf("  (      )  %lu
", ret); printf(" |%s|", ptr); char str1[30] = "415 A305"; ret = strtoul(str1, &ptr, 0); printf(" ( ) %lu
", ret); printf(" |%s|", ptr); char str2[30] = "1111 A32647"; ret = strtoul(str2, &ptr, 2); // printf(" ( ) %lu
", ret); // 15 1111 = 15 printf(" |%s|", ptr); return 0; } : ( ) 315 | A315| ( ) 415 | A305| ( ) 15 | A32647|
7.calloc(size_t nitems, size_t size)
void *calloc(size_t nitems, size_t size)          ,           。
malloc   calloc        ,malloc         ,  calloc           
nitems --          。
size --      


#include 
#include 


int main()
{
   int i, n;
   int *a;


   printf("        :");
   scanf("%d",&n);


   a = (int*)calloc(n, sizeof(int)); //                
   printf("   %d    :
",n); for( i=0 ; i < n ; i++ ) { scanf("%d",&a[i]); } printf(" :"); for( i=0 ; i < n ; i++ ) { printf("%d ",a[i]); } return 0; } : :2 2 : 123 456 :123 456
8. void free(void *ptr)
void free(void *ptr)        calloc、malloc   realloc         。
ptr --                ,            malloc、calloc   realloc        。
             ,         。          。


#include 
#include 
#include 


int main()
{
   char *str;


   /*         */
   str = (char *) malloc(15); //   15        malloc                     str 
   strcpy(str, "Hello World"); 
   printf("String = %s,  Address = %u
", str, str); /* */ str = (char *) realloc(str, 25); // str 25 strcat(str, "123456789"); printf("String = %s, Address = %u
", str, str); /* */ free(str); return(0); } : String = Hello World, Address = 7304208 String = Hello World123456789, Address = 7304208
9.malloc(size_t size)             
void *malloc(size_t size)          ,           
size --       ,      。
          ,          。      ,    NULL。


#include 
#include 
#include 


int main()
{
   char *str;


   /*         */
   str = (char *) malloc(15); //  15     
   strcpy(str, "12345678901234");
   printf("String = %s,  Address = %u
", str, str); /* */ str = (char *) realloc(str, 25); strcat(str, "123456"); printf("String = %s, Address = %u
", str, str); free(str); return(0); } : String = 12345678901234, Address = 7042064 String = 12345678901234123456, Address = 7042064
10.realloc(void *ptr, size_t size) 
void *realloc(void *ptr, size_t size)            malloc   calloc      ptr           


#include 
#include 
#include 
int main()
{
   char *str;


   /*         */
   str = (char *) malloc(10);
   strcpy(str, "123456789");
   printf("String = %s,  Address = %u
", str, str); /* */ str = (char *) realloc(str, 25); // strcat(str, "123456789"); printf("String = %s, Address = %u
", str, str); free(str); return 0; } : String = 123456789, Address = 7435280 String = 123456789123456789, Address = 7435280
11. abort()       
void abort(void)       ,          
#include 
#include 

int main ()
{
   FILE *fp;
   
   printf("     nofile.txt
"); fp = fopen( "nofile.txt","r" ); if(fp == NULL) { printf("
"); abort(); // XXXX printf("
"); // } printf(" nofile.txt
"); fclose(fp); return 0; } : nofile.txt -----------------------------
12. int atexit(void (*func)(void))
int atexit(void (*func)(void))         ,        func。
                ,              
func --             
        ,       ,         

#include 
#include 

void functionA ()
{
   printf("    A
"); } int main () { /* */ int ret = atexit(functionA ); printf("ret = %d ...
",ret); printf(" ...
"); return 0; // } : ret = 0 ... ... A

13.
void exit(int status)         。                     
,           SIGCHLD   
status --           。
       

#include 
#include 

int main ()
{
   printf("     ....
"); printf(" ....
"); exit(0); // 0 printf(" ....
"); return 0; } : .... ....

14.getenv(str)   getenv()      envvar        envvar        ,                   
char *getenv(const char *name)    name          ,           
name --            C    
         null       ,              。          ,    NULL

#include 
#include 

int main ()
{
   printf("PATH : %s
", getenv("PATH")); printf("HOME : %s
", getenv("HOME")); printf("ROOT : %s
", getenv("ROOT")); printf("COMSPEC : %s
", getenv("COMSPEC")); printf("LIB : %s
", getenv("LIB")); printf("USER : %s
", getenv("USER")); return 0; } : PATH : C:\Program Files (x86)\Dev-Cpp\MinGW64\bin;C:\Users\zwx320975\Desktop\IT Win+R \129-nTurn\129-nTurn\Goto;C:\Program Files (x86)\Common File \NetSarang;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\S stem32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Citrix\ICAService\;C:\Pro ram Files (x86)\Citrix\System32\;C:\Program Files\TortoiseSVN\bin;C:\Program Fi es\Java\jdk1.5.0_20\bin;C:\Program Files\Java\jdk1.5.0_20\jre;C:\Program Files x86)\Windows Kits\8.1\Windows Performance Toolkit\;C:\Program Files\Microsoft S L Server\110\Tools\Binn\;C:\Program Files (x86)\Microsoft SDKs\TypeScript\1.0\; :\Program Files\Microsoft SQL Server\120\Tools\Binn\ HOME : (null) ROOT : (null) COMSPEC : C:\Windows\system32\cmd.exe LIB : (null) USER : (null)

15. system(str command)    command                             
int system(const char *command)   command                             ,         
command --            C    
      ,      -1,         

#include 
#include 
#include 

int main ()
{
   char command[50];

   strcpy( command, "dir" );   //windows
   //strcpy( command, "ls -l" );   //Linux
   system(command);

   return 0;
} 
    :
 D:\TEMP    

2016/10/24  14:53              .
2016/10/24  14:53              ..
2016/10/20  15:40               241 1.cpp
2016/10/20  15:27           127,205 1.exe
2016/10/20  18:14               254 2.cpp
2016/10/20  18:14           370,513 2.exe
2016/10/21  17:14             1,461 3.cpp
2016/10/21  17:12           369,825 3.exe
2016/10/24  14:53               233 4.cpp
2016/10/24  14:53           370,476 4.exe
2015/11/09  17:53              frameworks
2015/11/04  19:50              HwCamera
2015/11/04  20:04              HwGallery2
2015/12/02  15:55              HwSoundRecorder
2016/10/20  14:28               926    1.cpp
2016/10/20  14:29           372,245    1.exe
2016/10/20  15:06               226    2.rc
              11          1,613,605   
               6      2,545,360,896     
			   

		   
			   
16. void *bsearch(const void *key, const void *base, size_t nitems, size_t size, int (*compar)(const void *, const void *))       
void *bsearch(const void *key, const void *base, size_t nitems, size_t size, int (*compar)(const void *, const void *))
nitems            ,base          ,key         ,
size             。         compar             
key --            ,      void*。             Item   
base --                   ,      void*。        
nitems -- base             。     Length
size --           ,      。   Item     
compar --            。
      ,                   ,     NULL  

#include 
#include 


int cmpfunc(const void * a, const void * b)
{
   return ( *(int*)a - *(int*)b );
}

int values[] = { 5, 20, 29, 32, 63 };

int main ()
{
   int *item;
   int key = 64;  //    Item = NULL  could not be found
   int key1 = 32;  //    Found item = 32
   /*    bsearch()         32 */
   item = (int*) bsearch (&key, values, 5, sizeof (int), cmpfunc); //               
   if( item != NULL ) 
   {
      printf("Found item = %d
", *item); } else { printf("Item = NULL could not be found
"); } item = (int*) bsearch (&key1, values, 5, sizeof (int), cmpfunc); // if( item != NULL ) { printf("Found item = %d
", *item); } else { printf("Item = NULL could not be found
"); } return 0; } : Item = NULL could not be found Found item = 32

17.void qsort(void *base, size_t nitems, size_t size, int (*compar)(const void *, const void*))        
base --                  。
nitems --   base            。
size --           ,      。
compar --            。
         。


#include 
#include 

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

int cmpfunc (const void * a, const void * b)
{
   return ( *(int*)a - *(int*)b );
}

int main()
{
   int n;

   printf("       :
"); for( n = 0 ; n < 5; n++ ) { printf("%d ", values[n]); } qsort(values, 5, sizeof(int), cmpfunc); printf("

"); for( n = 0 ; n < 5; n++ ) { printf("%d ", values[n]); } return 0; } : : 88 56 100 2 25 : 2 25 56 88 100
18.int abs(int x)    x     
x --     
      x     

#include 
#include 

int main ()
{
   int a, b;

   a = abs(100);
   printf("a    = %d
", a); b = abs(-200); printf("b = %d
", b); return 0; } : a = 100 b = 200
19.div_t div(int numer, int denom)     
div_t div(int numer, int denom)   numer(  )   denom(  )

                 ,        ,  div_t:int quot   ; int rem    ;。


#include 
#include 

int main()
{
   div_t output;

   output = div(27, 4);
   printf("(27/ 4)     = %d
", output.quot); printf("(27/4) = %d
", output.rem); output = div(27, 3); printf("(27/ 3) = %d
", output.quot); // printf("(27/3) = %d
", output.rem); // return 0; } : (27/ 4) = 6 (27/4) = 3 (27/ 3) = 9 (27/3) = 0

20. labs(long int x)           
long int labs(long int x)    x     
      x     。

#include 
#include 

int main ()
{
   long int a,b;

   a = labs(65987L);
   printf("a    = %ld
", a); b = labs(-1005090L); printf("b = %ld
", b); return 0; } : a = 65987 b = 1005090
21.div(long int numer, long int denom)     
div_t div(long int numer, long int denom)   numer(  )   denom(  )
                 ,         ,   ldiv_t:long quot  ; long rem   ;。

#include 
#include 

int main ()
{
   ldiv_t output;

   output = ldiv(100000L, 30000L);

   printf("  = %ld
", output.quot); printf(" = %ld
", output.rem); return 0; } : = 3 = 10000

22.  int rand(void)        
int rand(void)         0   RAND_MAX        
RAND_MAX      ,                 ,       32767
           0   RAND_MAX       。

#include 
#include 

int main()
{
   int i, n;
   time_t t;
   
   n = 5;
   
   /*           */
   srand((unsigned) time(&t));

   /*    0   49     5      */
   for( i = 0 ; i < n ; i++ ) {
      printf("%d
", rand() % 50); } return 0; } : 20 1 3 11 38

23.void srand(unsigned int seed)
void srand(unsigned int seed)       rand          
seed --        ,            。
         

#include 
#include 
#include 

int main()
{
   int i, n;
   time_t t;
   
   n = 5;
   
   /*           */
   srand((unsigned) time(&t)); //         

   /*    0   50     5      */
   for( i = 0 ; i < n ; i++ ) {
      printf("%d
", rand() % 50); } return 0; } : 13 11 49 23 43

24.mblen
int mblen(const char *str, size_t n)      str             
str --                 。
n --               。
            ,mblen()      str               。
           ,    0。
               ,                ,    -1

#include 
#include 
#include 

int main()
{
   int len;
   char *pmbnull  = NULL;
   char *pmb = (char *)malloc( MB_CUR_MAX );
   wchar_t *pwc = L"Hi";
   wchar_t *pwcs = (wchar_t *)malloc( sizeof( wchar_t ));


   
   len = mblen( pmb, MB_CUR_MAX );
   printf( "      %x      :%u
", pmb, len ); pmb = NULL; len = mblen( pmb, MB_CUR_MAX ); printf( " %x :%u
", pmb, len ); return 0; } : 717410 ( ) :4294967295 ( ) 0 :0
25.mbstowcs
size_t mbstowcs(schar_t *pwcs, const char *str, size_t n)     str                    pwcs       
pwcs --      wchar_t      ,                     。
str --              。
n --           。
           ,         。              ,    -1  。


#include 
#include 
#include 

int main()
{
   int len;
   char *pmbnull  = NULL;
   char *pmb = (char *)malloc( MB_CUR_MAX );
   wchar_t *pwc = L"Hi";
   wchar_t *pwcs = (wchar_t *)malloc( sizeof( wchar_t ));

   printf("         
"); len = wcstombs( pmb, pwc, MB_CUR_MAX); printf(" %d
", len); printf(" :%#.4x
", pmb); printf("
"); len = mbstowcs( pwcs, pmb, MB_CUR_MAX); printf(" %d
", len); printf(" :%#.4x

", pwcs); return 0; } : 1 :0x6f7410 1 :0x6f7430