OSDN Git Service

(split) Convert release and draft pages to UTF-8.
[linuxjm/LDP_man-pages.git] / release / man3 / strtol.3
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:53:39 1993 by Rik Faith (faith@cs.unc.edu)
28 .\" Added correction due to nsd@bbc.com (Nick Duffek) - aeb, 950610
29 .\"
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
38 .\"
39 .\" WORD:       base            基数
40 .\"
41 .TH STRTOL 3  2010-09-20 "GNU" "Linux Programmer's Manual"
42 .SH 名前
43 strtol, strtoll, strtoq \- 文字列を long int に変換する
44 .SH 書式
45 .nf
46 .B #include <stdlib.h>
47 .sp
48 .BI "long int strtol(const char *" nptr ", char **" endptr ", int " base );
49 .sp
50 .BI "long long int strtoll(const char *" nptr ", char **" endptr \
51 ", int " base );
52 .fi
53 .sp
54 .in -4n
55 glibc 向けの機能検査マクロの要件
56 .RB ( feature_test_macros (7)
57 参照):
58 .in
59 .sp
60 .ad l
61 .BR strtoll ():
62 .RS 4
63 XOPEN_SOURCE\ >=\ 600 || _BSD_SOURCE || _SVID_SOURCE || _ISOC99_SOURCE ||
64 _POSIX_C_SOURCE\ >=\ 200112L;
65 .br
66 or
67 .I cc\ -std=c99
68 .RE
69 .ad
70 .SH 説明
71 .BR strtol ()
72 関数は、 \fInptr\fP の文字列の最初の部分を、
73 \fIbase\fP を基数として long int に変換する。
74 この \fIbase\fP は 2 から 36 までの値
75 あるいは特別な意味を持つ値 0 でなければならない。
76 .PP
77 文字列の先頭には、任意の数の空白があってもよく (空白は
78 .BR isspace (3)
79 で判定される)、また数字の直前には \(aq+\(aq か \(aq\-\(aq の
80 符号があってもよい。
81 \fIbase\fP が 0 または 16 の場合には、文字列の先頭に "0x" を置くことが
82 でき、その場合には文字列は 16進数として扱われる。
83 これ以外の文字列で \fIbase\fP が 0 の場合は、
84 文字列が \(aq0\(aq で始まるときは 8進数として、
85 それ以外のときは 10進数として扱われる。
86 .PP
87 数字を表す文字列は
88 .I long int
89 に変換されるが、基数に対して
90 有効でない数字が現れた時点で変換は終了する。(11進数以上では \(aqA\(aq は
91 大文字・小文字に関わらず 10 を表し、 \(aqB\(aq は 11 を表現し、
92 以下同様に、 \(aqZ\(aq は 35 を表す。)
93 .PP
94 \fIendptr\fP がヌル値 (NULL) でない場合は、最初に現れた不正な文字が
95 .BR strtol ()
96 によって \fI*endptr\fP に保存されている。
97 文字列に有効な数字がひとつもなければ、
98 .BR strtol ()
99 は \fInptr\fP
100 の元の値を \fI*endptr\fP に代入する (そして 0 を返す)。
101 特に、\fI*nptr\fP が \(aq\\0\(aq 以外で、返された \fI**endptr\fP が
102 \(aq\\0\(aq ならば、文字列全体が有効だったことになる。
103 .PP
104 .BR strtoll ()
105 関数は
106 .BR strtol ()
107 と同様だが、long long int 型の値を返す。
108 .SH 返り値
109 アンダーフローもオーバーフローも起きなかった場合、
110 .BR strtol ()
111 関数は
112 変換された値を返す。オーバーフローした場合には
113 .B LONG_MAX
114 が返り、
115 アンダーフローした場合には
116 .B LONG_MIN
117 が返る。オーバーフロー、
118 アンダーフローのいずれの場合にも
119 大域変数 \fIerrno\fP には
120 .B ERANGE
121 が設定される。
122 .BR strtoll ()
123 も同様であるが、
124 .B LONG_MIN
125
126 .B LONG_MAX
127 の代わりに
128 .B LLONG_MIN
129
130 .B LLONG_MAX
131 が返される。
132 .SH エラー
133 .TP
134 .B EINVAL
135 (C99 にはない)
136 .I base
137 が対応していない値である。
138 .TP
139 .B ERANGE
140 結果の値が範囲外である。
141 .LP
142 実装によっては、変換が行われなかった場合 (数字がなく、0 を返した場合)、
143 \fIerrno\fP に \fBEINVAL\fP が設定される場合がある。
144 .SH 準拠
145 .BR strtol ()
146 は SVr4, 4.3BSD, C89, C99 と POSIX.1-2001 に準拠している。
147 .BR strtoll ()
148 は C99 と POSIX.1-2001 に準拠している。
149 .SH 注意
150 .B strtol ()
151 からは成功、失敗どちらの場合でも
152 0,
153 .BR LONG_MAX ,
154 .B LONG_MIN
155 .RB ( strtoll ()
156 では
157 .BR LLONG_MAX ,
158 .BR LLONG_MIN )
159 が返る可能性があるので、
160 プログラムは関数を呼び出す前に
161 .I errno
162 を 0 に設定し、呼び出し後に
163 .I errno
164 が 0 以外の値かどうかを確認しエラーが発生したかどうかを判断する
165 必要がある。
166
167 "C" 以外のロケールの場合、その他の文字列も受け付けられるかもしれない。
168 (例えば、現在のロケールの 1000 毎の区切り文字がサポートされているかもしれない。)
169 .LP
170 BSD には、
171 .sp
172 .in +4n
173 .nf
174 .BI "quad_t strtoq(const char *" nptr ", char **" endptr ", int " base );
175 .sp
176 .in
177 .fi
178 という完全に同様の定義を持つ関数がある。
179 使用中のアーキテクチャのワード長次第であるが、この関数は
180 .BR strtoll ()
181
182 .BR strtol ()
183 と等価となることもある。
184 .SH 例
185 以下のプログラムは
186 .BR strtol ()
187 の使用例である。
188 最初のコマンドライン引き数には
189 .BR strtol ()
190 が数字として解釈する文字列を指定する。
191 (省略可能な) 二番目の引き数には
192 変換に使用される基数を指定する
193 (この引き数は
194 .BR atoi (3)
195 を使って数値に変換される。
196 .BR atoi (3)
197
198 .BR strtol ()
199 よりも簡単なインタフェースを持つ関数で、
200 その中ではエラーチェックは行われない)。
201 このプログラムの実行例をいくつか以下に示す:
202 .in +4n
203 .nf
204
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"
215 No digits were found
216 .RB "$" " ./a.out 4000000000"
217 strtol: Numerical result out of range
218 .fi
219 .in
220 .SS プログラムのソース
221 \&
222 .nf
223 #include <stdlib.h>
224 #include <limits.h>
225 #include <stdio.h>
226 #include <errno.h>
227
228 int
229 main(int argc, char *argv[])
230 {
231     int base;
232     char *endptr, *str;
233     long val;
234
235     if (argc < 2) {
236         fprintf(stderr, "Usage: %s str [base]\\n", argv[0]);
237         exit(EXIT_FAILURE);
238     }
239
240     str = argv[1];
241     base = (argc > 2) ? atoi(argv[2]) : 10;
242
243     errno = 0;    /* To distinguish success/failure after call */
244     val = strtol(str, &endptr, base);
245
246     /* Check for various possible errors */
247
248     if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
249             || (errno != 0 && val == 0)) {
250         perror("strtol");
251         exit(EXIT_FAILURE);
252     }
253
254     if (endptr == str) {
255         fprintf(stderr, "No digits were found\\n");
256         exit(EXIT_FAILURE);
257     }
258
259     /* If we got here, strtol() successfully parsed a number */
260
261     printf("strtol() returned %ld\\n", val);
262
263     if (*endptr != \(aq\\0\(aq)        /* Not necessarily an error... */
264         printf("Further characters after number: %s\\n", endptr);
265
266     exit(EXIT_SUCCESS);
267 }
268 .fi
269 .SH 関連項目
270 .BR atof (3),
271 .BR atoi (3),
272 .BR atol (3),
273 .BR strtod (3),
274 .BR strtoul (3)