简介

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。

库函数

下面是头文件 stdlib.h 中定义的函数:

序号 函数 & 描述
1 double atof(const char *str)
把参数 str 所指向的字符串转换为一个浮点数(类型为 double 型)。
2 int atoi(const char *str)
把参数 str 所指向的字符串转换为一个整数(类型为 int 型)。
3 long int atol(const char *str)
把参数 str 所指向的字符串转换为一个长整数(类型为 long int 型)。
4 double strtod(const char *str, char **endptr)
把参数 str 所指向的字符串转换为一个浮点数(类型为 double 型)。
5 long int strtol(const char *str, char **endptr, int base)
把参数 str 所指向的字符串转换为一个长整数(类型为 long int 型)。
6 unsigned long int strtoul(const char *str, char **endptr, int base)
把参数 str 所指向的字符串转换为一个无符号长整数(类型为 unsigned long int 型)。
7 void *calloc(size_t nitems, size_t size)
分配所需的内存空间,并返回一个指向它的指针。
8 void free(void *ptr)
释放之前调用 calloc、mallocrealloc 所分配的内存空间。
9 void *malloc(size_t size)
分配所需的内存空间,并返回一个指向它的指针。
10 void *realloc(void *ptr, size_t size)
尝试重新调整之前调用 malloccalloc 所分配的 ptr 所指向的内存块的大小。
11 void abort(void)
使一个异常程序终止。
12 int atexit(void (*func)(void))
当程序正常终止时,调用指定的函数 func
13 void exit(int status)
使程序正常终止。
14 char *getenv(const char *name)
搜索 name 所指向的环境字符串,并返回相关的值给字符串。
15 int system(const char *string)
由 string 指定的命令传给要被命令处理器执行的主机环境。
16 void *bsearch(const void *key, const void *base, size_t nitems, size_t size, int (*compar)(const void *, const void *))
执行二分查找。
17 void qsort(void *base, size_t nitems, size_t size, int (*compar)(const void *, const void*))
数组排序。
18 int abs(int x)
返回 x 的绝对值。
19 div_t div(int numer, int denom)
分子除以分母。
20 long int labs(long int x)
返回 x 的绝对值。
21 ldiv_t ldiv(long int numer, long int denom)
分子除以分母。
22 int rand(void)
返回一个范围在 0 到 RAND_MAX 之间的伪随机数。
23 void srand(unsigned int seed)
该函数播种由函数 rand 使用的随机数发生器。
24 int mblen(const char *str, size_t n)
返回参数 str 所指向的多字节字符的长度。
25 size_t mbstowcs(schar_t *pwcs, const char *str, size_t n)
把参数 str 所指向的多字节字符的字符串转换为参数 pwcs 所指向的数组。
26 int mbtowc(whcar_t *pwc, const char *str, size_t n)
检查参数 str 所指向的多字节字符。
27 size_t wcstombs(char *str, const wchar_t *pwcs, size_t n)
把数组 pwcs 中存储的编码转换为多字节字符,并把它们存储在字符串 str 中。
28 int wctomb(char *str, wchar_t wchar)
检查对应于参数 wchar 所给出的多字节字符的编码。

C 库函数 - atof()

描述

C 库函数 double atof(const char *str) 把参数 str 所指向的字符串转换为一个浮点数(类型为 double 型)。

声明

下面是 atof() 函数的声明。

double atof(const char *str)

参数

  • str -- 要转换为浮点数的字符串。

返回值

函数返回转换后的双精度浮点数,如果没有执行有效的转换,则返回零(0.0)。

实例

下面的实例演示了 atof() 函数的用法。

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

int main()
{
   float val;
   char str[20];
   
   strcpy(str, "98993489");
   val = atof(str);
   printf("字符串值 = %s, 浮点值 = %f\n", str, val);

   strcpy(str, "xuebiancheng.cn");
   val = atof(str);
   printf("字符串值 = %s, 浮点值 = %f\n", str, val);

   return(0);
}

让我们编译并运行上面的程序,这将产生以下结果:

字符串值 = 98993489, 浮点值 = 98993488.000000
字符串值 = xuebiancheng.cn, 浮点值 = 0.000000

