stdlib.h

From Wikipedia, the free encyclopedia

C Standard Library headers

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.

Contents

[edit] 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 Conversion
atof string to float
atoi string to integer
atol string to long integer
strtod string to double
strtol string to long int
strtoul string to unsigned long int
Pseudo-random sequence generation
rand generate a pseudo-random value
srand set the pseudo-random generator seed
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 and searching
bsearch binary search an array
qsort sort an array
Mathematics
abs
labs
absolute value
div
ldiv
integer division

[edit] Member constants

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. 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)

Note: 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 might not be all-bits-zero.

[edit] Member data types

A datatype called size_t is defined in the stdlib.h library, which is used to represent the size of an object. Library functions that take sizes expect them to be of type size_t, and the sizeof operator evaluates to size_t.

The actual type of size_t is platform-dependent; a common mistake is to assume size_t is the same as unsigned int, which can lead to programming errors [1], particularly as 64-bit architectures become more prevalent.

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;

[edit] Nonstandard functions

itoa is a common function that is included in many implementations of stdlib.h, but the standard does not define the function. It may have been included because of its appearance in The C Programming Language, the same effect of itoa can be achieved with sprintf (which is standard).

[edit] See also

[edit] References