1strtoul(3) Library Functions Manual strtoul(3)
2
3
4
6 strtoul, strtoull, strtouq - convert a string to an unsigned long inte‐
7 ger
8
10 Standard C library (libc, -lc)
11
13 #include <stdlib.h>
14
15 unsigned long strtoul(const char *restrict nptr,
16 char **restrict endptr, int base);
17 unsigned long long strtoull(const char *restrict nptr,
18 char **restrict endptr, int base);
19
20 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
21
22 strtoull():
23 _ISOC99_SOURCE
24 || /* glibc <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE
25
27 The strtoul() function converts the initial part of the string in nptr
28 to an unsigned long value according to the given base, which must be
29 between 2 and 36 inclusive, or be the special value 0.
30
31 The string may begin with an arbitrary amount of white space (as deter‐
32 mined by isspace(3)) followed by a single optional '+' or '-' sign. If
33 base is zero or 16, the string may then include a "0x" prefix, and the
34 number will be read in base 16; otherwise, a zero base is taken as 10
35 (decimal) unless the next character is '0', in which case it is taken
36 as 8 (octal).
37
38 The remainder of the string is converted to an unsigned long value in
39 the obvious manner, stopping at the first character which is not a
40 valid digit in the given base. (In bases above 10, the letter 'A' in
41 either uppercase or lowercase represents 10, 'B' represents 11, and so
42 forth, with 'Z' representing 35.)
43
44 If endptr is not NULL, strtoul() stores the address of the first in‐
45 valid character in *endptr. If there were no digits at all, strtoul()
46 stores the original value of nptr in *endptr (and returns 0). In par‐
47 ticular, if *nptr is not '\0' but **endptr is '\0' on return, the en‐
48 tire string is valid.
49
50 The strtoull() function works just like the strtoul() function but re‐
51 turns an unsigned long long value.
52
54 The strtoul() function returns either the result of the conversion or,
55 if there was a leading minus sign, the negation of the result of the
56 conversion represented as an unsigned value, unless the original (non‐
57 negated) value would overflow; in the latter case, strtoul() returns
58 ULONG_MAX and sets errno to ERANGE. Precisely the same holds for str‐
59 toull() (with ULLONG_MAX instead of ULONG_MAX).
60
62 EINVAL (not in C99) The given base contains an unsupported value.
63
64 ERANGE The resulting value was out of range.
65
66 The implementation may also set errno to EINVAL in case no conversion
67 was performed (no digits seen, and 0 returned).
68
70 For an explanation of the terms used in this section, see at‐
71 tributes(7).
72
73 ┌─────────────────────────────────────┬───────────────┬────────────────┐
74 │Interface │ Attribute │ Value │
75 ├─────────────────────────────────────┼───────────────┼────────────────┤
76 │strtoul(), strtoull(), strtouq() │ Thread safety │ MT-Safe locale │
77 └─────────────────────────────────────┴───────────────┴────────────────┘
78
80 C11, POSIX.1-2008.
81
83 strtoul()
84 POSIX.1-2001, C89, SVr4.
85
86 strtoull()
87 POSIX.1-2001, C99.
88
90 Since strtoul() can legitimately return 0 or ULONG_MAX (ULLONG_MAX for
91 strtoull()) on both success and failure, the calling program should set
92 errno to 0 before the call, and then determine if an error occurred by
93 checking whether errno has a nonzero value after the call.
94
95 In locales other than the "C" locale, other strings may be accepted.
96 (For example, the thousands separator of the current locale may be sup‐
97 ported.)
98
99 BSD also has
100
101 u_quad_t strtouq(const char *nptr, char **endptr, int base);
102
103 with completely analogous definition. Depending on the wordsize of the
104 current architecture, this may be equivalent to strtoull() or to str‐
105 toul().
106
107 Negative values are considered valid input and are silently converted
108 to the equivalent unsigned long value.
109
111 See the example on the strtol(3) manual page; the use of the functions
112 described in this manual page is similar.
113
115 a64l(3), atof(3), atoi(3), atol(3), strtod(3), strtol(3), strtoumax(3)
116
117
118
119Linux man-pages 6.04 2023-03-30 strtoul(3)