简介
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 中定义的函数:
#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);
}