411 REFERENCE · Wikibooks
C programming/stdlib.h
Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.
stdlib.h is the header of the general 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 as cstdlib in C++. The name "stdlib" stands for "standard library".
Member functions
Members of the stdlib.h can be classified into the following categories: conversion, memory, process control, sort and search, mathematics.
Name Description Type Conversionatof string to double (NOT float)atoi string to integeratol string to longstrtod string to doublestrtol string to long intstrtoul string to unsigned long intstrtoll string to long long intstrtoull string to unsigned long long int Pseudo-random sequence generationint rand(void) generates a pseudo-random numberint 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 deallocationmalloccallocrealloc allocate memory from the heapfree release memory back to the heap Process control/abort/ terminate execution abnormallyatexit register a callback function for program exitexit terminate program executiongetenv retrieve an environment variablesystem execute an external command Sorting, searching and comparisonbsearch binary search an arrayqsort sort an array Mathematicsint abs(int) absolute value of an integer.long int labs(long int) absolute value of a long integer.div integer division (returns quotient and remainder)ldiv long integer division (returns quotient and remainder) Multibyte / Wide Charactersmblen size of multibyte char C++ Resources Network - stdlib.h accessed 2009 12 15 mbtowc, wctomb, mbstowcs, wcstombs multibyte & wide character conversion C++ Resources Network - stdlib.h accessed 2009 12 15
Member constants
NULL
The stdlib.h and stddef.h header files define the macro NULL, which yields a null pointer constant, and represents a pointer value that is guaranteed not to point to a valid address in memory.
Variants
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
size_t
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. http://www.open-std.org/JTC1/SC22/wg14/www/docs/n1124.pdf chapter 7.20
div_t, ldiv_t
Two less widely used datatypes, div_t and ldiv_t, are also defined. They are the return types of the div and ldiv functions. The standard defines them as:
typedef struct {
int quot, rem;
} div_t;
typedef struct {
long int quot, rem;
} ldiv_t;
Nonstandard functions
itoa
/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, calling itoa directly involves considerably less overhead than calling it (or an equivalent) via sprintf.
See also
stdio.h
C standard library
wchar_t (wide characters)
div (C) (division function)
References
stdlib.h summary on cplusplus.com, accessed in February 2006.
Why size_t matters, accessed in May 2016.