]>
Commit | Line | Data |
---|---|---|
fea681da MK |
1 | .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk) |
2 | .\" | |
3 | .\" Permission is granted to make and distribute verbatim copies of this | |
4 | .\" manual provided the copyright notice and this permission notice are | |
5 | .\" preserved on all copies. | |
6 | .\" | |
7 | .\" Permission is granted to copy and distribute modified versions of this | |
8 | .\" manual under the conditions for verbatim copying, provided that the | |
9 | .\" entire resulting derived work is distributed under the terms of a | |
10 | .\" permission notice identical to this one. | |
11 | .\" | |
12 | .\" Since the Linux kernel and libraries are constantly changing, this | |
13 | .\" manual page may be incorrect or out-of-date. The author(s) assume no | |
14 | .\" responsibility for errors or omissions, or for damages resulting from | |
15 | .\" the use of the information contained herein. The author(s) may not | |
16 | .\" have taken the same level of care in the production of this manual, | |
17 | .\" which is licensed free of charge, as they might when working | |
18 | .\" professionally. | |
19 | .\" | |
20 | .\" Formatted or processed versions of this manual, if unaccompanied by | |
21 | .\" the source, must acknowledge the copyright and authors of this work. | |
22 | .\" | |
23 | .\" References consulted: | |
24 | .\" Linux libc source code | |
25 | .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991) | |
26 | .\" 386BSD man pages | |
27 | .\" Modified Sun Jul 25 10:54:03 1993 by Rik Faith (faith@cs.unc.edu) | |
28 | .\" Fixed typo, aeb, 950823 | |
29 | .\" 2002-02-22, joey, mihtjel: Added strtoull() | |
30 | .\" | |
31 | .TH STRTOUL 3 2002-05-30 "GNU" "Linux Programmer's Manual" | |
32 | .SH NAME | |
33 | strtoul, strtoull, strtouq \- convert a string to an unsigned long integer | |
34 | .SH SYNOPSIS | |
35 | .nf | |
36 | .B #include <stdlib.h> | |
37 | .sp | |
38 | .BI "unsigned long int" | |
39 | .BI "strtoul(const char *" nptr ", char **" endptr ", int " base ); | |
40 | .sp | |
41 | .BI "unsigned long long int" | |
42 | .BI "strtoull(const char *" nptr ", char **" endptr ", int " base ); | |
43 | .fi | |
44 | .SH DESCRIPTION | |
63aa9df0 | 45 | The \fBstrtoul\fP() function converts the initial part of the string |
fea681da MK |
46 | in \fInptr\fP to an unsigned long integer value according to the |
47 | given \fIbase\fP, which must be between 2 and 36 inclusive, or be | |
48 | the special value 0. | |
49 | .PP | |
3f266a43 | 50 | The string may begin with an arbitrary amount of white space (as |
fea681da MK |
51 | determined by |
52 | .BR isspace (3)) | |
8c383102 | 53 | followed by a single optional `+' or `\-' |
fea681da MK |
54 | sign. If \fIbase\fP is zero or 16, the string may then include a |
55 | `0x' prefix, and the number will be read in base 16; otherwise, a | |
56 | zero \fIbase\fP is taken as 10 (decimal) unless the next character | |
57 | is `0', in which case it is taken as 8 (octal). | |
58 | .PP | |
59 | The remainder of the string is converted to an unsigned long int value | |
60 | in the obvious manner, stopping at the first character which is not a | |
61 | valid digit in the given base. (In bases above 10, the letter `A' in | |
62 | either upper or lower case represents 10, `B' represents 11, and so | |
63 | forth, with `Z' representing 35.) | |
64 | .PP | |
63aa9df0 | 65 | If \fIendptr\fP is not NULL, \fBstrtoul\fP() stores the address of the |
fea681da | 66 | first invalid character in \fI*endptr\fP. If there were no digits at |
63aa9df0 | 67 | all, \fBstrtoul\fP() stores the original value of \fInptr\fP in |
fea681da MK |
68 | \fI*endptr\fP (and returns 0). |
69 | In particular, if \fI*nptr\fP is not `\\0' but \fI**endptr\fP | |
70 | is `\\0' on return, the entire string is valid. | |
71 | .PP | |
72 | The | |
63aa9df0 | 73 | .BR strtoull () |
fea681da | 74 | function works just like the |
63aa9df0 | 75 | .BR strtoul () |
fea681da MK |
76 | function but returns an unsigned long long integer value. |
77 | .SH "RETURN VALUE" | |
63aa9df0 | 78 | The \fBstrtoul\fP() function returns either the result of the conversion |
fea681da | 79 | or, if there was a leading minus sign, the negation of the result of the |
190c7abe MK |
80 | conversion represented as an unsigned value, |
81 | unless the original (non-negated) value would overflow; in | |
63aa9df0 | 82 | the latter case, \fBstrtoul\fP() returns ULONG_MAX and sets the global |
fea681da MK |
83 | variable \fIerrno\fP to ERANGE. |
84 | Precisely the same holds for | |
63aa9df0 | 85 | .BR strtoull () |
fea681da MK |
86 | (with ULLONG_MAX instead of ULONG_MAX). |
87 | .SH ERRORS | |
88 | .TP | |
89 | .B EINVAL | |
90 | (not in C99) | |
91 | The given | |
92 | .I base | |
93 | contains an unsupported value. | |
94 | .TP | |
95 | .B ERANGE | |
96 | The resulting value was out of range. | |
97 | .LP | |
98 | The implementation may also set \fIerrno\fP to \fBEINVAL\fP in case | |
99 | no conversion was performed (no digits seen, and 0 returned). | |
100 | .SH NOTES | |
fefe023e MK |
101 | Since |
102 | .BR strtoul () | |
8699b7dd | 103 | can legitimately return 0 or LONG_MAX (LLONG_MAX for |
fefe023e MK |
104 | .BR strtoull ()) |
105 | on both success and failure, the calling program should set | |
106 | .I errno | |
107 | to 0 before the call, | |
108 | and then determine if an error occurred by checking whether | |
109 | .I errno | |
110 | has a non-zero value after the call. | |
111 | ||
190c7abe | 112 | In locales other than the "C" locale, other strings may be accepted. |
fea681da MK |
113 | (For example, the thousands separator of the current locale may be |
114 | supported.) | |
115 | .LP | |
116 | BSD also has | |
117 | .sp | |
118 | .in +4n | |
119 | .nf | |
120 | .BI "u_quad_t" | |
121 | .BI "strtouq(const char *" nptr ", char **" endptr ", int base" ); | |
122 | .sp | |
123 | .in -4n | |
124 | .fi | |
125 | with completely analogous definition. | |
126 | Depending on the wordsize of the current architecture, this | |
127 | may be equivalent to | |
63aa9df0 | 128 | .BR strtoull () |
fea681da | 129 | or to |
63aa9df0 | 130 | .BR strtoul (). |
190c7abe MK |
131 | |
132 | Negative values are considered valid input and are | |
133 | silently converted to the equivalent unsigned long value. | |
fea681da | 134 | .SH "CONFORMING TO" |
63aa9df0 | 135 | .BR strtoul () |
1eb85d14 | 136 | conforms to SVr4, C89, C99 and POSIX-2001, and |
63aa9df0 | 137 | .BR strtoull () |
68e1685c | 138 | to C99 and POSIX.1-2001. |
5a1cae6b MK |
139 | .SH EXAMPLE |
140 | See the example on the | |
141 | .BR strtol (3) | |
142 | manual page; | |
143 | the use of the functions described in this manual page is similar. | |
fea681da MK |
144 | .SH "SEE ALSO" |
145 | .BR atof (3), | |
146 | .BR atoi (3), | |
147 | .BR atol (3), | |
148 | .BR strtod (3), | |
149 | .BR strtol (3) |