strtol, strtoll
From cppreference.com
| ヘッダー <stdlib.h> で定義 |
||
long strtol( const char* str, char** str_end, int base ); |
(C99まで) | |
| long strtol( const char* restrict str, char** restrict str_end, int base ); |
(C99以降) | |
| long long strtoll( const char* restrict str, char** restrict str_end, int base ); |
(C99以降) | |
strで指されるバイト文字列内の整数値を解釈します。
まず、isspaceを呼び出して識別される空白文字をすべて破棄し、最初の非空白文字が見つかるまで続けます。その後、可能な限り多くの文字を読み取って有効なn進数(n = base)の整数表現を形成し、それを整数値に変換します。有効な整数値は、以下の部分で構成されます。
- (任意)プラスまたはマイナスの記号
- (オプション) 8進数を表す接頭辞 (
0) (基数が 8 または 0 の場合にのみ適用されます) - (オプション) 16進数を表す接頭辞 (
0xまたは0X) (基数が 16 または 0 の場合にのみ適用されます) - 数字のシーケンス
baseの有効な値のセットは{0, 2, 3, ..., 36}です。基数2の整数の有効な数字のセットは{0, 1}、基数3の整数は{0, 1, 2}などです。10より大きい基数では、有効な数字にはアルファベット文字が含まれ、基数11の整数ではAaから始まり、基数36の整数ではZzまでとなります。大文字・小文字は区別されません。
現在インストールされている C ロケール によって、追加の数値形式が受け入れられる場合があります。
base の値が 0 の場合、数値基数は自動検出されます。接頭辞が 0 の場合は基数は8進数、接頭辞が 0x または 0X の場合は基数は16進数、それ以外の場合は基数は10進数になります。
入力シーケンスにマイナス記号が含まれていた場合、数字シーケンスから計算された数値は、結果の型で単項マイナス を適用するかのように否定されます。
str_endが指すポインタは、解釈された最後の数値文字の次の文字を指すように設定されます。 str_endがヌルポインタの場合、無視されます。
もし str が空であるか、または期待される形式でない場合、変換は実行されません。そして(str_end がヌルポインタでない場合)、str の値が str_endが指すオブジェクトに格納されます。
目次 |
[編集] パラメータ
| str | - | 解釈されるヌル終端バイト文字列へのポインタ |
| str_end | - | 文字へのポインタへのポインタ |
| base | - | 解釈される整数値の基数 |
[編集] 戻り値
- 成功した場合、 str の内容に対応する整数値が返されます。
- 変換された値が対応する戻り値の型の範囲を超える場合、範囲エラーが発生し(errnoにERANGEを設定)、LONG_MAX、LONG_MIN、LLONG_MAXまたはLLONG_MINが返されます。
- 変換が実行できない場合、0 が返されます。
[編集] 例
このコードを実行
#include <errno.h> #include <limits.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> int main(void) { // parsing with error handling const char* p = "10 200000000000000000000000000000 30 -40 junk"; printf("Parsing '%s':\n", p); for (;;) { // errno can be set to any non-zero value by a library function call // regardless of whether there was an error, so it needs to be cleared // in order to check the error set by strtol errno = 0; char* end; const long i = strtol(p, &end, 10); if (p == end) break; const bool range_error = errno == ERANGE; printf("Extracted '%.*s', strtol returned %ld.", (int)(end-p), p, i); p = end; if (range_error) printf("\n --> Range error occurred."); putchar('\n'); } printf("Unextracted leftover: '%s'\n\n", p); // parsing without error handling printf("\"1010\" in binary --> %ld\n", strtol("1010", NULL, 2)); printf("\"12\" in octal --> %ld\n", strtol("12", NULL, 8)); printf("\"A\" in hex --> %ld\n", strtol("A", NULL, 16)); printf("\"junk\" in base-36 --> %ld\n", strtol("junk", NULL, 36)); printf("\"012\" in auto-detected base --> %ld\n", strtol("012", NULL, 0)); printf("\"0xA\" in auto-detected base --> %ld\n", strtol("0xA", NULL, 0)); printf("\"junk\" in auto-detected base --> %ld\n", strtol("junk", NULL, 0)); }
実行結果の例
Parsing '10 200000000000000000000000000000 30 -40 junk': Extracted '10', strtol returned 10. Extracted ' 200000000000000000000000000000', strtol returned 9223372036854775807. --> Range error occurred. Extracted ' 30', strtol returned 30. Extracted ' -40', strtol returned -40. Unextracted leftover: ' junk' "1010" in binary --> 10 "12" in octal --> 10 "A" in hex --> 10 "junk" in base-36 --> 926192 "012" in auto-detected base --> 10 "0xA" in auto-detected base --> 10 "junk" in auto-detected base --> 0
[編集] 参考文献
- C23標準 (ISO/IEC 9899:2024)
- 7.22.1.4 The strtol, strtoll, strtoul, and strtoull functions (p: TBD)
- C17標準 (ISO/IEC 9899:2018)
- 7.22.1.4 The strtol, strtoll, strtoul, and strtoull functions (p: 251-252)
- C11標準 (ISO/IEC 9899:2011)
- 7.22.1.4 The strtol, strtoll, strtoul, and strtoull functions (p: 344-345)
- C99標準 (ISO/IEC 9899:1999)
- 7.20.1.4 The strtol, strtoll, strtoul, and strtoull functions (p: 310-311)
- C89/C90標準 (ISO/IEC 9899:1990)
- 4.10.1.5 The strtol function
[編集] 関連項目
| (C99) |
バイト文字列を整数値に変換する (関数) |
| (C99) |
バイト文字列を符号なし整数値に変換する (関数) |
| (C95)(C99) |
ワイド文字列を整数値に変換する (関数) |
| (C95)(C99) |
ワイド文字列を符号なし整数値に変換する (関数) |
| C++ドキュメント for strtol, strtoll
| |