STDLIB.H(7) Miscellaneous Information Manual STDLIB.H(7)

stdlib.hstandard library definitions

#include <stdlib.h>

[CX] Some of the functionality described on this reference page extends the ISO C standard. Applications shall define the appropriate feature test macro (see XSH V2_chap02(3)) to enable the visibility of these symbols in this header.

The <stdlib.h> header shall define the following macros which shall expand to integer constant expressions:

Unsuccessful termination for exit () ; [CX] the value shall be between 1 and 125 inclusive.
Successful termination for exit () ; the value shall be 0.
Maximum value returned by () ; at least 32767.

The <stdlib.h> header shall define the following macro which shall expand to a positive integer expression with type size_t:

Maximum number of bytes in a character specified by the current locale (category LC_CTYPE ).

[CX] In the POSIX locale the value of {MB_CUR_MAX} shall be 1.

The <stdlib.h> header shall define NULL as described in <stddef.h>.

The <stdlib.h> header shall define the following data types through :

Structure type returned by the () function.
Structure type returned by the () function.
Structure type returned by the () function.
As described in <stddef.h> .
As described in <stddef.h> .

[CX] In addition, the <stdlib.h> header shall define the following symbolic constants and macros as described in <sys/wait.h>:

WCOREDUMP WEXITSTATUS WIFEXITED WIFSIGNALED WIFSTOPPED WNOHANG WSTOPSIG WTERMSIG WUNTRACED

[CX] The <stdlib.h> header shall define the following symbolic constants as described in <fcntl.h>:

O_APPEND O_CLOEXEC O_CLOFORK [SIO] O_DSYNC O_NOCTTY O_RDWR [SIO] O_RSYNC [CX] O_SYNC

The following shall be declared as functions and may also be defined as macros. Function prototypes shall be provided.

_Noreturn void  _Exit(int);
[XSI]
long            a64l(const char *);

_Noreturn void  abort(void);
int             abs(int);
void           *aligned_alloc(size_t, size_t);
int             at_quick_exit(void (*)(void));
int             atexit(void (*)(void));
double          atof(const char *);
int             atoi(const char *);
long            atol(const char *);
long long       atoll(const char *);
void           *bsearch(const void *, const void *, size_t, size_t,
                    int (*)(const void *, const void *));
void           *calloc(size_t, size_t);
div_t           div(int, int);
[XSI]
double          drand48(void);
double          erand48(unsigned short [3]);

_Noreturn void  exit(int);
void            free(void *);
char           *getenv(const char *);
[CX]
int             getsubopt(char **restrict, char *const *restrict,
                    char **restrict);

[XSI]
int             grantpt(int);
char           *initstate(unsigned, char *, size_t);
long            jrand48(unsigned short [3]);
char           *l64a(long);

long            labs(long);
[XSI]
void            lcong48(unsigned short [7]);

ldiv_t          ldiv(long, long);
long long       llabs(long long);
lldiv_t         lldiv(long long, long long);
[XSI]
long            lrand48(void);

void           *malloc(size_t);
int             mblen(const char *, size_t);
size_t          mbstowcs(wchar_t *restrict, const char *restrict,
                    size_t);
int             mbtowc(wchar_t *restrict, const char *restrict, size_t);
[CX]
char           *mkdtemp(char *);
int             mkostemp(char *, int);
int             mkstemp(char *);

[XSI]
long            mrand48(void);
long            nrand48(unsigned short [3]);

[ADV]
int             posix_memalign(void **, size_t, size_t);

[XSI]
int             posix_openpt(int);
char           *ptsname(int);
int             ptsname_r(int, char *, size_t);
int             putenv(char *);

void            qsort(void *, size_t, size_t, int (*)(const void *,
                    const void *));
_Noreturn void  quick_exit(int);
[CX]
void            qsort_r(void *, size_t, size_t, int (*)(const void *,
                    const void *, void *), void *);

int             rand(void);
[XSI]
long            random(void);

void           *realloc(void *, size_t);
[CX]
void           *reallocarray(void *, size_t, size_t);
char           *realpath(const char *restrict, char *restrict);
char           *secure_getenv(const char *);

[XSI]
unsigned short *seed48(unsigned short [3]);

[CX]
int             setenv(const char *, const char *, int);

[OB XSI]
void            setkey(const char *);

[XSI]
char            *setstate(char *);

void            srand(unsigned);
[XSI]
void            srand48(long);
void            srandom(unsigned);

double          strtod(const char *restrict, char **restrict);
float           strtof(const char *restrict, char **restrict);
long            strtol(const char *restrict, char **restrict, int);
long double     strtold(const char *restrict, char **restrict);
long long       strtoll(const char *restrict, char **restrict, int);
unsigned long   strtoul(const char *restrict, char **restrict, int);
unsigned long long
                strtoull(const char *restrict, char **restrict, int);
int             system(const char *);
[XSI]
int             unlockpt(int);

[CX]
int             unsetenv(const char *);

size_t          wcstombs(char *restrict, const wchar_t *restrict,
                    size_t);
int             wctomb(char *, wchar_t);

[CX] Inclusion of the <stdlib.h> header may also make visible all symbols from <fcntl.h>, <limits.h>, <math.h>, <stddef.h>, and <sys/wait.h>.

None.

The ISO C standard requires that exit(EXIT_FAILURE) returns "unsuccessful termination status" to the host environment. In a POSIX host environment this means that the lower 8 bits of EXIT_FAILURE must have at least one bit set. The standard developers decided to further restrict the allowed values for the following reasons:

  • Exit statuses of 126 and greater are ambiguous in certain circumstances because they have special meanings in the shell (see XCU 2.8.2 Exit Status for Commands and the EXIT STATUS section of sh(1)).
  • The xargs(1) utility quits when a command execution exits with status 255 (see XCU xargs(1)).
  • Calling exit(3) with a value greater than 255 or less than 0 is something that only programs which are specifically designed to have their exit status obtained by waitid(3) should do (since it does not truncate the exit status to 8 bits). "Pure ISO C" programs that call exit(EXIT_FAILURE) do not meet this design criterion.

The requirement that the value of EXIT_SUCCESS is 0 is not shaded CX because this matches the requirement in the ISO C standard that exit(EXIT_SUCCESS) returns "successful termination status" to the host environment (when the host environment is a POSIX implementation).

None.

<limits.h>, <math.h>, <stddef.h>, <sys/types.h>, <sys/wait.h>

XSH V2_chap02(3), _Exit(3), a64l(3), abort(3), abs(3), atexit(3), atof(3), atoi(3), atol(3), bsearch(3), calloc(3), div(3), drand48(3), exit(3), free(3), getenv(3), getsubopt(3), grantpt(3), initstate(3), labs(3), ldiv(3), malloc(3), mblen(3), mbstowcs(3), mbtowc(3), mkdtemp(3), posix_memalign(3), posix_openpt(3), ptsname(3), putenv(3), qsort(3), rand(3), realloc(3), realpath(3), setenv(3), setkey(3), strtod(3), strtol(3), strtoul(3), system(3), unlockpt(3), unsetenv(3), waitid(3), wcstombs(3), wctomb(3)

First released in Issue 3.

The DESCRIPTION is updated for alignment with the POSIX Threads Extension.

The ttyslot () and valloc () functions are marked LEGACY.

The type of the third argument to initstate(3) is changed from to size_t. The type of the return value from setstate(3) is changed from char to char *, and the type of the first argument is changed from char * to const char *.

The Open Group Corrigendum U021/1 is applied, correcting the prototype for realpath(3) to be consistent with the reference page.

The Open Group Corrigendum U028/13 is applied, correcting the prototype for putenv(3) to be consistent with the reference page.

The rand_r () function is marked as part of the Thread-Safe Functions option.

Function prototypes for setenv(3) and unsetenv(3) are added.

The posix_memalign(3) function is added for alignment with IEEE Std 1003.1d-1999.

This reference page is updated to align with the ISO/IEC 9899:1999 (“ISO C99”) standard.

The (), (), (), and () functions are marked LEGACY.

The ttyslot () and valloc () functions are removed as they were previously marked LEGACY.

Extensions beyond the ISO C standard are marked.

SD5-XBD-ERN-79 and SD5-XBD-ERN-105 are applied.

The LEGACY functions are removed.

The mkdtemp(3) function is added from The Open Group Technical Standard, 2006, Extended API Set Part 1.

The rand_r () function is marked obsolescent.

This reference page is clarified with respect to macros and symbolic constants.

The type of the first argument to setstate(3) is changed from const char * to char *.

POSIX.1-2008, Technical Corrigendum 1, XBD/TC1-2008/0066 [197] is applied.

POSIX.1-2008, Technical Corrigendum 2, XBD/TC2-2008/0075 [663] is applied.

Austin Group Defect 411 is applied, adding mkostemp(3).

Austin Group Defect 444 is applied, adding the keyword to the getsubopt(3) prototype.

Austin Group Defect 508 is applied, adding the ptsname_r(3) function.

Austin Group Defects 593 and 1350 are applied, adding some O_* symbolic constants and allowing <stdlib.h> to make visible all symbols from <fcntl.h>.

Austin Group Defect 900 is applied, adding the qsort_r(3) function.

Austin Group Defect 922 is applied, adding the secure_getenv(3) function.

Austin Group Defect 1141 is applied, adding WCOREDUMP.

Austin Group Defect 1192 is applied, marking the setkey(3) function as obsolescent.

Austin Group Defect 1218 is applied, adding reallocarray(3).

Austin Group Defect 1229 is applied, changing the descriptions of EXIT_FAILURE and EXIT_SUCCESS, and the RATIONALE section.

Austin Group Defect 1302 is applied, aligning this header with the ISO/IEC 9899:2018 standard.

Austin Group Defect 1330 is applied, removing obsolescent interfaces.

Austin Group Defect 1629 is applied, changing the RATIONALE section.

Austin Group Defect 1663 is applied, removing XSI shading from realpath(3).

June 14, 2024 posix.fail