.\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk) .\" .\" 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. .\" .\" 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 .\" .\" 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 .\" Updated Sat Mar 23 JST 2002 by Kentaro Shirakata .\" Updated & Modified 2006-07-20, .\" Akihiro MOTOKI , LDP v2.36 .\" .\" WORD: base 基数 .\" .TH STRTOL 3 2010-09-20 "GNU" "Linux Programmer's Manual" .SH 名前 .\"O strtol, strtoll, strtoq \- convert a string to a long integer strtol, strtoll, strtoq \- 文字列を long int に変換する .SH 書式 .nf .B #include .sp .BI "long int strtol(const char *" nptr ", char **" endptr ", int " base ); .sp .BI "long long int strtoll(const char *" nptr ", char **" endptr \ ", int " base ); .fi .sp .in -4n .\"O Feature Test Macro Requirements for glibc (see .\"O .BR feature_test_macros (7)): glibc 向けの機能検査マクロの要件 .RB ( feature_test_macros (7) 参照): .in .sp .ad l .BR strtoll (): .RS 4 XOPEN_SOURCE\ >=\ 600 || _BSD_SOURCE || _SVID_SOURCE || _ISOC99_SOURCE || _POSIX_C_SOURCE\ >=\ 200112L; .br or .I cc\ -std=c99 .RE .ad .\"O .SH DESCRIPTION .SH 説明 .\"O The .\"O .BR strtol () .\"O function converts the initial part of the string .\"O in \fInptr\fP to a long integer value according to the given \fIbase\fP, .\"O which must be between 2 and 36 inclusive, or be the special value 0. .BR strtol () 関数は、 \fInptr\fP の文字列の最初の部分を、 \fIbase\fP を基数として long int に変換する。 この \fIbase\fP は 2 から 36 までの値 あるいは特別な意味を持つ値 0 でなければならない。 .PP .\"O The string may begin with an arbitrary amount of white space (as .\"O determined by .\"O .BR isspace (3)) .\"O followed by a single optional \(aq+\(aq or \(aq\-\(aq sign. .\"O If \fIbase\fP is zero or 16, the string may then include a .\"O "0x" prefix, and the number will be read in base 16; otherwise, a .\"O zero \fIbase\fP is taken as 10 (decimal) unless the next character .\"O is \(aq0\(aq, in which case it is taken as 8 (octal). 文字列の先頭には、任意の数の空白があってもよく (空白は .BR isspace (3) で判定される)、また数字の直前には \(aq+\(aq か \(aq\-\(aq の 符号があってもよい。 \fIbase\fP が 0 または 16 の場合には、文字列の先頭に "0x" を置くことが でき、その場合には文字列は 16進数として扱われる。 これ以外の文字列で \fIbase\fP が 0 の場合は、 文字列が \(aq0\(aq で始まるときは 8進数として、 それ以外のときは 10進数として扱われる。 .PP .\"O The remainder of the string is converted to a .\"O .I long int .\"O value .\"O in the obvious manner, stopping at the first character which is not a .\"O valid digit in the given base. .\"O (In bases above 10, the letter \(aqA\(aq in .\"O either upper or lower case represents 10, \(aqB\(aq represents 11, and so .\"O forth, with \(aqZ\(aq representing 35.) 数字を表す文字列は .I long int に変換されるが、基数に対して 有効でない数字が現れた時点で変換は終了する。(11進数以上では \(aqA\(aq は 大文字・小文字に関わらず 10 を表し、 \(aqB\(aq は 11 を表現し、 以下同様に、 \(aqZ\(aq は 35 を表す。) .PP .\"O If \fIendptr\fP is not NULL, .\"O .BR strtol () .\"O stores the address of the .\"O first invalid character in \fI*endptr\fP. .\"O If there were no digits at .\"O all, .\"O .BR strtol () .\"O stores the original value of \fInptr\fP in .\"O \fI*endptr\fP (and returns 0). \fIendptr\fP がヌル値 (NULL) でない場合は、最初に現れた不正な文字が .BR strtol () によって \fI*endptr\fP に保存されている。 文字列に有効な数字がひとつもなければ、 .BR strtol () は \fInptr\fP の元の値を \fI*endptr\fP に代入する (そして 0 を返す)。 .\"O In particular, if \fI*nptr\fP is not \(aq\\0\(aq but \fI**endptr\fP .\"O is \(aq\\0\(aq on return, the entire string is valid. 特に、\fI*nptr\fP が \(aq\\0\(aq 以外で、返された \fI**endptr\fP が \(aq\\0\(aq ならば、文字列全体が有効だったことになる。 .PP .\"O The .\"O .BR strtoll () .\"O function works just like the .\"O .BR strtol () .\"O function but returns a long long integer value. .BR strtoll () 関数は .BR strtol () と同様だが、long long int 型の値を返す。 .SH 返り値 .\"O The .\"O .BR strtol () .\"O function returns the result of the conversion, .\"O unless the value would underflow or overflow. .\"O If an underflow occurs, .\"O .BR strtol () .\"O returns .\"O .BR LONG_MIN . .\"O If an overflow occurs, .\"O .BR strtol () .\"O returns .\"O .BR LONG_MAX . .\"O In both cases, \fIerrno\fP is set to .\"O .BR ERANGE . アンダーフローもオーバーフローも起きなかった場合、 .BR strtol () 関数は 変換された値を返す。オーバーフローした場合には .B LONG_MAX が返り、 アンダーフローした場合には .B LONG_MIN が返る。オーバーフロー、 アンダーフローのいずれの場合にも 大域変数 \fIerrno\fP には .B ERANGE が設定される。 .\"O Precisely the same holds for .\"O .BR strtoll () .\"O (with .\"O .B LLONG_MIN .\"O and .\"O .B LLONG_MAX .\"O instead of .\"O .B LONG_MIN .\"O and .\"O .BR LONG_MAX ). .BR strtoll () も同様であるが、 .B LONG_MIN と .B LONG_MAX の代わりに .B LLONG_MIN と .B LLONG_MAX が返される。 .SH エラー .TP .B EINVAL .\"O (not in C99) .\"O The given .\"O .I base .\"O contains an unsupported value. (C99 にはない) .I base が対応していない値である。 .TP .B ERANGE .\"O The resulting value was out of range. 結果の値が範囲外である。 .LP .\"O The implementation may also set \fIerrno\fP to \fBEINVAL\fP in case .\"O no conversion was performed (no digits seen, and 0 returned). 実装によっては、変換が行われなかった場合 (数字がなく、0 を返した場合)、 \fIerrno\fP に \fBEINVAL\fP が設定される場合がある。 .SH 準拠 .\"O .BR strtol () .\"O conforms to SVr4, 4.3BSD, C89, C99 and POSIX.1-2001, and .\"O .BR strtoll () .\"O to C99 and POSIX.1-2001. .BR strtol () は SVr4, 4.3BSD, C89, C99 と POSIX.1-2001 に準拠している。 .BR strtoll () は C99 と POSIX.1-2001 に準拠している。 .\"O .SH NOTES .SH 注意 .\"O Since .\"O .BR strtol () .\"O can legitimately return 0, .\"O .BR LONG_MAX , .\"O or .\"O .B LONG_MIN .\"O .RB ( LLONG_MAX .\"O or .\"O .B LLONG_MIN .\"O for .\"O .BR strtoll ()) .\"O on both success and failure, the calling program should set .\"O .I errno .\"O to 0 before the call, .\"O and then determine if an error occurred by checking whether .\"O .I errno .\"O has a nonzero value after the call. .B strtol () からは成功、失敗どちらの場合でも 0, .BR LONG_MAX , .B LONG_MIN .RB ( strtoll () では .BR LLONG_MAX , .BR LLONG_MIN ) が返る可能性があるので、 プログラムは関数を呼び出す前に .I errno を 0 に設定し、呼び出し後に .I errno が 0 以外の値かどうかを確認しエラーが発生したかどうかを判断する 必要がある。 .\"O In locales other than the "C" locale, other strings may also be accepted. .\"O (For example, the thousands separator of the current locale may be .\"O supported.) "C" 以外のロケールの場合、その他の文字列も受け付けられるかもしれない。 (例えば、現在のロケールの 1000 毎の区切り文字がサポートされているかもしれない。) .LP .\"O BSD also has BSD には、 .sp .in +4n .nf .BI "quad_t strtoq(const char *" nptr ", char **" endptr ", int " base ); .sp .in .fi .\"O with completely analogous definition. という完全に同様の定義を持つ関数がある。 .\"O Depending on the wordsize of the current architecture, this .\"O may be equivalent to .\"O .BR strtoll () .\"O or to .\"O .BR strtol (). 使用中のアーキテクチャのワード長次第であるが、この関数は .BR strtoll () か .BR strtol () と等価となることもある。 .\"O .SH EXAMPLE .SH 例 .\"O The program shown below demonstrates the use of .\"O .BR strtol (). .\"O The first command-line argument specifies a string from which .\"O .BR strtol () .\"O should parse a number. .\"O The second (optional) argument specifies the base to be used for .\"O the conversion. .\"O (This argument is converted to numeric form using .\"O .BR atoi (3), .\"O a function that performs no error checking and .\"O has a simpler interface than .\"O .BR strtol ().) 以下のプログラムは .BR strtol () の使用例である。 最初のコマンドライン引き数には .BR strtol () が数字として解釈する文字列を指定する。 (省略可能な) 二番目の引き数には 変換に使用される基数を指定する (この引き数は .BR atoi (3) を使って数値に変換される。 .BR atoi (3) は .BR strtol () よりも簡単なインタフェースを持つ関数で、 その中ではエラーチェックは行われない)。 .\"O Some examples of the results produced by this program are the following: このプログラムの実行例をいくつか以下に示す: .in +4n .nf .RB "$" " ./a.out 123" strtol() returned 123 .RB "$" " ./a.out \(aq 123\(aq" strtol() returned 123 .RB "$" " ./a.out 123abc" strtol() returned 123 Further characters after number: abc .RB "$" " ./a.out 123abc 55" strtol: Invalid argument .RB "$" " ./a.out \(aq\(aq" No digits were found .RB "$" " ./a.out 4000000000" strtol: Numerical result out of range .fi .in .\"O .SS Program source .SS プログラムのソース \& .nf #include #include #include #include int main(int argc, char *argv[]) { int base; char *endptr, *str; long val; if (argc < 2) { fprintf(stderr, "Usage: %s str [base]\\n", 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\\n"); exit(EXIT_FAILURE); } /* If we got here, strtol() successfully parsed a number */ printf("strtol() returned %ld\\n", val); if (*endptr != \(aq\\0\(aq) /* Not necessarily an error... */ printf("Further characters after number: %s\\n", endptr); exit(EXIT_SUCCESS); } .fi .\"O .SH "SEE ALSO" .SH 関連項目 .BR atof (3), .BR atoi (3), .BR atol (3), .BR strtod (3), .BR strtoul (3)