1 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
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.
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.
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
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" References consulted:
24 .\" Linux libc source code
25 .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
27 .\" Modified Sun Jul 25 10:53:39 1993 by Rik Faith (faith@cs.unc.edu)
28 .\" Added correction due to nsd@bbc.com (Nick Duffek) - aeb, 950610
30 .\" Japanese Version Copyright (c) 1999 AKAMATSU, Kazuo
31 .\" all rights reserved.
32 .\" Translated Thu Jan 9 21:00:00 JST 1999
33 .\" by AKAMATSU, Kazuo
34 .\" Updated Sun Apr 8 JST 2001 by Kentaro Shirakata <argrath@ub32.org>
35 .\" Updated Sat Mar 23 JST 2002 by Kentaro Shirakata <argrath@ub32.org>
36 .\" Updated & Modified 2006-07-20,
37 .\" Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v2.36
41 .TH STRTOL 3 2010-09-20 "GNU" "Linux Programmer's Manual"
43 .\"O strtol, strtoll, strtoq \- convert a string to a long integer
44 strtol, strtoll, strtoq \- ʸ»úÎó¤ò long int ¤ËÊÑ´¹¤¹¤ë
47 .B #include <stdlib.h>
49 .BI "long int strtol(const char *" nptr ", char **" endptr ", int " base );
51 .BI "long long int strtoll(const char *" nptr ", char **" endptr \
56 .\"O Feature Test Macro Requirements for glibc (see
57 .\"O .BR feature_test_macros (7)):
58 glibc ¸þ¤±¤Îµ¡Ç½¸¡ºº¥Þ¥¯¥í¤ÎÍ×·ï
59 .RB ( feature_test_macros (7)
66 XOPEN_SOURCE\ >=\ 600 || _BSD_SOURCE || _SVID_SOURCE || _ISOC99_SOURCE ||
67 _POSIX_C_SOURCE\ >=\ 200112L;
77 .\"O function converts the initial part of the string
78 .\"O in \fInptr\fP to a long integer value according to the given \fIbase\fP,
79 .\"O which must be between 2 and 36 inclusive, or be the special value 0.
81 ´Ø¿ô¤Ï¡¢ \fInptr\fP ¤Îʸ»úÎó¤ÎºÇ½é¤ÎÉôʬ¤ò¡¢
82 \fIbase\fP ¤ò´ð¿ô¤È¤·¤Æ long int ¤ËÊÑ´¹¤¹¤ë¡£
83 ¤³¤Î \fIbase\fP ¤Ï 2 ¤«¤é 36 ¤Þ¤Ç¤ÎÃÍ
84 ¤¢¤ë¤¤¤ÏÆÃÊ̤ʰÕÌ£¤ò»ý¤ÄÃÍ 0 ¤Ç¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
86 .\"O The string may begin with an arbitrary amount of white space (as
89 .\"O followed by a single optional \(aq+\(aq or \(aq\-\(aq sign.
90 .\"O If \fIbase\fP is zero or 16, the string may then include a
91 .\"O "0x" prefix, and the number will be read in base 16; otherwise, a
92 .\"O zero \fIbase\fP is taken as 10 (decimal) unless the next character
93 .\"O is \(aq0\(aq, in which case it is taken as 8 (octal).
94 ʸ»úÎó¤ÎÀèƬ¤Ë¤Ï¡¢Ç¤°Õ¤Î¿ô¤Î¶õÇò¤¬¤¢¤Ã¤Æ¤â¤è¤¯ (¶õÇò¤Ï
96 ¤ÇȽÄꤵ¤ì¤ë)¡¢¤Þ¤¿¿ô»ú¤ÎľÁ°¤Ë¤Ï \(aq+\(aq ¤« \(aq\-\(aq ¤Î
98 \fIbase\fP ¤¬ 0 ¤Þ¤¿¤Ï 16 ¤Î¾ì¹ç¤Ë¤Ï¡¢Ê¸»úÎó¤ÎÀèƬ¤Ë "0x" ¤òÃÖ¤¯¤³¤È¤¬
99 ¤Ç¤¡¢¤½¤Î¾ì¹ç¤Ë¤Ïʸ»úÎó¤Ï 16¿Ê¿ô¤È¤·¤Æ°·¤ï¤ì¤ë¡£
100 ¤³¤ì°Ê³°¤Îʸ»úÎó¤Ç \fIbase\fP ¤¬ 0 ¤Î¾ì¹ç¤Ï¡¢
101 ʸ»úÎó¤¬ \(aq0\(aq ¤Ç»Ï¤Þ¤ë¤È¤¤Ï 8¿Ê¿ô¤È¤·¤Æ¡¢
102 ¤½¤ì°Ê³°¤Î¤È¤¤Ï 10¿Ê¿ô¤È¤·¤Æ°·¤ï¤ì¤ë¡£
104 .\"O The remainder of the string is converted to a
107 .\"O in the obvious manner, stopping at the first character which is not a
108 .\"O valid digit in the given base.
109 .\"O (In bases above 10, the letter \(aqA\(aq in
110 .\"O either upper or lower case represents 10, \(aqB\(aq represents 11, and so
111 .\"O forth, with \(aqZ\(aq representing 35.)
114 ¤ËÊÑ´¹¤µ¤ì¤ë¤¬¡¢´ð¿ô¤ËÂФ·¤Æ
115 ͸ú¤Ç¤Ê¤¤¿ô»ú¤¬¸½¤ì¤¿»þÅÀ¤ÇÊÑ´¹¤Ï½ªÎ»¤¹¤ë¡£(11¿Ê¿ô°Ê¾å¤Ç¤Ï \(aqA\(aq ¤Ï
116 Âçʸ»ú¡¦¾®Ê¸»ú¤Ë´Ø¤ï¤é¤º 10 ¤òɽ¤·¡¢ \(aqB\(aq ¤Ï 11 ¤òɽ¸½¤·¡¢
117 °Ê²¼Æ±Íͤˡ¢ \(aqZ\(aq ¤Ï 35 ¤òɽ¤¹¡£)
119 .\"O If \fIendptr\fP is not NULL,
121 .\"O stores the address of the
122 .\"O first invalid character in \fI*endptr\fP.
123 .\"O If there were no digits at
126 .\"O stores the original value of \fInptr\fP in
127 .\"O \fI*endptr\fP (and returns 0).
128 \fIendptr\fP ¤¬¥Ì¥ëÃÍ (NULL) ¤Ç¤Ê¤¤¾ì¹ç¤Ï¡¢ºÇ½é¤Ë¸½¤ì¤¿ÉÔÀµ¤Êʸ»ú¤¬
130 ¤Ë¤è¤Ã¤Æ \fI*endptr\fP ¤ËÊݸ¤µ¤ì¤Æ¤¤¤ë¡£
131 ʸ»úÎó¤Ë͸ú¤Ê¿ô»ú¤¬¤Ò¤È¤Ä¤â¤Ê¤±¤ì¤Ð¡¢
134 ¤Î¸µ¤ÎÃͤò \fI*endptr\fP ¤ËÂåÆþ¤¹¤ë (¤½¤·¤Æ 0 ¤òÊÖ¤¹)¡£
135 .\"O In particular, if \fI*nptr\fP is not \(aq\\0\(aq but \fI**endptr\fP
136 .\"O is \(aq\\0\(aq on return, the entire string is valid.
137 Æäˡ¢\fI*nptr\fP ¤¬ \(aq\\0\(aq °Ê³°¤Ç¡¢ÊÖ¤µ¤ì¤¿ \fI**endptr\fP ¤¬
138 \(aq\\0\(aq ¤Ê¤é¤Ð¡¢Ê¸»úÎóÁ´ÂΤ¬Í¸ú¤À¤Ã¤¿¤³¤È¤Ë¤Ê¤ë¡£
142 .\"O function works just like the
144 .\"O function but returns a long long integer value.
148 ¤ÈƱÍͤÀ¤¬¡¢long long int ·¿¤ÎÃͤòÊÖ¤¹¡£
152 .\"O function returns the result of the conversion,
153 .\"O unless the value would underflow or overflow.
154 .\"O If an underflow occurs,
158 .\"O If an overflow occurs,
162 .\"O In both cases, \fIerrno\fP is set to
164 ¥¢¥ó¥À¡¼¥Õ¥í¡¼¤â¥ª¡¼¥Ð¡¼¥Õ¥í¡¼¤âµ¯¤¤Ê¤«¤Ã¤¿¾ì¹ç¡¢
167 ÊÑ´¹¤µ¤ì¤¿ÃͤòÊÖ¤¹¡£¥ª¡¼¥Ð¡¼¥Õ¥í¡¼¤·¤¿¾ì¹ç¤Ë¤Ï
170 ¥¢¥ó¥À¡¼¥Õ¥í¡¼¤·¤¿¾ì¹ç¤Ë¤Ï
172 ¤¬Ê֤롣¥ª¡¼¥Ð¡¼¥Õ¥í¡¼¡¢
173 ¥¢¥ó¥À¡¼¥Õ¥í¡¼¤Î¤¤¤º¤ì¤Î¾ì¹ç¤Ë¤â
174 Âç°èÊÑ¿ô \fIerrno\fP ¤Ë¤Ï
177 .\"O Precisely the same holds for
203 .\"O contains an unsupported value.
206 ¤¬Âбþ¤·¤Æ¤¤¤Ê¤¤ÃͤǤ¢¤ë¡£
209 .\"O The resulting value was out of range.
210 ·ë²Ì¤ÎÃͤ¬Èϰϳ°¤Ç¤¢¤ë¡£
212 .\"O The implementation may also set \fIerrno\fP to \fBEINVAL\fP in case
213 .\"O no conversion was performed (no digits seen, and 0 returned).
214 ¼ÂÁõ¤Ë¤è¤Ã¤Æ¤Ï¡¢ÊÑ´¹¤¬¹Ô¤ï¤ì¤Ê¤«¤Ã¤¿¾ì¹ç (¿ô»ú¤¬¤Ê¤¯¡¢0 ¤òÊÖ¤·¤¿¾ì¹ç)¡¢
215 \fIerrno\fP ¤Ë \fBEINVAL\fP ¤¬ÀßÄꤵ¤ì¤ë¾ì¹ç¤¬¤¢¤ë¡£
218 .\"O conforms to SVr4, 4.3BSD, C89, C99 and POSIX.1-2001, and
220 .\"O to C99 and POSIX.1-2001.
222 ¤Ï SVr4, 4.3BSD, C89, C99 ¤È POSIX.1-2001 ¤Ë½àµò¤·¤Æ¤¤¤ë¡£
224 ¤Ï C99 ¤È POSIX.1-2001 ¤Ë½àµò¤·¤Æ¤¤¤ë¡£
229 .\"O can legitimately return 0,
238 .\"O on both success and failure, the calling program should set
240 .\"O to 0 before the call,
241 .\"O and then determine if an error occurred by checking whether
243 .\"O has a nonzero value after the call.
245 ¤«¤é¤ÏÀ®¸ù¡¢¼ºÇԤɤÁ¤é¤Î¾ì¹ç¤Ç¤â
253 ¤¬ÊÖ¤ë²ÄǽÀ¤¬¤¢¤ë¤Î¤Ç¡¢
254 ¥×¥í¥°¥é¥à¤Ï´Ø¿ô¤ò¸Æ¤Ó½Ð¤¹Á°¤Ë
256 ¤ò 0 ¤ËÀßÄꤷ¡¢¸Æ¤Ó½Ð¤·¸å¤Ë
258 ¤¬ 0 °Ê³°¤ÎÃͤ«¤É¤¦¤«¤ò³Îǧ¤·¥¨¥é¡¼¤¬È¯À¸¤·¤¿¤«¤É¤¦¤«¤òȽÃǤ¹¤ë
261 .\"O In locales other than the "C" locale, other strings may also be accepted.
262 .\"O (For example, the thousands separator of the current locale may be
264 "C" °Ê³°¤Î¥í¥±¡¼¥ë¤Î¾ì¹ç¡¢¤½¤Î¾¤Îʸ»úÎó¤â¼õ¤±ÉÕ¤±¤é¤ì¤ë¤«¤â¤·¤ì¤Ê¤¤¡£
265 (Î㤨¤Ð¡¢¸½ºß¤Î¥í¥±¡¼¥ë¤Î 1000 Ëè¤Î¶èÀÚ¤êʸ»ú¤¬¥µ¥Ý¡¼¥È¤µ¤ì¤Æ¤¤¤ë¤«¤â¤·¤ì¤Ê¤¤¡£)
272 .BI "quad_t strtoq(const char *" nptr ", char **" endptr ", int " base );
276 .\"O with completely analogous definition.
277 ¤È¤¤¤¦´°Á´¤ËƱÍͤÎÄêµÁ¤ò»ý¤Ä´Ø¿ô¤¬¤¢¤ë¡£
278 .\"O Depending on the wordsize of the current architecture, this
279 .\"O may be equivalent to
283 »ÈÍÑÃæ¤Î¥¢¡¼¥¥Æ¥¯¥Á¥ã¤Î¥ï¡¼¥ÉĹ¼¡Âè¤Ç¤¢¤ë¤¬¡¢¤³¤Î´Ø¿ô¤Ï
287 ¤ÈÅù²Á¤È¤Ê¤ë¤³¤È¤â¤¢¤ë¡£
290 .\"O The program shown below demonstrates the use of
292 .\"O The first command-line argument specifies a string from which
294 .\"O should parse a number.
295 .\"O The second (optional) argument specifies the base to be used for
297 .\"O (This argument is converted to numeric form using
299 .\"O a function that performs no error checking and
300 .\"O has a simpler interface than
305 ºÇ½é¤Î¥³¥Þ¥ó¥É¥é¥¤¥ó°ú¤¿ô¤Ë¤Ï
307 ¤¬¿ô»ú¤È¤·¤Æ²ò¼á¤¹¤ëʸ»úÎó¤ò»ØÄꤹ¤ë¡£
308 (¾Êά²Äǽ¤Ê) ÆóÈÖÌܤΰú¤¿ô¤Ë¤Ï
309 ÊÑ´¹¤Ë»ÈÍѤµ¤ì¤ë´ð¿ô¤ò»ØÄꤹ¤ë
312 ¤ò»È¤Ã¤Æ¿ôÃͤËÊÑ´¹¤µ¤ì¤ë¡£
316 ¤è¤ê¤â´Êñ¤Ê¥¤¥ó¥¿¥Õ¥§¡¼¥¹¤ò»ý¤Ä´Ø¿ô¤Ç¡¢
317 ¤½¤ÎÃæ¤Ç¤Ï¥¨¥é¡¼¥Á¥§¥Ã¥¯¤Ï¹Ô¤ï¤ì¤Ê¤¤)¡£
318 .\"O Some examples of the results produced by this program are the following:
319 ¤³¤Î¥×¥í¥°¥é¥à¤Î¼Â¹ÔÎã¤ò¤¤¤¯¤Ä¤«°Ê²¼¤Ë¼¨¤¹:
323 .RB "$" " ./a.out 123"
324 strtol() returned 123
325 .RB "$" " ./a.out \(aq 123\(aq"
326 strtol() returned 123
327 .RB "$" " ./a.out 123abc"
328 strtol() returned 123
329 Further characters after number: abc
330 .RB "$" " ./a.out 123abc 55"
331 strtol: Invalid argument
332 .RB "$" " ./a.out \(aq\(aq"
334 .RB "$" " ./a.out 4000000000"
335 strtol: Numerical result out of range
338 .\"O .SS Program source
339 .SS ¥×¥í¥°¥é¥à¤Î¥½¡¼¥¹
348 main(int argc, char *argv[])
355 fprintf(stderr, "Usage: %s str [base]\\n", argv[0]);
360 base = (argc > 2) ? atoi(argv[2]) : 10;
362 errno = 0; /* To distinguish success/failure after call */
363 val = strtol(str, &endptr, base);
365 /* Check for various possible errors */
367 if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
368 || (errno != 0 && val == 0)) {
374 fprintf(stderr, "No digits were found\\n");
378 /* If we got here, strtol() successfully parsed a number */
380 printf("strtol() returned %ld\\n", val);
382 if (*endptr != \(aq\\0\(aq) /* Not necessarily an error... */
383 printf("Further characters after number: %s\\n", endptr);