int atoi(const char *str)
描述
C 库函数 int atoi(const char *str) 把参数 str 所指向的字符串转换为一个整数(类型为 int 型)。
参数
str -- 要转换为整数的字符串。
返回值
成功时,该函数返回转换后的整数作为int
值。
如果转换后的值超出a的可表示值范围int
,则会导致未定义的行为。若无法进行转换,则返回 0。
代码示例
/* atoi example */
#include <stdio.h> /* printf, fgets */
#include <stdlib.h> /* atoi */
int main ()
{
int i;
char buffer[256];
printf ("Enter a number: ");
fgets (buffer, 256, stdin);
i = atoi (buffer);
printf ("The value entered is %d. Its double is %d.\n",i,i*2);
return 0;
}