Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikibooksThe Free Textbook Project
Search

C Programming/stdlib.h

From Wikibooks, open books for an open world
<C Programming
(Redirected fromC Programming/C Reference/stdlib.h)

stdlib.h is the header of thegeneral purpose standard library of C programming language which includes functions involving memory allocation, process control, conversions and others. It is compatible with C++ and is known ascstdlib in C++. The name "stdlib" stands for "standard library".

Member functions

[edit |edit source]

Members of thestdlib.h can be classified into the following categories: conversion, memory, process control, sort and search, mathematics.

NameDescription
Type Conversion
atofstring to double (NOT float)
atoistring to integer
atolstring to long
strtodstring to double
strtolstring to long int
strtoulstring to unsigned long int
strtollstring to long long int
strtoullstring to unsigned long long int
Pseudo-random sequence generation
int rand(void)generates a pseudo-random number
int random(void)generates a pseudo-random number (not standard C; provided by POSIX)
void srand(unsigned int seed)set the rand() pseudo-random generator seed [common convention uses time() to seed]
void srandom(unsigned int seed)set the random() pseudo-random generator seed [common convention uses time() to seed] (not standard C; provided by POSIX)
Memory allocation and deallocation
malloc
calloc
realloc
allocate memory from the heap
freerelease memory back to the heap
Process control
/abort/terminate execution abnormally
atexitregister a callback function for program exit
exitterminate program execution
getenvretrieve an environment variable
systemexecute an external command
Sorting, searching and comparison
bsearchbinary search an array
qsortsort an array
Mathematics
int abs(int)absolute value of an integer.
long int labs(long int)absolute value of a long integer.
divinteger division (returns quotient and remainder)
ldivlong integer division (returns quotient and remainder)
Multibyte / Wide Characters
mblensize of multibyte char[1]
mbtowc, wctomb, mbstowcs, wcstombsmultibyte & wide character conversion[2]

Member constants

[edit |edit source]

NULL

[edit |edit source]

Thestdlib.h andstddef.h header files define the macroNULL, which yields a null pointer constant, and represents a pointer value that is guaranteed not to point to a valid address in memory.

Variants

[edit |edit source]

NULL may be defined as a constant expression equal to int zero, long int zero, or zero cast to a void * pointer:

#define NULL  0
#define NULL  0L
#define NULL  ((void *) 0)

Although the null pointer constant is always represented in C by the symbolic constant 0 or by 0 cast to a void pointer, the actual bit representation of such a pointer is system-specific and may contain one-bits.

Member data types

[edit |edit source]

size_t

[edit |edit source]

The size_t definition shall be provided to a referencing piece of code by including this header file. In fact most implementations don't have it defined literally in this file but instead include the file stddef.h as, for example, the standard library of the GNU C compiler does. The direct inclusion of stddef.h for application code is totally valid and thus can replace stdlib.h in cases where no other members from this file are needed or desired. This whole header file design conforms with e.g. the C99 ISO/ANSI standard definition.[3]

div_t, ldiv_t

[edit |edit source]

Two less widely used datatypes,div_t andldiv_t, are also defined. They are the return types of the div and ldiv functions. The standard defines them as:

typedefstruct{intquot,rem;}div_t;
typedefstruct{longintquot,rem;}ldiv_t;

Nonstandard functions

[edit |edit source]

itoa

[edit |edit source]

/itoa/ is a common function that is included in many implementations of stdlib.h, but the standard does not define the function. Although the same result can be achieved with sprintf, which is defined in the standard, callingitoa directly involves considerably less overhead than calling it (or an equivalent) viasprintf.

See also

[edit |edit source]
  • stdio.h
  • C standard library
  • wchar_t (wide characters)
  • div (C) (division function)

References

[edit |edit source]
  1. C++ Resources Network - stdlib.h accessed 2009 12 15
  2. C++ Resources Network - stdlib.h accessed 2009 12 15
  3. http://www.open-std.org/JTC1/SC22/wg14/www/docs/n1124.pdf chapter 7.20
Retrieved from "https://en.wikibooks.org/w/index.php?title=C_Programming/stdlib.h&oldid=4412710"
Category:

[8]ページ先頭

©2009-2025 Movatter.jp