strtol
convert a string value to a <span class="Vt">long</span>, <span class="Vt">long long</span>, <span class="Vt">intmax_t</span> or <span class="Vt">quad_t</span> integer
You're viewing an older version of this page (#3290). View the current version.
NAME
<span class="Nm" id="strtol">strtol</span>, <span class="Nm" id="strtoll">strtoll</span>, <span class="Nm" id="strtoimax">strtoimax</span>, <span class="Nm" id="strtoq">strtoq</span>
INTERFACE
#include <stdlib.h> #include <limits.h> long strtol ( const char * restrict nptr, char ** restrict endptr, int base ) long strtoll ( const char * restrict nptr, char ** restrict endptr, int base ) #include <inttypes.h> intmax_t strtoimax ( const char * restrict nptr, char ** restrict endptr, int base ) #include <sys/types.h> #include <stdlib.h> #include <limits.h> quad_t strtoq ( const char *nptr, char **endptr, int base )
DESCRIPTION
The strtol function converts the string in \c nptr to a <span class="Vt">long</span> value. The strtoll function converts the string in \c nptr to a <span class="Vt">long long</span> value. The strtoimax function converts the string in \c nptr to an <span class="Vt">intmax_t</span> value. The strtoq function converts the string in \c nptr to a <span class="Vt">quad_t</span> value. The conversion is done according to the given \c base, which must be between 2 and 36 inclusive, or be the special value 0. \n \n The string may begin with an arbitrary amount of white space (as determined by reference:isspace (3) ) followed by a single optional '<span class="Qlq">+</span>' or '<span class="Qlq">-</span>' sign. If \c base is zero or 16, the string may then include a "<span class="Dq"><span class="Li">0x</span></span>" prefix, and the number will be read in base 16; otherwise, a zero \c base is taken as 10 (decimal) unless the next character is '<span class="Qlq">0</span>', in which case it is taken as 8 (octal). \n \n The remainder of the string is converted to a <span class="Vt">long</span>, <span class="Vt">long long</span>, <span class="Vt">intmax_t</span> or <span class="Vt">quad_t</span> value in the obvious manner, stopping at the first character which is not a valid digit in the given base. (In bases above 10, the letter '<span class="Qlq">A</span>' in either upper or lower case represents 10, '<span class="Qlq">B</span>' represents 11, and so forth, with '<span class="Qlq">Z</span>' representing 35.) \n \n If \c endptr is not <span class="Dv">NULL</span>, strtol stores the address of the first invalid character in \c *endptr. If there were no digits at all, however, strtol stores the original value of \c nptr in \c *endptr. (Thus, if \c *nptr is not '<span class="Qlq">\\0</span>' but \c **endptr is '<span class="Qlq">\\0</span>' on return, the entire string was valid.) \n \n Extended locale versions of these functions are documented in reference:strtol_l (3) . See reference:xlocale (3) for more information.
RETURN VALUES
The strtol , strtoll , strtoimax and strtoq functions return the result of the conversion, unless the value would underflow or overflow. If no conversion could be performed, 0 is returned and the global variable <span class="Va">errno</span> is set to <span class="Er">EINVAL</span>. If an overflow or underflow occurs, <span class="Va">errno</span> is set to <span class="Er">ERANGE</span> and the function return value is clamped according to the following table. <table cellspacing="0" class="refpage" style="margin-left:25px"> <tr> <td valign="top" nowrap> <span class="Sy">Function</span> </td> <td valign="top"> <span class="Sy">overflow</span> </td> <td valign="top"> <span class="Sy">underflow</span> </td> </tr> <tr> <td valign="top" nowrap> strtol </td> <td valign="top"> <span class="Dv">LONG_MIN</span> </td> <td valign="top"> <span class="Dv">LONG_MAX</span> </td> </tr> <tr> <td valign="top" nowrap> strtoll </td> <td valign="top"> <span class="Dv">LLONG_MIN</span> </td> <td valign="top"> <span class="Dv">LLONG_MAX</span> </td> </tr> <tr> <td valign="top" nowrap> strtoimax </td> <td valign="top"> <span class="Dv">INTMAX_MIN</span> </td> <td valign="top"> <span class="Dv">INTMAX_MAX</span> </td> </tr> <tr> <td valign="top" nowrap> strtoq </td> <td valign="top"> <span class="Dv">LLONG_MIN</span> </td> <td valign="top"> <span class="Dv">LLONG_MAX</span> </td> </tr> </table>
ERRORS
<table cellspacing="0" class="refpage" style="margin-left:25px"> <tr> <td valign="top" nowrap> [<span class="Bq"><span class="Er">EINVAL</span></span>] </td> <td valign="top"> The value of \c base is not supported or no conversion could be performed. </td> </tr> <tr> <td valign="top" nowrap> [<span class="Bq"><span class="Er">ERANGE</span></span>] </td> <td valign="top"> The given string was out of range; the value converted has been clamped. </td> </tr> </table>
SEE ALSO
atof (3) , atoi (3) , atol (3) , strtod (3) , strtoul (3) , reference:wcstol (3) , reference:strtol_l (3)
STANDARDS
The strtol function conforms to The strtoll and strtoimax functions conform to The BSD strtoq function is deprecated.