📄 stdlib.html
字号:
<HTML><HEAD><TITLE><stdlib.h></TITLE></HEAD><BODY><H1><A NAME="<stdlib.h>"><CODE><stdlib.h></CODE></A></H1><HR><P><B><CODE><A HREF="#EXIT_FAILURE">EXIT_FAILURE</A>· <A HREF="#EXIT_SUCCESS">EXIT_SUCCESS</A>· <A HREF="#MB_CUR_MAX">MB_CUR_MAX</A>· <A HREF="#NULL">NULL</A>· <A HREF="#RAND_MAX">RAND_MAX</A></CODE></B></P><P><B><CODE><A HREF="#abort">abort</A>· <A HREF="#abs">abs</A>· <A HREF="#atexit">atexit</A>· <A HREF="#atof">atof</A>· <A HREF="#atoi">atoi</A>· <A HREF="#atol">atol</A>· <A HREF="#bsearch">bsearch</A>· <A HREF="#calloc">calloc</A>· <A HREF="#div">div</A>· <A HREF="#div_t">div_t</A>· <A HREF="#exit">exit</A>· <A HREF="#free">free</A>· <A HREF="#getenv">getenv</A>· <A HREF="#labs">labs</A>· <A HREF="#ldiv">ldiv</A>· <A HREF="#ldiv_t">ldiv_t</A>· <A HREF="#malloc">malloc</A>· <A HREF="#mblen">mblen</A>· <A HREF="#mbstowcs">mbstowcs</A>· <A HREF="#mbtowc">mbtowc</A>· <A HREF="#qsort">qsort</A>· <A HREF="#rand">rand</A>· <A HREF="#realloc">realloc</A>· <A HREF="#size_t">size_t</A>· <A HREF="#srand">srand</A>· <A HREF="#strtod">strtod</A>· <A HREF="#strtol">strtol</A>· <A HREF="#strtoul">strtoul</A>· <A HREF="#system">system</A>· <A HREF="#wchar_t">wchar_t</A>· <A HREF="#wcstombs">wcstombs</A>· <A HREF="#wctomb">wctomb</A></CODE></B></P><HR><P>Include the standard header <B><CODE><stdlib.h></CODE></B> to declare anassortment of useful functions and to define the macros and typesthat help you use them.</P><PRE> /* MACROS */#define <A HREF="#EXIT_FAILURE"><B>EXIT_FAILURE</B></A> <I><rvalue integer expression></I>#define <A HREF="#EXIT_SUCCESS"><B>EXIT_SUCCESS</B></A> <I><rvalue integer expression></I>#define <A HREF="#MB_CUR_MAX"><B>MB_CUR_MAX</B></A> <I><rvalue integer expression >= 1></I>#define <A HREF="#NULL"><B>NULL</B></A> <I><either 0, 0L, or (void *)0></I> <B>[0 in C++]</B>#define <A HREF="#RAND_MAX"><B>RAND_MAX</B></A> <I><integer constant expression >= 32,767></I> /* TYPES */typedef struct { int quot, rem; } <A HREF="#div_t"><B>div_t</B></A>;typedef struct { long quot, rem; } <A HREF="#ldiv_t"><B>ldiv_t</B></A>;typedef <I>ui-type</I> <A HREF="#size_t"><B>size_t</B></A>;typedef <I>i-type</I> <A HREF="#wchar_t"><B>wchar_t</B></A>; <B>[keyword in C++]</B> /* FUNCTIONS */int <A HREF="#abs"><B>abs</B></A>(int i);long <A HREF="#abs"><B>abs</B></A>(long i); <B>[C++ only]</B>long <A HREF="#labs"><B>labs</B></A>(long i);div_t <A HREF="#div"><B>div</B></A>(int numer, int denom);ldiv_t <A HREF="#div"><B>div</B></A>(long numer, long denom); <B>[C++ only]</B>ldiv_t <A HREF="#ldiv"><B>ldiv</B></A>(long numer, long denom);int <A HREF="#rand"><B>rand</B></A>(void);void <A HREF="#srand"><B>srand</B></A>(unsigned int seed);double <A HREF="#atof"><B>atof</B></A>(const char *s);int <A HREF="#atoi"><B>atoi</B></A>(const char *s);long <A HREF="#atol"><B>atol</B></A>(const char *s);double <A HREF="#strtod"><B>strtod</B></A>(const char *s, char **endptr);long <A HREF="#strtol"><B>strtol</B></A>(const char *s, char **endptr, int base);unsigned long <A HREF="#strtoul"><B>strtoul</B></A>(const char *s, char **endptr, int base);void *<A HREF="#calloc"><B>calloc</B></A>(size_t nelem, size_t size);void <A HREF="#free"><B>free</B></A>(void *ptr);void *<A HREF="#malloc"><B>malloc</B></A>(size_t size);void *<A HREF="#realloc"><B>realloc</B></A>(void *ptr, size_t size);int <A HREF="#mblen"><B>mblen</B></A>(const char *s, size_t n);size_t <A HREF="#mbstowcs"><B>mbstowcs</B></A>(wchar_t *wcs, const char *s, size_t n);int <A HREF="#mbtowc"><B>mbtowc</B></A>(wchar_t *pwc, const char *s, size_t n);size_t <A HREF="#wcstombs"><B>wcstombs</B></A>(char *s, const wchar_t *wcs, size_t n);int <A HREF="#wctomb"><B>wctomb</B></A>(char *s, wchar_t wchar);void <A HREF="#exit"><B>exit</B></A>(int status);void <A HREF="#abort"><B>abort</B></A>(void);char *<A HREF="#getenv"><B>getenv</B></A>(const char *name);int <A HREF="#system"><B>system</B></A>(const char *s);extern "C++" int <A HREF="#atexit"><B>atexit</B></A>(void (*func)(void)); <B>[C++ only]</B>extern "C" <B>[C++ only]</B> int <A HREF="#atexit"><B>atexit</B></A>(void (*func)(void));extern "C++" void *<A HREF="#bsearch"><B>bsearch</B></A>(const void *key, const void *base, size_t nelem, size_t size, int (*cmp)(const void *ck, const void *ce)); <B>[C++ only]</B>extern "C" <B>[C++ only]</B> void *<A HREF="#bsearch"><B>bsearch</B></A>(const void *key, const void *base, size_t nelem, size_t size, int (*cmp)(const void *ck, const void *ce));extern "C++" void <A HREF="#qsort"><B>qsort</B></A>(void *base, size_t nelem, size_t size, int (*cmp)(const void *e1, const void *e2)); <B>[C++ only]</B>extern "C" <B>[C++ only]</B> void <A HREF="#qsort"><B>qsort</B></A>(void *base, size_t nelem, size_t size, int (*cmp)(const void *e1, const void *e2));</PRE><H2><A NAME="abort"><CODE>abort</CODE></A></H2><PRE>void <B>abort</B>(void);</PRE><P>The function calls<CODE><A HREF="signal.html#raise">raise</A>(SIGABRT)</CODE>,which reports the abort signal,<A HREF="signal.html#SIGABRT"><CODE>SIGABRT</CODE></A>.Default handling for the abort signal is to cause abnormalprogram termination and report unsuccessful termination to the targetenvironment. Whether or not the target environment flushes outputstreams, closes open files, or removes temporary files on abnormaltermination is implementation defined. If you specify handling that causes<A HREF="signal.html#raise"><CODE>raise</CODE></A>to return control to <CODE>abort</CODE>, the function calls<CODE><A HREF="#exit">exit</A>(EXIT_FAILURE)</CODE>,to report unsuccessful termination with<A HREF="#EXIT_FAILURE"><CODE>EXIT_FAILURE</CODE></A>.<CODE>abort</CODE> never returns control to its caller.</P><H2><A NAME="abs"><CODE>abs</CODE></A></H2><PRE>int <B>abs</B>(int i);long <B>abs</B>(long i); <B>[C++ only]</B></PRE><P>The function returns the absolute value of<CODE>i</CODE>, <CODE>|i|</CODE>. The version that acceptsa <I>long</I> argument behaves the same as<A HREF="#abs"><CODE>labs</CODE></A></P><H2><A NAME="atexit"><CODE>atexit</CODE></A></H2><PRE>extern "C++" int <B>atexit</B>(void (*func)(void)); <B>[C++ only]</B>extern "C" <B>[C++ only]</B> int <B>atexit</B>(void (*func)(void));</PRE><P>The function registers the function whose address is <CODE>func</CODE>to be called by<A HREF="#exit"><CODE>exit</CODE></A> (or when<A HREF="lib_over.html#main"><CODE>main</CODE></A> returns)and returns zero if successful.The functions are called in reverse order of registry. You can register atleast 32 functions.</P><P>Furthermore, in C++, if control leaves a called function becauseit fails to handle a thrown exception, <CODE>terminate</CODE>is called.</P><H2><A NAME="atof"><CODE>atof</CODE></A></H2><PRE>double <B>atof</B>(const char *s);</PRE><P>The function converts the initial characters of the string <CODE>s</CODE>to an equivalent value <CODE>x</CODE> of type <I>double</I> and then returns<CODE>x.</CODE> The conversion is the same as for<CODE><A HREF="#strtod">strtod</A>(s, 0)</CODE>,except that a value is not necessarily stored in<A HREF="errno.html#errno"><CODE>errno</CODE></A>if a conversion error occurs.</P><H2><A NAME="atoi"><CODE>atoi</CODE></A></H2><PRE>int <B>atoi</B>(const char *s);</PRE><P>The function converts the initial characters of the string <CODE>s</CODE>to an equivalent value <CODE>x</CODE> of type <I>int</I> and then returns<CODE>x.</CODE> The conversion is the same as for<CODE>(int)<A HREF="#strtol">strtol</A>(s, 0, 10)</CODE>,except that a value is not necessarily stored in<A HREF="errno.html#errno"><CODE>errno</CODE></A>if a conversion error occurs.</P><H2><A NAME="atol"><CODE>atol</CODE></A></H2><PRE>long <B>atol</B>(const char *s);</PRE><P>The function converts the initial characters of the string <CODE>s</CODE>to an equivalent value <CODE>x</CODE> of type <I>long</I> and then returns<CODE>x.</CODE> The conversion is the same as for<CODE><A HREF="#strtol">strtol</A>(s, 0, 10)</CODE>,except that a value is not necessarily stored in<A HREF="errno.html#errno"><CODE>errno</CODE></A>if a conversion error occurs.</P><H2><A NAME="bsearch"><CODE>bsearch</CODE></A></H2><PRE>extern "C++" void *<B>bsearch</B>(const void *key, const void *base, size_t nelem, size_t size, int (*cmp)(const void *ck, const void *ce)); <B>[C++ only]</B>extern "C" <B>[C++ only]</B> void *<B>bsearch</B>(const void *key, const void *base, size_t nelem, size_t size, int (*cmp)(const void *ck, const void *ce));</PRE><P>The function searches an array of ordered values and returnsthe address of an array element that equals the search key <CODE>key</CODE>(if one exists); otherwise, it returns a null pointer. The array consistsof <CODE>nelem</CODE> elements, each of <CODE>size</CODE> bytes,beginning with the element whose address is <CODE>base</CODE>.</P><P><CODE>bsearch</CODE> calls the comparison function whose address is<CODE>cmp</CODE> to compare the search key with elements of the array. Thecomparison function must return:</P><UL><LI>a negative value if the search key <CODE>ck</CODE> is less than thearray element <CODE>ce</CODE><LI>zero if the two are equal<LI>a positive value if the search key is greater than the arrayelement</UL><P><CODE>bsearch</CODE> assumes that the array elements are in ascendingorder according to the same comparison rules that are used by thecomparison function.</P><H2><A NAME="calloc"><CODE>calloc</CODE></A></H2>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -