C语言库stdlib.h操作

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 <stdio.h>
#include <stdlib.h>
#include <string.h>


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


   strcpy(str, "123abc"); //遇到了字符a停止遍历  遍历为  123.000000
   val = atof(str);
   printf("字符串值 = %s, 浮点值 = %f\n", str, val);
   
   strcpy(str, "123.321");
   val = atof(str);
   printf("字符串值 = %s, 浮点值 = %f\n", 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 <stdio.h>
#include <stdlib.h>
#include <string.h>


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


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


   strcpy(str, "a123");
   val = atoi(str);
   printf("字符串值 = %s, 整型值 = %d\n", str, val);
   
   return 0;
}
打印结果:
字符串值 = 654321, 整型值 = 654321
字符串值 = 1256a4, 整型值 = 1256
字符串值 = a123, 整型值 = 0


3.atol(str)
long int atol(const char *str) 把参数 str 所指向的字符串转换为一个长整数(类型为 long int 型)
该函数返回转换后的长整数,如果没有执行有效的转换,则返回零


#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, "96avda1"); // 遍历为96
   val = atol(str);
   printf("字符串值 = %s, 长整型值 = %ld\n", str, val);


   strcpy(str, "a*fai");
   val = atol(str);
   printf("字符串值 = %s, 长整型值 = %ld\n", 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 <stdio.h>
#include <stdlib.h>


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


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


   char str1[30] = "18.032561A130A98 hello world";
   ret = strtod(str1, &ptr);
   printf("str1 数字(double)是 %lf\n", ret);
   printf("str1 字符串部分是 |%s|", ptr);
   
   char str2[30] = "3.21123456789";
   ret = strtod(str2, &ptr);
   printf("str2 数字(double)是 %lf\n", 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 <stdio.h>
#include <stdlib.h>


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


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




   char str1[30] = "11 str";
   ret = strtol(str1, &ptr, 9);
   printf("str1数字(无符号长整数)是 %ld\n", ret);  // 以9进制解析 11 结果为10 正确
   printf("str1字符串部分是 |%s|\n", ptr);


   char str2[30] = "32 str";
   ret = strtol(str2, &ptr, 0);
   printf("str2 数字(无符号长整数)是 %ld\n", ret);
   printf("str2 字符串部分是 |%s|\n", 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 <stdio.h>
#include <stdlib.h>


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


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


   char str1[30] = "415 A305";
   ret = strtoul(str1, &ptr, 0);
   printf("数字(无符号长整数)是 %lu\n", ret);
   printf("字符串部分是 |%s|", ptr);
   
   char str2[30] = "1111 A32647";  
   ret = strtoul(str2, &ptr, 2);  // 已二进制的形式解析 
   printf("数字(无符号长整数)是 %lu\n", 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 <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;
}
打印结果:
要输入的元素个数:2
输入 2 个数字:
123
456
输入的数字为:123 456


8. void free(void *ptr)
void free(void *ptr) 释放之前调用 calloc、malloc 或 realloc 所分配的内存空间。
ptr -- 指针指向一个要释放内存的内存块,该内存块之前是通过调用 malloc、calloc 或 realloc 进行分配内存的。
如果传递的参数是一个空指针,则不会执行任何动作。 该函数不返回任何值。


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


int main()
{
   char *str;


   /* 最初的内存分配 */
   str = (char *) malloc(15); // 分配15个字节空间   malloc 不会设置内存为零  并把该内存地址赋值给str 
   strcpy(str, "Hello World"); 
   printf("String = %s,  Address = %u\n", str, str);


   /* 重新分配内存 */
   str = (char *) realloc(str, 25); // 把 str的内存地址重新分配为 25 个字节地址
   strcat(str, "123456789");
   printf("String = %s,  Address = %u\n", 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 <stdio.h>
#include <stdlib.h>
#include <string.h>


int main()
{
   char *str;


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


   /* 重新分配内存 */
   str = (char *) realloc(str, 25);
   strcat(str, "123456");
   printf("String = %s,  Address = %u\n", 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
   char *str;


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


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


   free(str);
   
   return 0;
}
打印结果:
String = 123456789,  Address = 7435280
String = 123456789123456789,  Address = 7435280


11. abort() 中止程序执行
void abort(void) 中止程序执行,直接从调用的地方跳出
#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();   //直接跳出    XXXX已停止工作
      printf("已经终止程序\n");  //该语句永远不能打印
   }
   printf("准备关闭 nofile.txt\n");
   fclose(fp);
   
   return 0;
}
打印结果:
准备打开 nofile.txt
准备终止程序   
-----------------------------程序结束


12. int atexit(void (*func)(void))
int atexit(void (*func)(void)) 当程序正常终止时,调用指定的函数 func。
您可以在任何地方注册你的终止函数,但它会在程序终止的时候被调用
func -- 在程序终止时被调用的函数
如果函数成功注册,则该函数返回零,否则返回一个非零值

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

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

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

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

   return 0;  // 在返回前 调用
}
打印结果:
ret = 0  启动主程序...
退出主程序...
这是函数A



13.
void exit(int status) 立即终止调用进程。任何属于该进程的打开的文件描述符都会被关闭
,且会向父进程发送一个 SIGCHLD 信号
status -- 传递给父进程的状态值。
该函数不返回值

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

int main ()
{
   printf("程序的开头....\n");
   
   printf("退出程序....\n");
   exit(0);   // 0 是传递给父进程的信号标志

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

   return 0;
}
打印结果:
程序的开头....
退出程序....



14.getenv(str)   getenv()用来取得参数envvar环境变量的内容 envvar为环境变量的名称,如果该变量存在则会返回指向该内容的指针
char *getenv(const char *name) 搜索 name 所指向的环境字符串,并返回相关的值给字符串
name -- 包含被请求变量名称的 C 字符串
该函数返回一个以 null 结尾的字符串,该字符串为被请求环境变量的值。如果该环境变量不存在,则返回 NULL

#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"));
   printf("COMSPEC : %s\n", getenv("COMSPEC"));
   printf("LIB : %s\n", getenv("LIB"));
   printf("USER : %s\n", 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 <stdlib.h>
#include <stdio.h>
#include <string.h>

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    <DIR>          .
2016/10/24  14:53    <DIR>          ..
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    <DIR>          frameworks
2015/11/04  19:50    <DIR>          HwCamera
2015/11/04  20:04    <DIR>          HwGallery2
2015/12/02  15:55    <DIR>          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 <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 = 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\n", *item);
   }
   else 
   {
      printf("Item = NULL  could not be found\n");
   }
   
   
   item = (int*) bsearch (&key1, values, 5, sizeof (int), cmpfunc); //查找到  就返回对应元素的指针
   if( item != NULL ) 
   {
      printf("Found item = %d\n", *item);
   }
   else 
   {
      printf("Item = NULL  could not be found\n");
   }
 
   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 <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


18.int abs(int x) 返回 x 的绝对值
x -- 完整的值
该函数返回 x 的绝对值

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

int main ()
{
   int a, b;

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

   b = abs(-200);
   printf("b 的值 = %d\n", b);
   
   return 0;
}
打印结果:
a 的值 = 100
b 的值 = 200



19.div_t div(int numer, int denom)   除法
div_t div(int numer, int denom) 把 numer(分子)除以 denom(分母)

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


#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



20. labs(long int x)  返回长整型的绝对值
long int labs(long int x) 返回 x 的绝对值
该函数返回 x 的绝对值。

#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


21.div(long int numer, long int denom)  长整型
div_t div(long int numer, long int denom) 把 numer(分子)除以 denom(分母)
该函数返回定义在 <cstdlib> 中的结构中的值, 该结构有两个成员,如  ldiv_t:long quot 商; long rem 余数;。

#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



22.  int rand(void)   得到随机数
int rand(void) 返回一个范围在 0 到 RAND_MAX 之间的伪随机数
RAND_MAX 是一个常量,它的默认值在不同的实现中会有所不同,但是值至少是 32767
该函数返回一个范围在 0 到 RAND_MAX 之间的整数值。

#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;
}
打印结果:
20
1
3
11
38



23.void srand(unsigned int seed)
void srand(unsigned int seed) 播种由函数 rand 使用的随机数发生器
seed -- 这是一个整型值,用于伪随机数生成算法播种。
该函数不返回任何值

#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;
}
打印结果:
13
11
49
23
43



24.mblen
int mblen(const char *str, size_t n) 返回参数 str 所指向的多字节字符的长度
str -- 指向多字节字符的第一个字节的指针。
n -- 要检查的字符长度的最大字节数。
如果识别了一个非空宽字符,mblen() 函数返回 str 开始的多字节序列解析的字节数。
如果识别了一个空宽字符,则返回 0。
如果识别了一个无效的多字节序列,或者不能解析一个完整的多字节字符,则返回 -1

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


   
   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;
}
打印结果:
多字节字符 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 <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
第一个多字节字符的十六进制值:0x6f7410
转换回宽字符字符串
被转换的字符 1
第一个宽字符的十六进制值:0x6f7430




  • 5
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值