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 edit

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

Name Description
Type Conversion
atof string to double (NOT float)
atoi string to integer
atol string to long
strtod string to double
strtol string to long int
strtoul string to unsigned long int
strtoll string to long long int
strtoull string 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
free release memory back to the heap
Process control
/abort/ terminate execution abnormally
atexit register a callback function for program exit
exit terminate program execution
getenv retrieve an environment variable
system execute an external command
Sorting, searching and comparison
bsearch binary search an array
qsort sort an array
Mathematics
int 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 Characters
mblen size of multibyte char [1]
mbtowc, wctomb, mbstowcs, wcstombs multibyte & wide character conversion [2]

Member constants edit

NULL edit

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 edit

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

size_t edit

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

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 edit

itoa edit

/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 edit

  • stdio.h
  • C standard library
  • wchar_t (wide characters)
  • div (C) (division function)

References edit