1STRTOUL(3)                 Linux Programmer's Manual                STRTOUL(3)
2
3
4

NAME

6       strtoul, strtoull, strtouq - convert a string to an unsigned long inte‐
7       ger
8

SYNOPSIS

10       #include <stdlib.h>
11
12       unsigned long strtoul(const char *restrict nptr,
13                             char **restrict endptr, int base);
14       unsigned long long strtoull(const char *restrict nptr,
15                             char **restrict endptr, int base);
16
17   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
18
19       strtoull():
20           _ISOC99_SOURCE
21               || /* Glibc <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE
22

DESCRIPTION

24       The strtoul() function converts the initial part of the string in  nptr
25       to  an  unsigned  long value according to the given base, which must be
26       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 value in
36       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 in‐
42       valid character in *endptr.  If there were no digits at all,  strtoul()
43       stores  the original value of nptr in *endptr (and returns 0).  In par‐
44       ticular, if *nptr is not '\0' but **endptr is '\0' on return,  the  en‐
45       tire string is valid.
46
47       The  strtoull() function works just like the strtoul() function but re‐
48       turns an unsigned long long value.
49

RETURN VALUE

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

ERRORS

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

ATTRIBUTES

67       For an  explanation  of  the  terms  used  in  this  section,  see  at‐
68       tributes(7).
69
70       ┌─────────────────────────────────────┬───────────────┬────────────────┐
71Interface                            Attribute     Value          
72       ├─────────────────────────────────────┼───────────────┼────────────────┤
73strtoul(), strtoull(), strtouq()     │ Thread safety │ MT-Safe locale │
74       └─────────────────────────────────────┴───────────────┴────────────────┘
75

CONFORMING TO

77       strtoul(): POSIX.1-2001, POSIX.1-2008, C89, C99 SVr4.
78
79       strtoull(): POSIX.1-2001, POSIX.1-2008, C99.
80

NOTES

82       Since  strtoul() can legitimately return 0 or ULONG_MAX (ULLONG_MAX for
83       strtoull()) on both success and failure, the calling program should set
84       errno  to 0 before the call, and then determine if an error occurred by
85       checking whether errno has a nonzero value after the call.
86
87       In locales other than the "C" locale, other strings  may  be  accepted.
88       (For example, the thousands separator of the current locale may be sup‐
89       ported.)
90
91       BSD also has
92
93           u_quad_t strtouq(const char *nptr, char **endptr, int base);
94
95       with completely analogous definition.  Depending on the wordsize of the
96       current  architecture,  this may be equivalent to strtoull() or to str‐
97       toul().
98
99       Negative values are considered valid input and are  silently  converted
100       to the equivalent unsigned long value.
101

EXAMPLES

103       See  the example on the strtol(3) manual page; the use of the functions
104       described in this manual page is similar.
105

SEE ALSO

107       a64l(3), atof(3), atoi(3), atol(3), strtod(3), strtol(3), strtoumax(3)
108

COLOPHON

110       This page is part of release 5.12 of the Linux  man-pages  project.   A
111       description  of  the project, information about reporting bugs, and the
112       latest    version    of    this    page,    can     be     found     at
113       https://www.kernel.org/doc/man-pages/.
114
115
116
117GNU                               2021-03-22                        STRTOUL(3)
Impressum