atoi/atol/atof
int atoi( const char *str )
long atol( const char *str )
double atof( const char *str )
atoi関数は文字列strをint型に変換して返す。
atol関数は文字列strをlong型に変換して返す。
atof関数は文字列strをdouble型に変換して返す。
文字列strをint型に変換してint型変数iに代入する
文字列strをlong型に変換してlong型変数lに代入する
文字列strをdouble型に変換してdouble型変数dに代入する
実行結果
・先行する空白は無視される。
・'+'または'-'がある時は符合とされる。
・符号指定が省略された場合は'+'とみなされる。
・文字列に数字でない文字が現れたら、そこで変換を終了する
long atol( const char *str )
double atof( const char *str )
atoi関数は文字列strをint型に変換して返す。
atol関数は文字列strをlong型に変換して返す。
atof関数は文字列strをdouble型に変換して返す。
i = atoi( str )
文字列strをint型に変換してint型変数iに代入する
l = atol( str )
文字列strをlong型に変換してlong型変数lに代入する
d = atof( str )
文字列strをdouble型に変換してdouble型変数dに代入する
#include <stdio.h>
#include <stdlib.h>
void main()
{
int d;
printf( "%d\n", d = atoi( " 100" ) );
printf( "%d\n", d = atoi( " 100 234" ) );
printf( "%d\n", d = atoi( "-200" ) );
printf( "%d\n", d = atoi( "+200" ) );
printf( "%d\n", d = atoi( "- 200" ) );
printf( "%d\n", d = atoi( "300abc4" ) );
}
実行結果
100
100
-200
200
0
300
・先行する空白は無視される。
・'+'または'-'がある時は符合とされる。
・符号指定が省略された場合は'+'とみなされる。
・文字列に数字でない文字が現れたら、そこで変換を終了する