C 库函数 - atoi()

描述

C 库函数 int atoi(const char *str) 把参数 str 所指向的字符串转换为一个整数(类型为 int 型)。

声明

下面是 atoi() 函数的声明。

int atoi(const char *str)

参数

  • str -- 要转换为整数的字符串。

返回值

该函数返回转换后的长整数,如果没有执行有效的转换,则返回零。

实例

下面的实例演示了 atoi() 函数的用法。

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

int main()
{
   int val;
   char str[20];
   
   strcpy(str, "98993489");
   val = atoi(str);
   printf("字符串值 = %s, 整型值 = %d\n", str, val);

   strcpy(str, "xuebiancheng.cn");
   val = atoi(str);
   printf("字符串值 = %s, 整型值 = %d\n", str, val);

   return(0);
}

让我们编译并运行上面的程序,这将产生以下结果:

字符串值 = 98993489, 整型值 = 98993489
字符串值 = xuebiancheng.cn, 整型值 = 0

C 库函数 - atol()

描述

C 库函数 long int atol(const char *str) 把参数 str 所指向的字符串转换为一个长整数(类型为 long int 型)。

声明

下面是 atol() 函数的声明。

long int atol(const char *str)

参数

  • str -- 要转换为长整数的字符串。

返回值

该函数返回转换后的长整数,如果没有执行有效的转换,则返回零。

实例

下面的实例演示了 atol() 函数的用法。

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

int main()
{
   long val;
   char str[20];
   
   strcpy(str, "98993489");
   val = atol(str);
   printf("字符串值 = %s, 长整型值 = %ld\n", str, val);

   strcpy(str, "xuebiancheng.cn");
   val = atol(str);
   printf("字符串值 = %s, 长整型值 = %ld\n", str, val);

   return(0);
}

让我们编译并运行上面的程序,这将产生以下结果:

字符串值 = 98993489, 长整型值 = 98993489
字符串值 = xuebiancheng.cn, 长整型值 = 0

C 库函数 - strtod()

描述

C 库函数 double strtod(const char *str, char **endptr) 把参数 str 所指向的字符串转换为一个浮点数(类型为 double 型)。如果 endptr 不为空,则指向转换中最后一个字符后的字符的指针会存储在 endptr 引用的位置。

声明

下面是 strtod() 函数的声明。

double strtod(const char *str, char **endptr)

参数

  • str -- 要转换为双精度浮点数的字符串。
  • endptr -- 对类型为 char* 的对象的引用,其值由函数设置为 str 中数值后的下一个字符。

返回值

该函数返回转换后的双精度浮点数,如果没有执行有效的转换,则返回零(0.0)。

实例

下面的实例演示了 strtod() 函数的用法。

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

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

   ret = strtod(str, &ptr);
   printf("数字(double)是 %lf\n", ret);
   printf("字符串部分是 |%s|", ptr);

   return(0);
}

让我们编译并运行上面的程序,这将产生以下结果:

数字(double)是 20.303000
字符串部分是 | This is test|

C 库函数 - strtol()

描述

C 库函数 long int strtol(const char *str, char **endptr, int base) 把参数 str 所指向的字符串根据给定的 base 转换为一个长整数(类型为 long int 型),base 必须介于 2 和 36(包含)之间,或者是特殊值 0。

声明

下面是 strtol() 函数的声明。

long int strtol(const char *str, char **endptr, int base)

参数

  • str -- 要转换为长整数的字符串。
  • endptr -- 对类型为 char* 的对象的引用,其值由函数设置为 str 中数值后的下一个字符。
  • base -- 基数,必须介于 2 和 36(包含)之间,或者是特殊值 0。

返回值

该函数返回转换后的长整数,如果没有执行有效的转换,则返回一个零值。

实例

下面的实例演示了 strtol() 函数的用法。

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

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

   ret = strtol(str, &ptr, 10);
   printf("数字(无符号长整数)是 %ld\n", ret);
   printf("字符串部分是 |%s|", ptr);

   return(0);
}

让我们编译并运行上面的程序,这将产生以下结果:

