C Standard Library:5 Utility Functions: <stdlib.h>

本文详细介绍了C语言标准库&lt;stdlib.h&gt;中的各种函数,包括数字转换、内存分配等实用功能。重点讲解了如atof、atoi、atol等字符串到数值的转换函数,以及rand和srand伪随机数生成函数,还有calloc、malloc、realloc和free等内存管理函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

5 Utility Functions: <stdlib.h>

The header <stdlib.h> declares functions for number conversion, storage allocation, and
similar tasks.

double atof(const char *s)
atof converts s to double; it is equivalent to strtod(s, (char**)NULL).


int atoi(const char *s)
converts s to int; it is equivalent to (int)strtol(s, (char**)NULL, 10).


long atol(const char *s)
converts s to long; it is equivalent to strtol(s, (char**)NULL, 10).


double strtod(const char *s, char **endp)
strtod converts the prefix of s to double, ignoring leading white space; it stores a
pointer to any unconverted suffix in *endp unless endp is NULL. If the answer would
overflow, HUGE_VAL is returned with the proper sign; if the answer would underflow,
zero is returned. In either case errno is set to ERANGE.


long strtol(const char *s, char **endp, int base)
strtol converts the prefix of s to long, ignoring leading white space; it stores a
pointer to any unconverted suffix in *endp unless endp is NULL. If base is between 2
and 36, conversion is done assuming that the input is written in that base. If base is
zero, the base is 8, 10, or 16; leading 0 implies octal and leading 0x or 0X hexadecimal.

Letters in either case represent digits from 10 to base-1; a leading 0x or 0X is
permitted in base 16. If the answer would overflow, LONG_MAX or LONG_MIN is
returned, depending on the sign of the result, and errno is set to ERANGE.


unsigned long strtoul(const char *s, char **endp, int base)
strtoul is the same as strtol except that the result is unsigned long and the error value is ULONG_MAX.


int rand(void)
rand returns a pseudo-random integer in the range 0 to RAND_MAX, which is at least 32767.


void srand(unsigned int seed)
srand uses seed as the seed for a new sequence of pseudo-random numbers. The
initial seed is 1.


void *calloc(size_t nobj, size_t size)
calloc returns a pointer to space for an array of nobj objects, each of size size, or
NULL if the request cannot be satisfied. The space is initialized to zero bytes.


void *malloc(size_t size)
malloc returns a pointer to space for an object of size size, or NULL if the request
cannot be satisfied. The space is uninitialized.


void *realloc(void *p, size_t size)
realloc changes the size of the object pointed to by p to size. The contents will be
unchanged up to the minimum of the old and new sizes. If the new size is larger, the
new space is uninitialized. realloc returns a pointer to the new space, or NULL if the
request cannot be satisfied, in which case *p is unchanged.


void free(void *p)
free deallocates the space pointed to by p; it does nothing if p is NULL. p must be a
pointer to space previously allocated by calloc, malloc, or realloc.


void abort(void)
abort causes the program to terminate abnormally, as if by raise(SIGABRT).


void exit(int status)
exit causes normal program termination. atexit functions are called in reverse order
of registration, open files are flushed, open streams are closed, and control is returned
to the environment. How status is returned to the environment is implementationdependent,
but zero is taken as successful termination. The values EXIT_SUCCESS and
EXIT_FAILURE may also be used.


int atexit(void (*fcn)(void))
atexit registers the function fcn to be called when the program terminates normally;
it returns non-zero if the registration cannot be made.


int system(const char *s)
system passes the string s to the environment for execution. If s is NULL, system
returns non-zero if there is a command processor. If s is not NULL, the return value is
implementation-dependent.


char *getenv(const char *name)
getenv returns the environment string associated with name, or NULL if no string exists.
Details are implementation-dependent.


void *bsearch(const void *key, const void *base,size_t n, size_t size,int (*cmp)(const void *keyval, const void *datum))
bsearch searches base[0]...base[n-1] for an item that matches *key. The function
cmp must return negative if its first argument (the search key) is less than its second (a
table entry), zero if equal, and positive if greater. Items in the array base must be in
ascending order. bsearch returns a pointer to a matching item, or NULL if none exists.


void qsort(void *base, size_t n, size_t size,int (*cmp)(const void *, const void *))
qsort sorts into ascending order an array base[0]...base[n-1] of objects of size
size. The comparison function cmp is as in bsearch.


int abs(int n)
abs returns the absolute value of its int argument.


long labs(long n)
labs returns the absolute value of its long argument.


div_t div(int num, int denom)
div computes the quotient and remainder of num/denom. The results are stored in the
int members quot and rem of a structure of type div_t.


ldiv_t ldiv(long num, long denom)
ldiv computes the quotient and remainder of num/denom. The results are stored in the
long members quot and rem of a structure of type ldiv_t.

转载于:https://www.cnblogs.com/freewater/archive/2013/03/21/2972991.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值