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 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 \
56 .RB ( feature_test_macros (7)
63 XOPEN_SOURCE\ >=\ 600 || _BSD_SOURCE || _SVID_SOURCE || _ISOC99_SOURCE ||
64 _POSIX_C_SOURCE\ >=\ 200112L;
72 関数は、 \fInptr\fP の文字列の最初の部分を、
73 \fIbase\fP を基数として long int に変換する。
74 この \fIbase\fP は 2 から 36 までの値
75 あるいは特別な意味を持つ値 0 でなければならない。
77 文字列の先頭には、任意の数の空白があってもよく (空白は
79 で判定される)、また数字の直前には \(aq+\(aq か \(aq\-\(aq の
81 \fIbase\fP が 0 または 16 の場合には、文字列の先頭に "0x" を置くことが
82 でき、その場合には文字列は 16進数として扱われる。
83 これ以外の文字列で \fIbase\fP が 0 の場合は、
84 文字列が \(aq0\(aq で始まるときは 8進数として、
90 有効でない数字が現れた時点で変換は終了する。(11進数以上では \(aqA\(aq は
91 大文字・小文字に関わらず 10 を表し、 \(aqB\(aq は 11 を表現し、
92 以下同様に、 \(aqZ\(aq は 35 を表す。)
94 \fIendptr\fP がヌル値 (NULL) でない場合は、最初に現れた不正な文字が
96 によって \fI*endptr\fP に保存されている。
100 の元の値を \fI*endptr\fP に代入する (そして 0 を返す)。
101 特に、\fI*nptr\fP が \(aq\\0\(aq 以外で、返された \fI**endptr\fP が
102 \(aq\\0\(aq ならば、文字列全体が有効だったことになる。
107 と同様だが、long long int 型の値を返す。
109 アンダーフローもオーバーフローも起きなかった場合、
112 変換された値を返す。オーバーフローした場合には
142 実装によっては、変換が行われなかった場合 (数字がなく、0 を返した場合)、
143 \fIerrno\fP に \fBEINVAL\fP が設定される場合がある。
146 は SVr4, 4.3BSD, C89, C99 と POSIX.1-2001 に準拠している。
148 は C99 と POSIX.1-2001 に準拠している。
164 が 0 以外の値かどうかを確認しエラーが発生したかどうかを判断する
167 "C" 以外のロケールの場合、その他の文字列も受け付けられるかもしれない。
168 (例えば、現在のロケールの 1000 毎の区切り文字がサポートされているかもしれない。)
174 .BI "quad_t strtoq(const char *" nptr ", char **" endptr ", int " base );
179 使用中のアーキテクチャのワード長次第であるが、この関数は
201 このプログラムの実行例をいくつか以下に示す:
205 .RB "$" " ./a.out 123"
206 strtol() returned 123
207 .RB "$" " ./a.out \(aq 123\(aq"
208 strtol() returned 123
209 .RB "$" " ./a.out 123abc"
210 strtol() returned 123
211 Further characters after number: abc
212 .RB "$" " ./a.out 123abc 55"
213 strtol: Invalid argument
214 .RB "$" " ./a.out \(aq\(aq"
216 .RB "$" " ./a.out 4000000000"
217 strtol: Numerical result out of range
229 main(int argc, char *argv[])
236 fprintf(stderr, "Usage: %s str [base]\\n", argv[0]);
241 base = (argc > 2) ? atoi(argv[2]) : 10;
243 errno = 0; /* To distinguish success/failure after call */
244 val = strtol(str, &endptr, base);
246 /* Check for various possible errors */
248 if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
249 || (errno != 0 && val == 0)) {
255 fprintf(stderr, "No digits were found\\n");
259 /* If we got here, strtol() successfully parsed a number */
261 printf("strtol() returned %ld\\n", val);
263 if (*endptr != \(aq\\0\(aq) /* Not necessarily an error... */
264 printf("Further characters after number: %s\\n", endptr);