数字(无符号长整数)是 2030300
字符串部分是 | This is test|

C 库函数 - strtoul()

描述

C 库函数 unsigned long int strtoul(const char *str, char **endptr, int base) 把参数 str 所指向的字符串根据给定的 base 转换为一个无符号长整数(类型为 unsigned long int 型),base 必须介于 2 和 36(包含)之间,或者是特殊值 0。

声明

下面是 strtoul() 函数的声明。

unsigned long int strtoul(const char *str, char **endptr, int base)

参数

  • str -- 要转换为无符号长整数的字符串。
  • endptr -- 对类型为 char* 的对象的引用,其值由函数设置为 str 中数值后的下一个字符。
  • base -- 基数,必须介于 2 和 36(包含)之间,或者是特殊值 0。

返回值

该函数返回转换后的长整数,如果没有执行有效的转换,则返回一个零值。

实例

下面的实例演示了 strtoul() 函数的用法。

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

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

   ret = strtoul(str, &ptr, 10);
   printf("数字(无符号长整数)是 %lu\n", ret);
   printf("字符串部分是 |%s|", ptr);

   return(0);
}

让我们编译并运行上面的程序,这将产生以下结果:

数字(无符号长整数)是 2030300
字符串部分是 | This is test|

C 库函数 - calloc()

描述

C 库函数 void *calloc(size_t nitems, size_t size) 分配所需的内存空间,并返回一个指向它的指针。malloccalloc 之间的不同点是,malloc 不会设置内存为零,而 calloc 会设置分配的内存为零。

声明

下面是 calloc() 函数的声明。

void *calloc(size_t nitems, size_t size)

参数

  • nitems -- 要被分配的元素个数。
  • size -- 元素的大小。

返回值

该函数返回一个指针,指向已分配的内存。如果请求失败,则返回 NULL。

实例

下面的实例演示了 calloc() 函数的用法。

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

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

   printf("要输入的元素个数:");
   scanf("%d",&n);

   a = (int*)calloc(n, sizeof(int));
   printf("输入 %d 个数字:\n",n);
   for( i=0 ; i < n ; i++ ) 
   {
      scanf("%d",&a[i]);
   }

   printf("输入的数字为:");
   for( i=0 ; i < n ; i++ ) {
      printf("%d ",a[i]);
   }
   
   return(0);
}

让我们编译并运行上面的程序,这将产生以下结果:

要输入的元素个数:3
输入 3 个数字:
22
55
14
输入的数字为:22 55 14

C 库函数 - free()

描述

C 库函数 void free(void *ptr) 释放之前调用 calloc、malloc 或 realloc 所分配的内存空间。

声明

下面是 free() 函数的声明。

void free(void *ptr)

参数

  • ptr -- 指针指向一个要释放内存的内存块,该内存块之前是通过调用 malloc、calloc 或 realloc 进行分配内存的。如果传递的参数是一个空指针,则不会执行任何动作。

返回值

该函数不返回任何值。

实例

下面的实例演示了 free() 函数的用法。

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

int main()
{
   char *str;

   /* 最初的内存分配 */
   str = (char *) malloc(15);
   strcpy(str, "xuebiancheng");
   printf("String = %s,  Address = %u\n", str, str);

   /* 重新分配内存 */
   str = (char *) realloc(str, 25);
   strcat(str, ".cn");
   printf("String = %s,  Address = %u\n", str, str);

   /* 释放已分配的内存 */
   free(str);
   
   return(0);
}

让我们编译并运行上面的程序,这将产生以下结果:

String = xuebianheng, Address = 355090448
String = xuebiancheng.cn, Address = 355090448

C 库函数 - malloc()

描述

C 库函数 void *malloc(size_t size) 分配所需的内存空间,并返回一个指向它的指针。

声明

下面是 malloc() 函数的声明。

void *malloc(size_t size)

参数

  • size -- 内存块的大小,以字节为单位。

返回值

该函数返回一个指针 ,指向已分配大小的内存。如果请求失败,则返回 NULL。

实例

下面的实例演示了 malloc() 函数的用法。

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

