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
13 strtoul(const char *nptr, char **endptr, int base);
14
15 unsigned long long int
16 strtoull(const char *nptr, char **endptr, int base);
17
19 The strtoul() function converts the initial part of the string in nptr
20 to an unsigned long integer value according to the given base, which
21 must be between 2 and 36 inclusive, or be the special value 0.
22
23 The string may begin with an arbitrary amount of white space (as deter‐
24 mined by isspace(3)) followed by a single optional `+' or `-' sign. If
25 base is zero or 16, the string may then include a `0x' prefix, and the
26 number will be read in base 16; otherwise, a zero base is taken as 10
27 (decimal) unless the next character is `0', in which case it is taken
28 as 8 (octal).
29
30 The remainder of the string is converted to an unsigned long int value
31 in the obvious manner, stopping at the first character which is not a
32 valid digit in the given base. (In bases above 10, the letter `A' in
33 either upper or lower case represents 10, `B' represents 11, and so
34 forth, with `Z' representing 35.)
35
36 If endptr is not NULL, strtoul() stores the address of the first
37 invalid character in *endptr. If there were no digits at all, str‐
38 toul() stores the original value of nptr in *endptr (and returns 0).
39 In particular, if *nptr is not `\0' but **endptr is `\0' on return, the
40 entire string is valid.
41
42 The strtoull() function works just like the strtoul() function but
43 returns an unsigned long long integer value.
44
46 The strtoul() function returns either the result of the conversion or,
47 if there was a leading minus sign, the negation of the result of the
48 conversion represented as an unsigned value, unless the original (non-
49 negated) value would overflow; in the latter case, strtoul() returns
50 ULONG_MAX and sets the global variable errno to ERANGE. Precisely the
51 same holds for strtoull() (with ULLONG_MAX instead of ULONG_MAX).
52
54 EINVAL (not in C99) The given base contains an unsupported value.
55
56 ERANGE The resulting value was out of range.
57
58 The implementation may also set errno to EINVAL in case no conversion
59 was performed (no digits seen, and 0 returned).
60
62 Since strtoul() can legitimately return 0 or LONG_MAX (LLONG_MAX for
63 strtoull()) on both success and failure, the calling program should set
64 errno to 0 before the call, and then determine if an error occurred by
65 checking whether errno has a non-zero value after the call.
66
67 In locales other than the "C" locale, other strings may be accepted.
68 (For example, the thousands separator of the current locale may be sup‐
69 ported.)
70
71 BSD also has
72
73 u_quad_t
74 strtouq(const char *nptr, char **endptr, int base);
75
76 with completely analogous definition. Depending on the wordsize of the
77 current architecture, this may be equivalent to strtoull() or to str‐
78 toul().
79
80 Negative values are considered valid input and are silently converted
81 to the equivalent unsigned long value.
82
84 strtoul() conforms to SVr4, C89, C99 and POSIX-2001, and strtoull() to
85 C99 and POSIX.1-2001.
86
88 See the example on the strtol(3) manual page; the use of the functions
89 described in this manual page is similar.
90
92 atof(3), atoi(3), atol(3), strtod(3), strtol(3)
93
94
95
96GNU 2002-05-30 STRTOUL(3)