# Standard library header <cstdlib>

From cppreference.com
< cpp‎ | header

This header was originally in the C standard library as `<stdlib.h>`.

This header provides miscellaneous utilities. Symbols defined here are used by several library components.

### Types
div_t | structure type, returned by std::div (typedef)
ldiv_t | structure type, returned by std::ldiv (typedef)
lldiv_t (C++11) | structure type, returned by std::lldiv (typedef)
size_t | unsigned integer type returned by the `sizeof` operator (typedef)
### Macro constants
EXIT_SUCCESS EXIT_FAILURE | indicates program execution status (macro constant)
MB_CUR_MAX | maximum number of bytes in a multibyte character with the current locale (macro constant)
NULL | implementation-defined null pointer constant (macro constant)
RAND_MAX | maximum possible value generated by std::rand (macro constant)
### Functions
##### Process control
abort | causes abnormal program termination (without cleaning up) (function)
exit | causes normal program termination with cleaning up (function)
quick_exit (C++11) | causes quick program termination without completely cleaning up (function)
_Exit (C++11) | causes normal program termination without cleaning up (function)
atexit | registers a function to be called on std::exit() invocation (function)
at_quick_exit (C++11) | registers a function to be called on std::quick_exit invocation (function)
system | calls the host environment's command processor (function)
getenv | access to the list of environment variables (function)
##### Memory management
malloc | allocates memory (function)
aligned_alloc (C++17) | allocates aligned memory (function)
calloc | allocates and zeroes memory (function)
realloc | expands or shrinks previously allocated memory block (function)
free | deallocates previously allocated memory (function)
##### Numeric string conversion
atof | converts a byte string to a floating point value (function)
atoi atol atoll (C++11) | converts a byte string to an integer value (function)
strtol strtoll (C++11) | converts a byte string to an integer value (function)
strtoul strtoull (C++11) | converts a byte string to an unsigned integer value (function)
strtof strtod strtold | converts a byte string to a floating point value (function)
##### Wide string manipulation
mblen | returns the number of bytes in the next multibyte character (function)
mbtowc | converts the next multibyte character to wide character (function)
wctomb | converts a wide character to its multibyte representation (function)
mbstowcs | converts a narrow multibyte character string to wide string (function)
wcstombs | converts a wide string to narrow multibyte character string (function)
##### Miscellaneous algorithms and math
rand | generates a pseudo-random number (function)
srand | seeds pseudo-random number generator (function)
qsort | sorts a range of elements with unspecified type (function)
bsearch | searches an array for an element of unspecified type (function)
abs(int)labs llabs (C++11) | computes absolute value of an integral value (\(\small{|x|}\)|x|) (function)
div(int)ldiv lldiv (C++11) | computes quotient and remainder of integer division (function)

### Synopsis

    namespace std {
      using size_t =  /* see description */;
      using div_t =   /* see description */;
      using ldiv_t =  /* see description */;
      using lldiv_t = /* see description */;
    }

    #define NULL         /* see description */
    #define EXIT_FAILURE /* see description */
    #define EXIT_SUCCESS /* see description */
    #define RAND_MAX     /* see description */
    #define MB_CUR_MAX   /* see description */

    namespace std {
      // Exposition-only function type aliases
      extern "C" using /*c-atexit-handler*/ = void();                      // exposition only
      extern "C++" using /*atexit-handler*/ = void();                      // exposition only
      extern "C" using /*c-compare-pred*/ = int(const void*, const void*); // exposition only
      extern "C++" using /*compare-pred*/ = int(const void*, const void*); // exposition only

      // start and termination
      [[noreturn]] void abort() noexcept;
      int atexit(/*c-atexit-handler*/* func) noexcept;
      int atexit(/*atexit-handler*/* func) noexcept;
      int at_quick_exit(/*c-atexit-handler*/* func) noexcept;
      int at_quick_exit(/*atexit-handler*/* func) noexcept;
      [[noreturn]] void exit(int status);
      [[noreturn]] void _Exit(int status) noexcept;
      [[noreturn]] void quick_exit(int status) noexcept;

      char* getenv(const char* name);
      int system(const char* string);

      // C library memory allocation
      void* aligned_alloc(size_t alignment, size_t size);
      void* calloc(size_t nmemb, size_t size);
      void free(void* ptr);
      void* malloc(size_t size);
      void* realloc(void* ptr, size_t size);

      double atof(const char* nptr);
      int atoi(const char* nptr);
      long int atol(const char* nptr);
      long long int atoll(const char* nptr);
      double strtod(const char* nptr, char** endptr);
      float strtof(const char* nptr, char** endptr);
      long double strtold(const char* nptr, char** endptr);
      long int strtol(const char* nptr, char** endptr, int base);
      long long int strtoll(const char* nptr, char** endptr, int base);
      unsigned long int strtoul(const char* nptr, char** endptr, int base);
      unsigned long long int strtoull(const char* nptr, char** endptr, int base);

      // multibyte / wide string and character conversion functions
      int mblen(const char* s, size_t n);
      int mbtowc(wchar_t* pwc, const char* s, size_t n);
      int wctomb(char* s, wchar_t wchar);
      size_t mbstowcs(wchar_t* pwcs, const char* s, size_t n);
      size_t wcstombs(char* s, const wchar_t* pwcs, size_t n);

      // C standard library algorithms
      void* bsearch(const void* key, const void* base, size_t nmemb, size_t size,
                    /*c-compare-pred*/* compar);
      void* bsearch(const void* key, const void* base, size_t nmemb, size_t size,
                    /*compare-pred*/* compar);
      void qsort(void* base, size_t nmemb, size_t size, /*c-compare-pred*/* compar);
      void qsort(void* base, size_t nmemb, size_t size, /*compare-pred*/* compar);

      // low-quality random number generation
      int rand();
      void srand(unsigned int seed);

      // absolute values
      constexpr int abs(int j);
      constexpr long int abs(long int j);
      constexpr long long int abs(long long int j);
      constexpr float abs(float j);
      constexpr double abs(double j);
      constexpr long double abs(long double j);

      constexpr long int labs(long int j);
      constexpr long long int llabs(long long int j);

      constexpr div_t div(int numer, int denom);
      constexpr ldiv_t div(long int numer, long int denom);
      constexpr lldiv_t div(long long int numer, long long int denom);
      constexpr ldiv_t ldiv(long int numer, long int denom);
      constexpr lldiv_t lldiv(long long int numer, long long int denom);
    }

### Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR | Applied to | Behavior as published | Correct behavior
--- | --- | --- | ---
LWG 286 | C++98 | the definition of `size_t` was not provided in `<cstdlib>` | provided

### See also

- Program support utilities
- Pseudo-random number generation
- Common mathematical functions
- Mathematical special functions
- C memory management library
- Null-terminated byte strings
- Null-terminated multibyte strings
- Algorithms library

Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/header/cstdlib&oldid=145934"