int main()
{
   char *str;

   /* 最初的内存分配 */
   str = (char *) malloc(15);
   strcpy(str, "xuebiancheng");
   printf("String = %s,  Address = %u\n", str, str);

   /* 重新分配内存 */
   str = (char *) realloc(str, 25);
   strcat(str, ".cn");
   printf("String = %s,  Address = %u\n", str, str);

   free(str);

   return(0);
}:q

让我们编译并运行上面的程序,这将产生以下结果:

String = xuebiancheng,  Address = 3662685808
String = xuebiancheng.cn,  Address = 3662685808

C 库函数 - realloc()

描述

C 库函数 void *realloc(void *ptr, size_t size) 尝试重新调整之前调用 malloccalloc 所分配的 ptr 所指向的内存块的大小。

声明

下面是 realloc() 函数的声明。

void *realloc(void *ptr, size_t size)

参数

  • ptr -- 指针指向一个要重新分配内存的内存块,该内存块之前是通过调用 malloc、calloc 或 realloc 进行分配内存的。如果为空指针,则会分配一个新的内存块,且函数返回一个指向它的指针。
  • size -- 内存块的新的大小,以字节为单位。如果大小为 0,且 ptr 指向一个已存在的内存块,则 ptr 所指向的内存块会被释放,并返回一个空指针。

返回值

该函数返回一个指针 ,指向重新分配大小的内存。如果请求失败,则返回 NULL。

实例

下面的实例演示了 realloc() 函数的用法。

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

int main()
{
   char *str;

   /* 最初的内存分配 */
   str = (char *) malloc(15);
   strcpy(str, "xuebiancheng");
   printf("String = %s,  Address = %u\n", str, str);

   /* 重新分配内存 */
   str = (char *) realloc(str, 25);
   strcat(str, ".cn");
   printf("String = %s,  Address = %u\n", str, str);

   free(str);
   
   return(0);
}

让我们编译并运行上面的程序,这将产生以下结果:

String = xuebiancheng, Address = 355090448
String = xuebiancheng.cn, Address = 355090448

C 库函数 - abort()

描述

C 库函数 void abort(void) 中止程序执行,直接从调用的地方跳出。

声明

下面是 abort() 函数的声明。

void abort(void)

参数

  • NA

返回值

该函数不返回任何值。

实例

下面的实例演示了 abort() 函数的用法。

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

int main ()
{
   FILE *fp;
   
   printf("准备打开 nofile.txt\n");
   fp = fopen( "nofile.txt","r" );
   if(fp == NULL)
   {
      printf("准备终止程序\n");
      abort();
   }
   printf("准备关闭 nofile.txt\n");
   fclose(fp);
   
   return(0);
}

让我们编译并运行上面的程序,这将产生以下结果,因为我们尝试打开的文件 nofile.txt 是不存在的:

准备打开 nofile.txt
准备终止程序

C 库函数 - atexit()

描述

C 库函数 int atexit(void (*func)(void)) 当程序正常终止时,调用指定的函数 func。您可以在任何地方注册你的终止函数,但它会在程序终止的时候被调用。

声明

下面是 atexit() 函数的声明。

int atexit(void (*func)(void))

参数

  • func -- 在程序终止时被调用的函数。

返回值

如果函数成功注册,则该函数返回零,否则返回一个非零值。

实例

下面的实例演示了 atexit() 函数的用法。

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

void functionA ()
{
   printf("这是函数A\n");
}

int main ()
{
   /* 注册终止函数 */
   atexit(functionA );
   
   printf("启动主程序...\n");

   printf("退出主程序...\n");

   return(0);
}

让我们编译并运行上面的程序,这将产生以下结果:

启动主程序...
退出主程序...
这是函数A

C 库函数 - exit()

描述

C 库函数 void exit(int status) 立即终止调用进程。任何属于该进程的打开的文件描述符都会被关闭,该进程的子进程由进程 1 继承,初始化,且会向父进程发送一个 SIGCHLD 信号。

声明

下面是 exit() 函数的声明。

void exit(int status)

参数

  • status -- 返回给父进程的状态值。

返回值

该函数不返回值。

实例

