1STRTOUL(3) Linux Programmer's Manual STRTOUL(3)
2
3
4
6 strtoul, strtoull, strtouq - convert a string to an unsigned long inte‐
7 ger
8
10 #include <stdlib.h>
11
12 unsigned long int strtoul(const char *nptr, char **endptr, int base);
13
14 unsigned long long int strtoull(const char *nptr, char **endptr,
15 int base);
16
17 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
18
19 strtoull(): XOPEN_SOURCE >= 600 || _BSD_SOURCE || _SVID_SOURCE ||
20 _ISOC99_SOURCE; or cc -std=c99
21
23 The strtoul() function converts the initial part of the string in nptr
24 to an unsigned long int value according to the given base, which must
25 be between 2 and 36 inclusive, or be the special value 0.
26
27 The string may begin with an arbitrary amount of white space (as deter‐
28 mined by isspace(3)) followed by a single optional '+' or '-' sign. If
29 base is zero or 16, the string may then include a "0x" prefix, and the
30 number will be read in base 16; otherwise, a zero base is taken as 10
31 (decimal) unless the next character is '0', in which case it is taken
32 as 8 (octal).
33
34 The remainder of the string is converted to an unsigned long int value
35 in the obvious manner, stopping at the first character which is not a
36 valid digit in the given base. (In bases above 10, the letter 'A' in
37 either upper or lower case represents 10, 'B' represents 11, and so
38 forth, with 'Z' representing 35.)
39
40 If endptr is not NULL, strtoul() stores the address of the first
41 invalid character in *endptr. If there were no digits at all, str‐
42 toul() stores the original value of nptr in *endptr (and returns 0).
43 In particular, if *nptr is not '\0' but **endptr is '\0' on return, the
44 entire string is valid.
45
46 The strtoull() function works just like the strtoul() function but
47 returns an unsigned long long int value.
48
50 The strtoul() function returns either the result of the conversion or,
51 if there was a leading minus sign, the negation of the result of the
52 conversion represented as an unsigned value, unless the original (non-
53 negated) value would overflow; in the latter case, strtoul() returns
54 ULONG_MAX and sets errno to ERANGE. Precisely the same holds for str‐
55 toull() (with ULLONG_MAX instead of ULONG_MAX).
56
58 EINVAL (not in C99) The given base contains an unsupported value.
59
60 ERANGE The resulting value was out of range.
61
62 The implementation may also set errno to EINVAL in case no conversion
63 was performed (no digits seen, and 0 returned).
64
66 strtoul() conforms to SVr4, C89, C99 and POSIX-2001, and strtoull() to
67 C99 and POSIX.1-2001.
68
70 Since strtoul() can legitimately return 0 or LONG_MAX (LLONG_MAX for
71 strtoull()) on both success and failure, the calling program should set
72 errno to 0 before the call, and then determine if an error occurred by
73 checking whether errno has a non-zero value after the call.
74
75 In locales other than the "C" locale, other strings may be accepted.
76 (For example, the thousands separator of the current locale may be sup‐
77 ported.)
78
79 BSD also has
80
81 u_quad_t strtouq(const char *nptr, char **endptr, int base);
82
83 with completely analogous definition. Depending on the wordsize of the
84 current architecture, this may be equivalent to strtoull() or to str‐
85 toul().
86
87 Negative values are considered valid input and are silently converted
88 to the equivalent unsigned long int value.
89
91 See the example on the strtol(3) manual page; the use of the functions
92 described in this manual page is similar.
93
95 atof(3), atoi(3), atol(3), strtod(3), strtol(3)
96
98 This page is part of release 3.22 of the Linux man-pages project. A
99 description of the project, and information about reporting bugs, can
100 be found at http://www.kernel.org/doc/man-pages/.
101
102
103
104GNU 2007-07-26 STRTOUL(3)