文字列変換関数atoi
ヘッダ #include "stdlib.h"
形 式 int atoi(const char *nptr);
機 能 nptrが指す文字列の先頭部分をint型の表現に変換する。エラーが発生したときの動作を除くと、(int)strtol(nptr, (char **)NULL, 10)と等価である。
返却値 変換された値を返す。結果の値がint型で表現できないときの動作は定義されない(処理系に依存する)。
#include
int _space_sign(const char *s, const char **endptr)
{
while (isspace((unsigned char)*s))
++s;
int sign = 0;
switch (*s)
{
case '-':
sign = -1;
// fall through
case '+':
++s;
break;
}
*endptr = s;
return sign;
}
#include
int atoi(const char *s)
{
int sign = _space_sign(s, &s);
int result;
for (result = 0; isdigit((unsigned char)*s); s++)
result = result * 10 + *s - '0';
if (sign != 0)
result = -result;
return result;
}
[Quote]:http://bohyoh.com
http://libc.blog47.fc2.com/blog-entry-74.html