下面的实例演示了 exit() 函数的用法。

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

int main ()
{
   printf("程序的开头....\n");
   
   printf("退出程序....\n");
   exit(0);

   printf("程序的结尾....\n");

   return(0);
}

让我们编译并运行上面的程序,这将产生以下结果:

程序的开头....
退出程序....

C 库函数 - getenv()

描述

C 库函数 char *getenv(const char *name) 搜索 name 所指向的环境字符串,并返回相关的值给字符串。

声明

下面是 getenv() 函数的声明。

char *getenv(const char *name)

参数

  • name -- 包含被请求变量名称的 C 字符串。

返回值

该函数返回一个以 null 结尾的字符串,该字符串为被请求环境变量的值。如果该环境变量不存在,则返回 NULL。

实例

下面的实例演示了 getenv() 函数的用法。

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

int main ()
{
   printf("PATH : %s\n", getenv("PATH"));
   printf("HOME : %s\n", getenv("HOME"));
   printf("ROOT : %s\n", getenv("ROOT"));

   return(0);
}

让我们编译并运行上面的程序,这将产生以下结果:

PATH : /sbin:/usr/sbin:/bin:/usr/bin:/usr/local/bin
HOME : /
ROOT : (null)

C 库函数 - system()

描述

C 库函数 int system(const char *command)command 指定的命令名称或程序名称传给要被命令处理器执行的主机环境,并在命令完成后返回。

声明

下面是 system() 函数的声明。

int system(const char *command)

参数

  • command -- 包含被请求变量名称的 C 字符串。

返回值

如果发生错误,则返回值为 -1,否则返回命令的状态。

实例

下面的实例演示了 system() 函数的用法,列出了 unix 机上当前目录下所有的文件和目录。

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

int main ()
{
   char command[50];

   strcpy( command, "ls -l" );
   system(command);

   return(0);
} 

让我们编译并运行上面的程序,在 unix 机上将产生以下结果:

drwxr-xr-x 2 apache apache 4096 Aug 22 07:25 hsperfdata_apache
drwxr-xr-x 2 railo railo 4096 Aug 21 18:48 hsperfdata_railo
rw------ 1 apache apache 8 Aug 21 18:48 mod_mono_dashboard_XXGLOBAL_1
rw------ 1 apache apache 8 Aug 21 18:48 mod_mono_dashboard_asp_2
srwx---- 1 apache apache 0 Aug 22 05:28 mod_mono_server_asp
rw------ 1 apache apache 0 Aug 22 05:28 mod_mono_server_asp_1280495620
srwx---- 1 apache apache 0 Aug 21 18:48 mod_mono_server_global

下面的实例演示了 system() 函数的用法,列出了 windows 机上当前目录下所有的文件和目录。

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

int main ()
{
   char command[50];

   strcpy( command, "dir" );
   system(command);

   return(0);
} 

让我们编译并运行上面的程序,在 windows 机上将产生以下结果:

a.txt
amit.doc
sachin
saurav
file.c

C 库函数 - bsearch()

描述

C 库函数 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 所对应的比较函数升序排序。

声明

下面是 bsearch() 函数的声明。

void *bsearch(const void *key, const void *base, size_t nitems, size_t size, int (*compar)(const void *, const void *))

参数

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

返回值

如果查找成功,该函数返回一个指向数组中匹配元素的指针,否则返回空指针。.

实例

下面的实例演示了 bsearch() 函数的用法。

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


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 = 32;

   /* 使用 bsearch() 在数组中查找值 32 */
   item = (int*) bsearch (&key, values, 5, sizeof (int), cmpfunc);
   if( item != NULL ) 
   {
      printf("Found item = %d\n", *item);
   }
   else 
   {
      printf("Item = %d could not be found\n", *item);
   }
   
   return(0);
}

让我们编译并运行上面的程序,这将产生以下结果:

Found item = 32

C 库函数 - qsort()

描述

C 库函数 void qsort(void *base, size_t nitems, size_t size, int (*compar)(const void *, const void*)) 对数组进行排序。

声明

下面是 qsort() 函数的声明。

