strtoul
convert a string to an <span class="Vt">unsigned long</span>, <span class="Vt">unsigned long long</span>, <span class="Vt">uintmax_t</span>, or <span class="Vt">u_quad_t</span> integer
You're viewing an older version of this page (#3291). View the current version.
NAME
<span class="Nm" id="strtoul">strtoul</span>, <span class="Nm" id="strtoull">strtoull</span>, <span class="Nm" id="strtoumax">strtoumax</span>, <span class="Nm" id="strtouq">strtouq</span>
INTERFACE
#include <stdlib.h> #include <limits.h> unsigned long strtoul ( const char * restrict nptr, char ** restrict endptr, int base ) unsigned long long strtoull ( const char * restrict nptr, char ** restrict endptr, int base ) #include <inttypes.h> uintmax_t strtoumax ( const char * restrict nptr, char ** restrict endptr, int base ) #include <sys/types.h> #include <stdlib.h> #include <limits.h> u_quad_t strtouq ( const char *nptr, char **endptr, int base )
DESCRIPTION
The strtoul function converts the string in \c nptr to an <span class="Vt">unsigned long</span> value. The strtoull function converts the string in \c nptr to an <span class="Vt">unsigned long long</span> value. The strtoumax function converts the string in \c nptr to an <span class="Vt">uintmax_t</span> value. The strtouq function converts the string in \c nptr to a <span class="Vt">u_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 an <span class="Vt">unsigned long</span> value in the obvious manner, stopping at the end of the string or at the first character that does not produce 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>, strtoul stores the address of the first invalid character in \c *endptr. If there were no digits at all, however, strtoul 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.)
RETURN VALUES
The strtoul , strtoull , strtoumax and strtouq functions return either the result of the conversion or, if there was a leading minus sign, the negation of the result of the conversion, unless the original (non-negated) value would overflow; in the latter case, strtoul returns <span class="Dv">ULONG_MAX</span>, strtoull returns <span class="Dv">ULLONG_MAX</span>, strtoumax returns <span class="Dv">UINTMAX_MAX</span>, and strtouq returns <span class="Dv">ULLONG_MAX</span>. In all cases, <span class="Va">errno</span> is set to <span class="Er">ERANGE</span>. 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>.
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
strtol (3) , reference:wcstoul (3) , reference:strtol_l (3)
STANDARDS
The strtoul function conforms to The strtoull and strtoumax functions conform to The BSD strtouq function is deprecated.