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 2007-07-26 "GNU" "Linux Programmer's Manual"
43 strtol, strtoll, strtoq \- ʸ»úÎó¤ò long int ¤ËÊÑ´¹¤¹¤ë
46 .B #include <stdlib.h>
48 .BI "long int strtol(const char *" nptr ", char **" endptr ", int " base );
50 .BI "long long int strtoll(const char *" nptr ", char **" endptr \
55 glibc ¸þ¤±¤Îµ¡Ç½¸¡ºº¥Þ¥¯¥í¤ÎÍ×·ï
56 .RB ( feature_test_macros (7)
62 XOPEN_SOURCE >= 600 || _BSD_SOURCE || _SVID_SOURCE || _ISOC99_SOURCE; or
67 ´Ø¿ô¤Ï¡¢ \fInptr\fP ¤Îʸ»úÎó¤ÎºÇ½é¤ÎÉôʬ¤ò¡¢
68 \fIbase\fP ¤ò´ð¿ô¤È¤·¤Æ long int ¤ËÊÑ´¹¤¹¤ë¡£
69 ¤³¤Î \fIbase\fP ¤Ï 2 ¤«¤é 36 ¤Þ¤Ç¤ÎÃÍ
70 ¤¢¤ë¤¤¤ÏÆÃÊ̤ʰÕÌ£¤ò»ý¤ÄÃÍ 0 ¤Ç¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
72 ʸ»úÎó¤ÎÀèƬ¤Ë¤Ï¡¢Ç¤°Õ¤Î¿ô¤Î¶õÇò¤¬¤¢¤Ã¤Æ¤â¤è¤¯ (¶õÇò¤Ï
74 ¤ÇȽÄꤵ¤ì¤ë)¡¢¤Þ¤¿¿ô»ú¤ÎľÁ°¤Ë¤Ï \(aq+\(aq ¤« \(aq\-\(aq ¤Î
76 \fIbase\fP ¤¬ 0 ¤Þ¤¿¤Ï 16 ¤Î¾ì¹ç¤Ë¤Ï¡¢Ê¸»úÎó¤ÎÀèƬ¤Ë "0x" ¤òÃÖ¤¯¤³¤È¤¬
77 ¤Ç¤¡¢¤½¤Î¾ì¹ç¤Ë¤Ïʸ»úÎó¤Ï 16¿Ê¿ô¤È¤·¤Æ°·¤ï¤ì¤ë¡£
78 ¤³¤ì°Ê³°¤Îʸ»úÎó¤Ç \fIbase\fP ¤¬ 0 ¤Î¾ì¹ç¤Ï¡¢
79 ʸ»úÎó¤¬ \(aq0\(aq ¤Ç»Ï¤Þ¤ë¤È¤¤Ï 8¿Ê¿ô¤È¤·¤Æ¡¢
80 ¤½¤ì°Ê³°¤Î¤È¤¤Ï 10¿Ê¿ô¤È¤·¤Æ°·¤ï¤ì¤ë¡£
84 ¤ËÊÑ´¹¤µ¤ì¤ë¤¬¡¢´ð¿ô¤ËÂФ·¤Æ
85 ͸ú¤Ç¤Ê¤¤¿ô»ú¤¬¸½¤ì¤¿»þÅÀ¤ÇÊÑ´¹¤Ï½ªÎ»¤¹¤ë¡£(11¿Ê¿ô°Ê¾å¤Ç¤Ï \(aqA\(aq ¤Ï
86 Âçʸ»ú¡¦¾®Ê¸»ú¤Ë´Ø¤ï¤é¤º 10 ¤òɽ¤·¡¢ \(aqB\(aq ¤Ï 11 ¤òɽ¸½¤·¡¢
87 °Ê²¼Æ±Íͤˡ¢ \(aqZ\(aq ¤Ï 35 ¤òɽ¤¹¡£)
89 \fIendptr\fP ¤¬¥Ì¥ëÃÍ (NULL) ¤Ç¤Ê¤¤¾ì¹ç¤Ï¡¢ºÇ½é¤Ë¸½¤ì¤¿ÉÔÀµ¤Êʸ»ú¤¬
91 ¤Ë¤è¤Ã¤Æ \fI*endptr\fP ¤ËÊݸ¤µ¤ì¤Æ¤¤¤ë¡£
92 ʸ»úÎó¤Ë͸ú¤Ê¿ô»ú¤¬¤Ò¤È¤Ä¤â¤Ê¤±¤ì¤Ð¡¢
95 ¤Î¸µ¤ÎÃͤò \fI*endptr\fP ¤ËÂåÆþ¤¹¤ë (¤½¤·¤Æ 0 ¤òÊÖ¤¹)¡£
96 Æäˡ¢\fI*nptr\fP ¤¬ \(aq\\0\(aq °Ê³°¤Ç¡¢ÊÖ¤µ¤ì¤¿ \fI**endptr\fP ¤¬
97 \(aq\\0\(aq ¤Ê¤é¤Ð¡¢Ê¸»úÎóÁ´ÂΤ¬Í¸ú¤À¤Ã¤¿¤³¤È¤Ë¤Ê¤ë¡£
102 ¤ÈƱÍͤÀ¤¬¡¢long long int ·¿¤ÎÃͤòÊÖ¤¹¡£
104 ¥¢¥ó¥À¡¼¥Õ¥í¡¼¤â¥ª¡¼¥Ð¡¼¥Õ¥í¡¼¤âµ¯¤¤Ê¤«¤Ã¤¿¾ì¹ç¡¢
107 ÊÑ´¹¤µ¤ì¤¿ÃͤòÊÖ¤¹¡£¥ª¡¼¥Ð¡¼¥Õ¥í¡¼¤·¤¿¾ì¹ç¤Ë¤Ï
110 ¥¢¥ó¥À¡¼¥Õ¥í¡¼¤·¤¿¾ì¹ç¤Ë¤Ï
112 ¤¬Ê֤롣¥ª¡¼¥Ð¡¼¥Õ¥í¡¼¡¢
113 ¥¢¥ó¥À¡¼¥Õ¥í¡¼¤Î¤¤¤º¤ì¤Î¾ì¹ç¤Ë¤â
114 Âç°èÊÑ¿ô \fIerrno\fP ¤Ë¤Ï
132 ¤¬Âбþ¤·¤Æ¤¤¤Ê¤¤ÃͤǤ¢¤ë¡£
135 ·ë²Ì¤ÎÃͤ¬Èϰϳ°¤Ç¤¢¤ë¡£
137 ¼ÂÁõ¤Ë¤è¤Ã¤Æ¤Ï¡¢ÊÑ´¹¤¬¹Ô¤ï¤ì¤Ê¤«¤Ã¤¿¾ì¹ç (¿ô»ú¤¬¤Ê¤¯¡¢0 ¤òÊÖ¤·¤¿¾ì¹ç)¡¢
138 \fIerrno\fP ¤Ë \fBEINVAL\fP ¤¬ÀßÄꤵ¤ì¤ë¾ì¹ç¤¬¤¢¤ë¡£
141 ¤Ï SVr4, 4.3BSD, C89, C99 ¤È POSIX.1-2001 ¤Ë½àµò¤·¤Æ¤¤¤ë¡£
143 ¤Ï C99 ¤È POSIX.1-2001 ¤Ë½àµò¤·¤Æ¤¤¤ë¡£
146 ¤«¤é¤ÏÀ®¸ù¡¢¼ºÇԤɤÁ¤é¤Î¾ì¹ç¤Ç¤â
154 ¤¬ÊÖ¤ë²ÄǽÀ¤¬¤¢¤ë¤Î¤Ç¡¢
155 ¥×¥í¥°¥é¥à¤Ï´Ø¿ô¤ò¸Æ¤Ó½Ð¤¹Á°¤Ë
157 ¤ò 0 ¤ËÀßÄꤷ¡¢¸Æ¤Ó½Ð¤·¸å¤Ë
159 ¤¬ 0 °Ê³°¤ÎÃͤ«¤É¤¦¤«¤ò³Îǧ¤·¥¨¥é¡¼¤¬È¯À¸¤·¤¿¤«¤É¤¦¤«¤òȽÃǤ¹¤ë
162 "C" °Ê³°¤Î¥í¥±¡¼¥ë¤Î¾ì¹ç¡¢¤½¤Î¾¤Îʸ»úÎó¤â¼õ¤±ÉÕ¤±¤é¤ì¤ë¤«¤â¤·¤ì¤Ê¤¤¡£
163 (Î㤨¤Ð¡¢¸½ºß¤Î¥í¥±¡¼¥ë¤Î 1000 Ëè¤Î¶èÀÚ¤êʸ»ú¤¬¥µ¥Ý¡¼¥È¤µ¤ì¤Æ¤¤¤ë¤«¤â¤·¤ì¤Ê¤¤¡£)
169 .BI "quad_t strtoq(const char *" nptr ", char **" endptr ", int " base );
173 ¤È¤¤¤¦´°Á´¤ËƱÍͤÎÄêµÁ¤ò»ý¤Ä´Ø¿ô¤¬¤¢¤ë¡£
174 »ÈÍÑÃæ¤Î¥¢¡¼¥¥Æ¥¯¥Á¥ã¤Î¥ï¡¼¥ÉĹ¼¡Âè¤Ç¤¢¤ë¤¬¡¢¤³¤Î´Ø¿ô¤Ï
178 ¤ÈÅù²Á¤È¤Ê¤ë¤³¤È¤â¤¢¤ë¡£
183 ºÇ½é¤Î¥³¥Þ¥ó¥É¥é¥¤¥ó°ú¤¿ô¤Ë¤Ï
185 ¤¬¿ô»ú¤È¤·¤Æ²ò¼á¤¹¤ëʸ»úÎó¤ò»ØÄꤹ¤ë¡£
186 (¾Êά²Äǽ¤Ê) ÆóÈÖÌܤΰú¤¿ô¤Ë¤Ï
187 ÊÑ´¹¤Ë»ÈÍѤµ¤ì¤ë´ð¿ô¤ò»ØÄꤹ¤ë
190 ¤ò»È¤Ã¤Æ¿ôÃͤËÊÑ´¹¤µ¤ì¤ë¡£
194 ¤è¤ê¤â´Êñ¤Ê¥¤¥ó¥¿¥Õ¥§¡¼¥¹¤ò»ý¤Ä´Ø¿ô¤Ç¡¢
195 ¤½¤ÎÃæ¤Ç¤Ï¥¨¥é¡¼¥Á¥§¥Ã¥¯¤Ï¹Ô¤ï¤ì¤Ê¤¤)¡£
196 ¤³¤Î¥×¥í¥°¥é¥à¤Î¼Â¹ÔÎã¤ò¤¤¤¯¤Ä¤«°Ê²¼¤Ë¼¨¤¹:
200 .RB "$" " ./a.out 123"
201 strtol() returned 123
202 .RB "$" " ./a.out \(aq 123\(aq"
203 strtol() returned 123
204 .RB "$" " ./a.out 123abc"
205 strtol() returned 123
206 Further characters after number: abc
207 .RB "$" " ./a.out 123abc 55"
208 strtol: Invalid argument
209 .RB "$" " ./a.out \(aq\(aq"
211 .RB "$" " ./a.out 4000000000"
212 strtol: Numerical result out of range
215 .SS ¥×¥í¥°¥é¥à¤Î¥½¡¼¥¹
224 main(int argc, char *argv[])
231 fprintf(stderr, "Usage: %s str [base]\\n", argv[0]);
236 base = (argc > 2) ? atoi(argv[2]) : 10;
238 errno = 0; /* To distinguish success/failure after call */
239 val = strtol(str, &endptr, base);
241 /* Check for various possible errors */
243 if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
244 || (errno != 0 && val == 0)) {
250 fprintf(stderr, "No digits were found\\n");
254 /* If we got here, strtol() successfully parsed a number */
256 printf("strtol() returned %ld\\n", val);
258 if (*endptr != \(aq\\0\(aq) /* Not necessarily an error... */
259 printf("Further characters after number: %s\\n", endptr);