void qsort(void *base, size_t nitems, size_t size, int (*compar)(const void *, const void*))

参数

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

返回值

该函数不返回任何值。

实例

下面的实例演示了 qsort() 函数的用法。

#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 );
}

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]);
   }
  
  return(0);
}

让我们编译并运行上面的程序,这将产生以下结果:

排序之前的列表:
88 56 100 2 25 
排序之后的列表:
2 25 56 88 100

C 库函数 - abs()

描述

C 库函数 int abs(int x) 返回 x 的绝对值。

声明

下面是 abs() 函数的声明。

int abs(int x)

参数

  • x -- 完整的值。

返回值

该函数返回 x 的绝对值。

实例

下面的实例演示了 abs() 函数的用法。

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

int main ()
{
   int a, b;

   a = abs(5);
   printf("a 的值 = %d\n", a);

   b = abs(-10);
   printf("b 的值 = %d\n", b);
   
   return(0);
}

让我们编译并运行上面的程序,这将产生以下结果:

a 的值 = 5
b 的值 = 10

C 库函数 - div()

描述

C 库函数 div_t div(int numer, int denom)numer(分子)除以 denom(分母)

声明

下面是 div() 函数的声明。

div_t div(int numer, int denom)

参数

  • numer -- 分子。
  • denom -- 分母。

返回值

该函数返回定义在 <cstdlib> 中的结构中的值,该结构有两个成员,如 div_t:int quot; int rem;

实例

下面的实例演示了 div() 函数的用法。

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

int main()
{
   div_t output;

   output = div(27, 4);
   printf("(27/ 4) 的商  = %d\n", output.quot);
   printf("(27/4) 的余数 = %d\n", output.rem);

   output = div(27, 3);
   printf("(27/ 3) 的商 = %d\n", output.quot);
   printf("(27/3) 的余数 = %d\n", output.rem);

   return(0);
}

让我们编译并运行上面的程序,这将产生以下结果:

(27/ 4) 的商 = 6
(27/4) 的余数 = 3
(27/ 3) 的商 = 9
(27/3) 的余数 = 0

C 库函数 - labs()

描述

C 库函数 long int labs(long int x) 返回 x 的绝对值。

声明

下面是 labs() 函数的声明。

long int labs(long int x)

参数

  • x -- 完整的值。

返回值

该函数返回 x 的绝对值。

实例

下面的实例演示了 labs() 函数的用法。

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

int main ()
{
   long int a,b;

   a = labs(65987L);
   printf("a 的值 = %ld\n", a);

   b = labs(-1005090L);
   printf("b 的值 = %ld\n", b);
   
   return(0);
}

让我们编译并运行上面的程序,这将产生以下结果:

a 的值 = 65987
b 的值 = 1005090

C 库函数 - ldiv()

描述

C 库函数 div_t div(long int numer, long int denom)numer(分子)除以 denom(分母)

声明

下面是 ldiv() 函数的声明。

div_t div(long int numer, long int denom)

参数

  • numer -- 分子。
  • denom -- 分母。

返回值

该函数返回定义在 <cstdlib> 中的结构中的值,该结构有两个成员,如 ldiv_t:long quot; long rem;

实例

下面的实例演示了 ldiv() 函数的用法。

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

int main ()
{
   ldiv_t output;

   output = ldiv(100000L, 30000L);

   printf("商 = %ld\n", output.quot);

   printf("余数 = %ld\n", output.rem);
   
   return(0);
}

让我们编译并运行上面的程序,这将产生以下结果:

 = 3
余数 = 10000

C 库函数 - rand()

描述

C 库函数 int rand(void) 返回一个范围在 0 到 RAND_MAX 之间的伪随机数。

RAND_MAX 是一个常量,它的默认值在不同的实现中会有所不同,但是值至少是 32767。

声明

下面是 rand() 函数的声明。

int rand(void)

参数

  • NA

返回值

该函数返回一个范围在 0 到 RAND_MAX 之间的整数值。

实例

下面的实例演示了 rand() 函数的用法。

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

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\n", rand() % 50);
   }
   
  return(0);
}

让我们编译并运行上面的程序,这将产生以下结果:

