OSDN Git Service

Update release for LDP 3.67
[linuxjm/LDP_man-pages.git] / release / man3 / strtol.3
1 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
2 .\"
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
7 .\"
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
12 .\"
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein.  The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" %%%LICENSE_END
24 .\"
25 .\" References consulted:
26 .\"     Linux libc source code
27 .\"     Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
28 .\"     386BSD man pages
29 .\" Modified Sun Jul 25 10:53:39 1993 by Rik Faith (faith@cs.unc.edu)
30 .\" Added correction due to nsd@bbc.com (Nick Duffek) - aeb, 950610
31 .\"*******************************************************************
32 .\"
33 .\" This file was generated with po4a. Translate the source file.
34 .\"
35 .\"*******************************************************************
36 .\"
37 .\" Japanese Version Copyright (c) 1999 AKAMATSU, Kazuo
38 .\"         all rights reserved.
39 .\" Translated Thu Jan 9 21:00:00 JST 1999
40 .\"         by AKAMATSU, Kazuo
41 .\" Updated Sun Apr  8 JST 2001 by Kentaro Shirakata <argrath@ub32.org>
42 .\" Updated Sat Mar 23 JST 2002 by Kentaro Shirakata <argrath@ub32.org>
43 .\" Updated & Modified 2006-07-20,
44 .\"         Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v2.36
45 .\" Updated 2012-05-04, Akihiro MOTOKI <amotoki@gmail.com>
46 .\"
47 .TH STRTOL 3 2014\-03\-18 GNU "Linux Programmer's Manual"
48 .SH 名前
49 strtol, strtoll, strtoq \- 文字列を long int に変換する
50 .SH 書式
51 .nf
52 \fB#include <stdlib.h>\fP
53 .sp
54 \fBlong int strtol(const char *\fP\fInptr\fP\fB, char **\fP\fIendptr\fP\fB, int \fP\fIbase\fP\fB);\fP
55 .sp
56 \fBlong long int strtoll(const char *\fP\fInptr\fP\fB, char **\fP\fIendptr\fP\fB, int \fP\fIbase\fP\fB);\fP
57 .fi
58 .sp
59 .in -4n
60 glibc 向けの機能検査マクロの要件 (\fBfeature_test_macros\fP(7)  参照):
61 .in
62 .sp
63 .ad l
64 \fBstrtoll\fP():
65 .RS 4
66 XOPEN_SOURCE\ >=\ 600 || _BSD_SOURCE || _SVID_SOURCE || _ISOC99_SOURCE
67 || _POSIX_C_SOURCE\ >=\ 200112L;
68 .br
69 または \fIcc\ \-std=c99\fP
70 .RE
71 .ad
72 .SH 説明
73 \fBstrtol\fP()  関数は、 \fInptr\fP の文字列の最初の部分を、 \fIbase\fP を基数として long int に変換する。 この
74 \fIbase\fP は 2 から 36 までの値 あるいは特別な意味を持つ値 0 でなければならない。
75 .PP
76 文字列の先頭には、任意の数の空白があってもよく (空白は \fBisspace\fP(3) で判定される)、また数字の直前には \(aq+\(aq か
77 \(aq\-\(aq の 符号があってもよい。 \fIbase\fP が 0 または 16 の場合には、文字列の先頭に "0x" を置くことが
78 でき、その場合には文字列は 16進数として扱われる。 これ以外の文字列で \fIbase\fP が 0 の場合は、 文字列が \(aq0\(aq
79 で始まるときは 8進数として、 それ以外のときは 10進数として扱われる。
80 .PP
81 数字を表す文字列は \fIlong int\fP に変換されるが、基数に対して 有効でない数字が現れた時点で変換は終了する。(11進数以上では
82 \(aqA\(aq は 大文字・小文字に関わらず 10 を表し、 \(aqB\(aq は 11 を表現し、 以下同様に、 \(aqZ\(aq は 35
83 を表す。)
84 .PP
85 \fIendptr\fP がヌル値 (NULL) でない場合は、最初に現れた不正な文字が \fBstrtol\fP()  によって \fI*endptr\fP
86 に保存されている。 文字列に有効な数字がひとつもなければ、 \fBstrtol\fP()  は \fInptr\fP の元の値を \fI*endptr\fP に代入する
87 (そして 0 を返す)。 特に、\fI*nptr\fP が \(aq\e0\(aq 以外で、返された \fI**endptr\fP が \(aq\e0\(aq
88 ならば、文字列全体が有効だったことになる。
89 .PP
90 \fBstrtoll\fP()  関数は \fBstrtol\fP()  と同様だが、long long int 型の値を返す。
91 .SH 返り値
92 アンダーフローもオーバーフローも起きなかった場合、 \fBstrtol\fP()  関数は 変換された値を返す。オーバーフローした場合には
93 \fBLONG_MAX\fP が返り、 アンダーフローした場合には \fBLONG_MIN\fP が返る。オーバーフロー、 アンダーフローのいずれの場合にも
94 大域変数 \fIerrno\fP には \fBERANGE\fP が設定される。 \fBstrtoll\fP()  も同様であるが、 \fBLONG_MIN\fP と
95 \fBLONG_MAX\fP の代わりに \fBLLONG_MIN\fP と \fBLLONG_MAX\fP が返される。
96 .SH エラー
97 .TP 
98 \fBEINVAL\fP
99 (C99 にはない) 指定された \fIbase\fP がサポートされていない値である。
100 .TP 
101 \fBERANGE\fP
102 結果の値が範囲外である。
103 .LP
104 実装によっては、変換が行われなかった場合 (数字がなく、0 を返した場合)、 \fIerrno\fP に \fBEINVAL\fP が設定される場合がある。
105 .SH 属性
106 .SS "マルチスレッディング (pthreads(7) 参照)"
107 関数 \fBstrtol\fP(), \fBstrtoll\fP(), \fBstrtoq\fP() は、例外付きのスレッドセーフである。実行中に
108 \fBsetlocale\fP(3) を呼び出してロケールを変更しない限り、マルチスレッドアプリケーションで安全に使用することができる。
109 .SH 準拠
110 \fBstrtol\fP()  は SVr4, 4.3BSD, C89, C99 と POSIX.1\-2001 に準拠している。 \fBstrtoll\fP()
111 は C99 と POSIX.1\-2001 に準拠している。
112 .SH 注意
113 \fBstrtol ()\fP からは成功、失敗どちらの場合でも 0, \fBLONG_MAX\fP, \fBLONG_MIN\fP (\fBstrtoll\fP()  では
114 \fBLLONG_MAX\fP, \fBLLONG_MIN\fP)  が返る可能性があるので、 プログラムは関数を呼び出す前に \fIerrno\fP を 0
115 に設定し、呼び出し後に \fIerrno\fP が 0 以外の値かどうかを確認しエラーが発生したかどうかを判断する 必要がある。
116
117 POSIX.1\-2001 では、 "C" と "POSIX"
118 以外のロケールでは、これらの関数は、他の実装時に定義される数字を示す文字列を受け付けるとされている。
119 .LP
120 BSD には、
121 .sp
122 .in +4n
123 .nf
124 \fBquad_t strtoq(const char *\fP\fInptr\fP\fB, char **\fP\fIendptr\fP\fB, int \fP\fIbase\fP\fB);\fP
125 .sp
126 .in
127 .fi
128 という完全に同様の定義を持つ関数がある。 使用中のアーキテクチャのワード長次第であるが、この関数は \fBstrtoll\fP()  か
129 \fBstrtol\fP()  と等価となることもある。
130 .SH 例
131 以下のプログラムは \fBstrtol\fP()  の使用例である。 最初のコマンドライン引き数には \fBstrtol\fP()
132 が数字として解釈する文字列を指定する。 (省略可能な) 二番目の引き数には 変換に使用される基数を指定する (この引き数は \fBatoi\fP(3)
133 を使って数値に変換される。 \fBatoi\fP(3)  は \fBstrtol\fP()  よりも簡単なインタフェースを持つ関数で、
134 その中ではエラーチェックは行われない)。 このプログラムの実行例をいくつか以下に示す:
135 .in +4n
136 .nf
137
138 $\fB ./a.out 123\fP
139 strtol() returned 123
140 $\fB ./a.out \(aq    123\(aq\fP
141 strtol() returned 123
142 $\fB ./a.out 123abc\fP
143 strtol() returned 123
144 Further characters after number: abc
145 $\fB ./a.out 123abc 55\fP
146 strtol: Invalid argument
147 $\fB ./a.out \(aq\(aq\fP
148 No digits were found
149 $\fB ./a.out 4000000000\fP
150 strtol: Numerical result out of range
151 .fi
152 .in
153 .SS プログラムのソース
154 \&
155 .nf
156 #include <stdlib.h>
157 #include <limits.h>
158 #include <stdio.h>
159 #include <errno.h>
160
161 int
162 main(int argc, char *argv[])
163 {
164     int base;
165     char *endptr, *str;
166     long val;
167
168     if (argc < 2) {
169         fprintf(stderr, "Usage: %s str [base]\en", argv[0]);
170         exit(EXIT_FAILURE);
171     }
172
173     str = argv[1];
174     base = (argc > 2) ? atoi(argv[2]) : 10;
175
176     errno = 0;    /* To distinguish success/failure after call */
177     val = strtol(str, &endptr, base);
178
179     /* Check for various possible errors */
180
181     if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
182             || (errno != 0 && val == 0)) {
183         perror("strtol");
184         exit(EXIT_FAILURE);
185     }
186
187     if (endptr == str) {
188         fprintf(stderr, "No digits were found\en");
189         exit(EXIT_FAILURE);
190     }
191
192     /* If we got here, strtol() successfully parsed a number */
193
194     printf("strtol() returned %ld\en", val);
195
196     if (*endptr != \(aq\e0\(aq)        /* Not necessarily an error... */
197         printf("Further characters after number: %s\en", endptr);
198
199     exit(EXIT_SUCCESS);
200 }
201 .fi
202 .SH 関連項目
203 \fBatof\fP(3), \fBatoi\fP(3), \fBatol\fP(3), \fBstrtod\fP(3), \fBstrtoul\fP(3)
204 .SH この文書について
205 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.67 の一部
206 である。プロジェクトの説明とバグ報告に関する情報は
207 http://www.kernel.org/doc/man\-pages/ に書かれている。