C stdlib.h

简介

stdlib .h 头文件定义了四个变量类型、一些宏和各种通用工具函数。

库变量

下面是头文件 stdlib.h 中定义的变量类型:

序号变量 & 描述
1size_t
这是无符号整数类型,它是 sizeof 关键字的结果。
2wchar_t
这是一个宽字符常量大小的整数类型。
3div_t
这是 div 函数返回的结构。
4ldiv_t
这是 ldiv 函数返回的结构。

库宏

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

序号宏 & 描述
1NULL
这个宏是一个空指针常量的值。
2EXIT_FAILURE
这是 exit 函数失败时要返回的值。
3EXIT_SUCCESS
这是 exit 函数成功时要返回的值。
4RAND_MAX
这个宏是 rand 函数返回的最大值。
5MB_CUR_MAX
这个宏表示在多字节字符集中的最大字符数,不能大于 MB_LEN_MAX。

库函数

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

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

 

#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, "runoob");
   val = atof(str);
   printf("字符串值 = %s, 浮点值 = %f\n", str, val);
 
   return(0);
}
#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, "runoob.com");
   val = atoi(str);
   printf("字符串值 = %s, 整型值 = %d\n", str, val);

   return(0);
}
#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, "runoob.com");
   val = atol(str);
   printf("字符串值 = %s, 长整型值 = %ld\n", str, val);

   return(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("数字(double)是 %lf\n", ret);
   printf("字符串部分是 |%s|", ptr);

   return(0);
}
#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);
}
#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]);
   }
   free (a);  // 释放内存
   return(0);
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
int main()
{
   char *str;
 
   /* 最初的内存分配 */
   str = (char *) malloc(15);
   strcpy(str, "runoob");
   printf("String = %s,  Address = %p\n", str, str);
 
   /* 重新分配内存 */
   str = (char *) realloc(str, 25);
   strcat(str, ".com");
   printf("String = %s,  Address = %p\n", str, str);
 
   /* 释放已分配的内存 */
   free(str);
 
   return(0);
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
 
int main()
{
   char *str;
 
   /* 最初的内存分配 */
   str = (char *) malloc(15);
   strcpy(str, "runoob");
   printf("String = %s,  Address = %u\n", str, str);
 
   /* 重新分配内存 */
   str = (char *) realloc(str, 25);
   strcat(str, ".com");
   printf("String = %s,  Address = %u\n", str, str);
 
   free(str);
 
   return(0);
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
int main()
{
   char *str;
 
   /* 最初的内存分配 */
   str = (char *) malloc(15);
   strcpy(str, "runoob");
   printf("String = %s,  Address = %p\n", str, str);
 
   /* 重新分配内存 */
   str = (char *) realloc(str, 25);
   strcat(str, ".com");
   printf("String = %s,  Address = %p\n", str, str);
 
   free(str);
   
   return(0);
}
#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);
}
#include <stdio.h>
#include <stdlib.h>

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

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

   return(0);
}
#include <stdio.h>
#include <string.h>
#include<stdlib.h>
 
int main ()
{
   char command[50];
 
   strcpy( command, "ls -l" );
   system(command);
 
   return(0);
}
#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);
}
#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.w3cschool.cc";

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

 


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

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值