38
45
29
29
47

C 库函数 - srand()

描述

C 库函数 void srand(unsigned int seed) 播种由函数 rand 使用的随机数发生器。

声明

下面是 srand() 函数的声明。

void srand(unsigned int seed)

参数

  • seed -- 这是一个整型值,用于伪随机数生成算法播种。

返回值

该函数不返回任何值。

实例

下面的实例演示了 srand() 函数的用法。

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

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\n", rand() % 50);
   }
   
  return(0);
}

让我们编译并运行上面的程序,这将产生以下结果:

38
45
29
29
47

C 库函数 - mblen()

描述

C 库函数 int mblen(const char *str, size_t n) 返回参数 str 所指向的多字节字符的长度。

声明

下面是 mblen() 函数的声明。

int mblen(const char *str, size_t n)

参数

  • str -- 指向多字节字符的第一个字节的指针。
  • n -- 要检查的字符长度的最大字节数。

返回值

如果识别了一个非空宽字符,mblen() 函数返回 str 开始的多字节序列解析的字节数。如果识别了一个空宽字符,则返回 0。如果识别了一个无效的多字节序列,或者不能解析一个完整的多字节字符,则返回 -1。

实例

下面的实例演示了 mblen() 函数的用法。

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

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("转换为多字节字符串\n");
   len = wcstombs( pmb, pwc, MB_CUR_MAX);
   printf("被转换的字符 %d\n", len);
   printf("第一个多字节字符的十六进制值:%#.4x\n", pmb);
   
   len = mblen( pmb, MB_CUR_MAX );
   printf( "多字节字符 %x 的字节长度:%u\n", pmb, len );
   
   pmb = NULL;
   
   len = mblen( pmb, MB_CUR_MAX );
   printf( "多字节字符 %x 的字节长度:%u\n", pmb, len );
   
   return(0);
}

让我们编译并运行上面的程序,这将产生以下结果:

转换为多字节字符串
被转换的字符 1
第一个多字节字符的十六进制值:0x168c6010
多字节字符 168c6010 的字节长度:1
多字节字符 0 的字节长度:0

C 库函数 - mbstowcs()

描述

C 库函数 size_t mbstowcs(schar_t *pwcs, const char *str, size_t n) 把参数 str 所指向的多字节字符的字符串转换为参数 pwcs 所指向的数组。

声明

下面是 mbstowcs() 函数的声明。

size_t mbstowcs(schar_t *pwcs, const char *str, size_t n)

参数

  • pwcs -- 指向一个 wchar_t 元素的数组,数组长度足以存储一个最大字符长度的宽字符串。
  • str -- 要被转换的多字节字符字符串。
  • n -- 要被转换的最大字符数。

返回值

该函数返回转换的字符数,不包括结尾的空字符。如果遇到一个无效的多字节字符,则返回 -1 值。

实例

下面的实例演示了 mbstowcs() 函数的用法。

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

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("转换为多字节字符串\n");
   len = wcstombs( pmb, pwc, MB_CUR_MAX);
   printf("被转换的字符 %d\n", len);
   printf("第一个多字节字符的十六进制值:%#.4x\n", pmb);
   
   printf("转换回宽字符字符串\n");
   len = mbstowcs( pwcs, pmb, MB_CUR_MAX);
   printf("被转换的字符 %d\n", len);
   printf("第一个宽字符的十六进制值:%#.4x\n\n", pwcs);
   
   return(0);
}

让我们编译并运行上面的程序,这将产生以下结果:

转换为多字节字符串
被转换的字符 1
第一个多字节字符的十六进制值:0x19a60010
转换回宽字符字符串
被转换的字符 1
第一个宽字符的十六进制值:0x19a60030

C 库函数 - mbtowc()

描述

C 库函数 int mbtowc(whcar_t *pwc, const char *str, size_t n) 把一个多字节序列转换为一个宽字符。

声明

下面是 mbtowc() 函数的声明。

int mbtowc(whcar_t *pwc, const char *str, size_t n)

参数

  • pwc -- 指向类型为 wchar_t 对象的指针。
  • str -- 指向多字节字符的第一个字节的指针。
  • n -- 要被检查的最大字节数。

