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():
20 _ISOC99_SOURCE ||
21 || /* Glibc versions <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE
22
24 The strtoul() function converts the initial part of the string in nptr
25 to an unsigned long int value according to the given base, which must
26 be between 2 and 36 inclusive, or be the special value 0.
27
28 The string may begin with an arbitrary amount of white space (as deter‐
29 mined by isspace(3)) followed by a single optional '+' or '-' sign. If
30 base is zero or 16, the string may then include a "0x" prefix, and the
31 number will be read in base 16; otherwise, a zero base is taken as 10
32 (decimal) unless the next character is '0', in which case it is taken
33 as 8 (octal).
34
35 The remainder of the string is converted to an unsigned long int value
36 in the obvious manner, stopping at the first character which is not a
37 valid digit in the given base. (In bases above 10, the letter 'A' in
38 either uppercase or lowercase represents 10, 'B' represents 11, and so
39 forth, with 'Z' representing 35.)
40
41 If endptr is not NULL, strtoul() stores the address of the first
42 invalid character in *endptr. If there were no digits at all, str‐
43 toul() stores the original value of nptr in *endptr (and returns 0).
44 In particular, if *nptr is not '\0' but **endptr is '\0' on return, the
45 entire string is valid.
46
47 The strtoull() function works just like the strtoul() function but
48 returns an unsigned long long int value.
49
51 The strtoul() function returns either the result of the conversion or,
52 if there was a leading minus sign, the negation of the result of the
53 conversion represented as an unsigned value, unless the original (non‐
54 negated) value would overflow; in the latter case, strtoul() returns
55 ULONG_MAX and sets errno to ERANGE. Precisely the same holds for str‐
56 toull() (with ULLONG_MAX instead of ULONG_MAX).
57
59 EINVAL (not in C99) The given base contains an unsupported value.
60
61 ERANGE The resulting value was out of range.
62
63 The implementation may also set errno to EINVAL in case no conversion
64 was performed (no digits seen, and 0 returned).
65
67 For an explanation of the terms used in this section, see
68 attributes(7).
69
70 ┌─────────────────────────────────┬───────────────┬────────────────┐
71 │Interface │ Attribute │ Value │
72 ├─────────────────────────────────┼───────────────┼────────────────┤
73 │strtoul(), strtoull(), strtouq() │ Thread safety │ MT-Safe locale │
74 └─────────────────────────────────┴───────────────┴────────────────┘
76 strtoul(): POSIX.1-2001, POSIX.1-2008, C89, C99 SVr4.
77
78 strtoull(): POSIX.1-2001, POSIX.1-2008, C99.
79
81 Since strtoul() can legitimately return 0 or ULONG_MAX (ULLONG_MAX for
82 strtoull()) on both success and failure, the calling program should set
83 errno to 0 before the call, and then determine if an error occurred by
84 checking whether errno has a nonzero value after the call.
85
86 In locales other than the "C" locale, other strings may be accepted.
87 (For example, the thousands separator of the current locale may be sup‐
88 ported.)
89
90 BSD also has
91
92 u_quad_t strtouq(const char *nptr, char **endptr, int base);
93
94 with completely analogous definition. Depending on the wordsize of the
95 current architecture, this may be equivalent to strtoull() or to str‐
96 toul().
97
98 Negative values are considered valid input and are silently converted
99 to the equivalent unsigned long int value.
100
102 See the example on the strtol(3) manual page; the use of the functions
103 described in this manual page is similar.
104
106 a64l(3), atof(3), atoi(3), atol(3), strtod(3), strtol(3)
107
109 This page is part of release 4.16 of the Linux man-pages project. A
110 description of the project, information about reporting bugs, and the
111 latest version of this page, can be found at
112 https://www.kernel.org/doc/man-pages/.
113
114
115
116GNU 2017-09-15 STRTOUL(3)