<stdlib.h> 实用功能
<stdlib.h> 头文件里包含了C语言的中最常用的系统函数
宏:
NULL 空
EXIT_FAILURE 失败状态码
EXIT_SUCCESS 成功状态码
RAND_MAX rand的最大返回值
MB_CUR_MAX 多字节字符中的最大字节数
变量:
typedef size_t是unsigned integer类型
typedef wchar_t 一个宽字符的大小
struct div_t 是结构体类型 作为div函数的返回类型
struct ldiv_t是结构体类型 作为ldiv函数的返回类型
函数:
字符串函数
atof(); 将字符串转换成浮点型数
函数名:atof
功能:把字符串转换成浮点数
用法:double atof(const char *nptr);
程序例:
[syntaxHighLighter brush="c"]
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
float f;
char *str = "12345.67";
f = atof(str);
printf("string = %s float = %f\n", str, f);
return 0;
}
[/syntaxHighLighter]
atoi(); 将字符串转换成整型数
函数名:atoi
功能:把字符串转换成长整型数
用法:int atoi(const char *nptr);
程序例:
[syntaxHighLighter brush="c"]
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
int n;
char *str = "12345.67";
n = atoi(str);
printf("string = %s integer = %d\n", str, n);
return 0;
}
[/syntaxHighLighter]
atol(); 将字符串转换成长整型数
函数名:atol
功能:把字符串转换成长整型数
用法:long atol(const char *nptr);
程序例:
[syntaxHighLighter brush="c"]
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
long l;
char *str = "98765432";
l = atol(lstr);
printf("string = %s integer = %ld\n", str, l);
return(0);
}
[/syntaxHighLighter]
strtod(); 将字符串转换成浮点数
函数名:strtod
功能:将字符串转换为double型值
用法:double strtod(char *str, char **endptr);
程序例:
[syntaxHighLighter brush="c"]
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
char input[80], *endptr;
double value;
printf("Enter a floating point number:");
gets(input);
value = strtod(input, &endptr);
printf("The string is %s the number is %lf\n", input, value);
return 0;
}
[/syntaxHighLighter]
strtol(); 将字符串转换成长整型数
函数名:strtol
功能:将串转换为长整数
用法:long strtol(char *str, char **endptr, int base);
程序例:
[syntaxHighLighter brush="c"]
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
char *string = "87654321", *endptr;
long lnumber;
/* strtol converts string to long integer */
lnumber = strtol(string, &endptr, 10);
printf("string = %s long = %ld\n", string, lnumber);
return 0;
}
[/syntaxHighLighter]
strtoul(); 将字符串转换成无符号长整型数
函数名:strtoul
功能:strtoul()会将参数nptr字符串根据参数base来转换成无符号的长整型数。参数base范围从2至36,或0。参数base代表采用的进制方式,如base值为10则采用10进制,若base值为16则采用16进制数等。当base值为0时则是采用10进制做转换,但遇到如'0x'前置字符则会使用16进制做转换。一开始strtoul()会扫描参数nptr字符串,跳过前面的空格字符串,直到遇上数字或正负符号才开始做转换,再遇到非数字或字符串结束时('')结束转换,并将结果返回。若参数endptr不为NULL,则会将遇到不合条件而终止的nptr中的字符指针由endptr返回。
用法:unsigned long int strtoul(const char *nptr,char **endptr,int base);
返回值:返回转换后的长整型数,否则返回ERANGE并将错误代码存入errno中。
附加说明:ERANGE指定的转换字符串超出合法范围。
程序例: 将十六进制 0xFF,转换成 10进制,得到 255
[syntaxHighLighter brush="c"]
#include <stdlib.h>
#include <stdio.h>
int main()
{
int a;
char pNum[]="0xFF";
a=strtoul(pNum,0,16);
printf("%d\n",a);
return 0;
}
[/syntaxHighLighter]
内存控制函数
calloc(); 配置内存空间
原型:extern void *calloc(int num_elems, int elem_size);
用法:#include <alloc.h>
功能:为具有num_elems个长度为elem_size元素的数组分配内存
说明:如果分配成功则返回指向被分配内存的指针,否则返回空指针NULL。
当内存不再使用时,应使用free()函数将内存块释放。
举例:
[syntaxHighLighter brush="c"]
// calloc.c
#include <syslib.h>
#include <alloc.h>
main()
{
char *p;
clrscr(); // clear screen
p=(char *)calloc(100,sizeof(char));
if(p) printf("Memory Allocated at: %x",p);
else printf("Not Enough Memory!\n");
free(p);
getchar();
return 0;
}
[/syntaxHighLighter]
free(); 释放原先配置的内存
原型:extern void free(void *p);
用法:#include <alloc.h>
功能:释放指针p所指向的的内存空间。
说明:p所指向的内存空间必须是用calloc,malloc,realloc所分配的内存。
如果p为NULL或指向不存在的内存块则不做任何操作。
举例:
[syntaxHighLighter brush="c"]
// free.c
#include <syslib.h>
#include <alloc.h>
main()
{
char *p;
clrscr(); // clear screen
textmode(0x00);
p=(char *)malloc(100);
if(p) printf("Memory Allocated at: %x",p);
else printf("Not Enough Memory!\n");
getchar();
free(p); // release memory to reuse it
p=(char *)calloc(100,1);
if(p) printf("Memory Reallocated at: %x",p);
else printf("Not Enough Memory!\n");
free(p); // release memory at program end
getchar();
return 0;
}
[/syntaxHighLighter]
malloc(); 配置内存空间
原型:extern void *malloc(unsigned int num_bytes);
用法:#include <alloc.h>
功能:分配长度为num_bytes字节的内存块
说明:如果分配成功则返回指向被分配内存的指针,否则返回空指针NULL。
当内存不再使用时,应使用free()函数将内存块释放。
举例:
[syntaxHighLighter brush="c"]
// malloc.c
#include <syslib.h>
#include <alloc.h>
main()
{
char *p;
clrscr(); // clear screen
p=(char *)malloc(100);
if(p) printf("Memory Allocated at: %x",p);
else printf("Not Enough Memory!\n");
free(p);
getchar();
return 0;
}
[/syntaxHighLighter]
realloc(); 重新分配主存
原型:extern void *realloc(void *mem_address, unsigned int newsize);
用法:#include <alloc.h>
功能:改变mem_address所指内存区域的大小为newsize长度。
说明:如果重新分配成功则返回指向被分配内存的指针,否则返回空指针NULL。
当内存不再使用时,应使用free()函数将内存块释放。
举例:
[syntaxHighLighter brush="c"]
// realloc.c
#include <syslib.h>
#include <alloc.h>
main()
{
char *p;
clrscr(); // clear screen
p=(char *)malloc(100);
if(p) printf("Memory Allocated at: %x",p);
else printf("Not Enough Memory!\n");
getchar();
p=(char *)realloc(p,256);
if(p) printf("Memory Reallocated at: %x",p);
else printf("Not Enough Memory!\n");
free(p);
getchar();
return 0;
}
[/syntaxHighLighter]
环境函数
abort(); 异常终止一个进程
atexit();设置程序正常结束前调用的函数
exit(); 正常结束进程
getenv(); 取得环境变量内容
system(); 执行shell 命令
搜索和排序函数
bsearch(); 二元搜索
qsort(); 利用快速排序法排列数组
数学函数
abs(); 计算整型数的绝对值
div(); 将两个整数相除, 返回商和余数
labs(); 取长整型绝对值
ldiv();两个长整型数相除, 返回商和余数
rand(); 随机数发生器
srand(); 设置随机数种子
多字节函数
mblen(); 根据locale的设置确定字符的字节数
mbstowcs(); 把多字节字符串转换为宽字符串
mbtowc(); 把多字节字符转换为宽字符
wcstombs(); 把宽字符串转换为多字节字符串
wctomb(); 把宽字符转换为多字节字符









