名前空間
変種
操作

atoi, atol, atoll

提供: cppreference.com
< c‎ | string‎ | byte
ヘッダ <stdlib.h> で定義
int       atoi( const char *str );
long      atol( const char *str );
long long atoll( const char *str );
(C99以上)

str の指すバイト文字列内の整数値を解釈します。

最初の非ホワイトスペース文字が見つかるまで、あらゆるホワイトスペース文字を破棄し、その後、有効な整数表現を形成する可能な限り多くの文字を取得し、それを整数値に変換します。 有効な整数値は以下の部分から構成されます。

  • (オプション) 正または負の符号
  • 数字

目次

[編集] 引数

str - 解釈するヌル終端バイト文字列を指すポインタ

[編集] 戻り値

成功した場合は str の内容に対応する整数値。 変換後の値が対応する戻り値の型の範囲外の場合、戻り値は未定義です。 変換が行えない場合は、 0 が返されます。

[編集]

#include <stdio.h>
#include <stdlib.h>
 
int main(void)
{
    printf("%i\n", atoi(" -123junk"));
    printf("%i\n", atoi("0"));
    printf("%i\n", atoi("junk"));         // no conversion can be performed
    printf("%i\n", atoi("2147483648"));   // UB: out of range of int
}

出力:

-123
0
0
-2147483648

[編集] 参考文献

  • C11 standard (ISO/IEC 9899:2011):
  • 7.22.1.2 The atoi, atol, and atoll functions (p: 341)
  • C99 standard (ISO/IEC 9899:1999):
  • 7.20.1.2 The atoi, atol, and atoll functions (p: 307)
  • C89/C90 standard (ISO/IEC 9899:1990):
  • 4.10.1.2 The atoi function
  • 4.10.1.3 The atol function

[編集] 関連項目

バイト文字列を整数値に変換します
(関数) [edit]
バイト文字列を符号無し整数値に変換します
(関数) [edit]
atoi, atol, atollC++リファレンス