电脑爱好者,提供IT资讯信息及各类编程知识文章介绍,欢迎大家来本站学习电脑知识。 最近更新 | 联系我们 RSS订阅本站最新文章
电脑爱好者
站内搜索: 
当前位置:首页>> c语言>>C语言专题——标准库<stdlib.h>:

C语言专题——标准库<stdlib.h>

来源:www.cncfan.com | 2006-4-28 | (有4386人读过)

1 字符串转换

double atof (const char*);
int atoi (const char*);
long atol (const char*);

double strtod (const char*, char**);
long strtol (const char*, char**, int);
unsigned long strtoul (const char*, char**, int);

1> 第二组函数的参数意义如下:
const char* 指向需要转换的字符串
char** 更新后指向当前数字串之后的一个位置
int 基数(进制数)

2> strtol函数举例:

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

int main()
{
char *buf = " 0B 00 41 42 43 44 45 46 47 48 49 ";
char *ptr = buf;

while (isspace(*ptr))
ptr++;
while (*ptr != '\0')
{
printf("%ld\n", strtol(ptr, &ptr, 16));
while (isspace(*ptr))
ptr++;
}

return 0;
}

2 随机数

常量
#define RAND_MAX 0x7FFF rand的最大返回值

函数
void srand (unsigned int); 置随机数发生器(种子)
int rand (void); 返回下一个伪随机数

3 内存管理

常量
#define NULL ((void *)0) 空指针

函数
void* calloc (size_t, size_t); 分配内存, 并清零
void* malloc (size_t); 分配内存
void* realloc (void*, size_t); 重新分配内存, 返回新指针
void free (void*); 释放内存

4 与环境的接口

常量
#define EXIT_SUCCESS 0
#define EXIT_FAILURE 1

函数
void abort (void);
void exit (int);
int atexit (void (*)(void));

int system (const char*);
char* getenv (const char*);

5 查找与排序

void* bsearch (const void*, const void*, size_t, size_t,
int (*)(const void*, const void*));
void qsort (const void*, size_t, size_t,
int (*)(const void*, const void*));

1> comp函数的返回值
int comp(const void *p1, const void *p2)
{
const int *pi1 = (const int *)p1;
const int *pi2 = (const int *)p2;
return *pi1 - *pi2;
}
若第一个指针所指向的内容, 排序后应该放在第二个指针所指向的内容之前, 那么应返回负值;
反之返回正值; 其他情况返回0. 上例中的comp函数将int数组按升序排序.

2> qsort的调用方法
qsort((void *)list, length, sizeof(int), comp);

6 整数运算

结构
typedef struct { int quot, rem; } div_t;
typedef struct { long quot, rem; } ldiv_t;

函数
int abs (int);
long labs (long);

div_t div (int, int);
ldiv_t ldiv (long, long);

7 多字节字符

常量
MB_CUR_MAX 多字节字符中的最大字节数

函数
size_t wcstombs (char*, const wchar_t*, size_t);
int wctomb (char*, wchar_t);

int mblen (const char*, size_t);
size_t mbstowcs (wchar_t*, const char*, size_t);
int mbtowc (wchar_t*, const char*, size_t);


source: 《C & C++ Code Capsules》
c语言热门文章排行
网站赞助商
购买此位置

 

关于我们 | 网站地图 | 文档一览 | 友情链接| 联系我们

Copyright © 2003-2024 电脑爱好者 版权所有 备案号:鲁ICP备09059398号