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