返回值

  • 如果 str 不为 NULL,mbtowc() 函数返回 str 开始消耗的字节数,如果指向一个空字节,则返回 0,如果操作失败,则返回 -1。
  • 如果 str 为 NULL,如果编码具有移位状态,则 mbtowc() 函数返回非零,如果编码是无状态的,则返回零。

实例

下面的实例演示了 mbtowc() 函数的用法。

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

int main()
{
   char *str = "这里是 w3cschool.cc";
   wchar_t mb[100];
   int len;
   
   len = mblen(NULL, MB_CUR_MAX); 

   mbtowc(mb, str, len*strlen(str) );
   
   wprintf(L"%ls \n", mb );   
   
   return(0);
}

让我们编译并运行上面的程序,这将产生以下结果,因为它要以多字节形式输出结果,这是一种二进制输出。

???

C 库函数 - wcstombs()

C 标准库 - <stdlib.h> C 标准库 - <stdlib.h>

描述

C 库函数 size_t wcstombs(char *str, const wchar_t *pwcs, size_t n) 把宽字符字符串 pwcs 转换为一个 str 开始的多字节字符串。最多会有 n 个字节被写入 str 中。

声明

下面是 wcstombs() 函数的声明。

size_t wcstombs(char *str, const wchar_t *pwcs, size_t n)

参数

  • str -- 指向一个 char 元素的数组,至少有 n 字节长。
  • pwcs -- 要被转换的宽字符字符串。
  • n -- 要被写入到 str 中的最大字节数。

返回值

该函数返回转换和写入到 str 中的字节数,不包括结尾的空字符。如果遇到一个无效的多字节字符,则返回 -1 值。

实例

下面的实例演示了 wcstombs() 函数的用法。

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

#define BUFFER_SIZE 50

int main()
{
   size_t ret;
   char *MB = (char *)malloc( BUFFER_SIZE );
   wchar_t *WC = L"http://www.xuebiancheng.cn";

   /* 转换宽字符字符串 */
   ret = wcstombs(MB, WC, BUFFER_SIZE);
   
   printf("要转换的字符数 = %u\n", ret);
   printf("多字节字符 = %s\n\n", MB);
   
   return(0);
}

让我们编译并运行上面的程序,这将产生以下结果:

要转换的字符数 = 26
多字节字符 = http://www.xuebiancheng.cn

C 库函数 - wctomb()

描述

C 库函数 int wctomb(char *str, wchar_t wchar) 把宽字符 wchar 转换为它的多字节表示形式,并把它存储在 str 指向的字符数组的开头。

声明

下面是 wctomb() 函数的声明。

int wctomb(char *str, wchar_t wchar)

参数

  • str -- 一个指针,指向一个足以存储多字节字符的数组。
  • wchar -- 类型为 wchar_t 的宽字符。

返回值

  • 如果 str 不为 NULL,wctomb() 函数返回写入字节数组中的字节数。如果 wchar 不能被表示为一个多字节序列,则会返回 -1。
  • 如果 str 为 NULL,如果编码具有移位状态,则 wctomb() 函数返回非零,如果编码是无状态的,则返回零。

实例

下面的实例演示了 wctomb() 函数的用法。

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

int main()
{
   int i;
   wchar_t wc = L'a';
   char *pmbnull = NULL;
   char *pmb = (char *)malloc(sizeof( char ));

   printf("要转换的宽字符:\n");
   i = wctomb( pmb, wc );
   printf("被转换的字符:%u\n", i);
   printf("多字节字符:%.1s\n", pmb);

   printf("当要转换的字符为 NULL 时尝试转换:\n");
   i = wctomb( pmbnull, wc );
   printf("被转换的字符:%u\n", i);
   /* 不会输出任何值 */
   printf("多字节字符:%.1s\n", pmbnull);
   
   return(0);
}

让我们编译并运行上面的程序,这将产生以下结果:

要转换的宽字符:
被转换的字符:1
多字节字符:a
当要转换的字符为 NULL 时尝试转换:
被转换的字符:0
多字节字符: