atoi() 函数在 stdlib.h 头文件中定义。它有助于将给定的字符串参数(str)转换为整数值,即 int 类型。
int atoi(const char *str); #where str should be a string
atoi() 函数接受一个参数。在 C 语言中,stdlib.h 头文件支持类型转换。
| 参数 | 描述 | 必需/可选 |
|---|---|---|
| str | 表示浮点数的字符串 | 必需 |
atoi() 函数的返回值是一个整数值。
| 输入 | 返回值 |
|---|---|
| 成功 | 整数值 |
| 无效转换 | 零 |
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int x;
char st[20];
strcpy(st, "23333444");
x = atoi(st);
printf("String value is %s, Int value = %d\n", st, x);
strcpy(st, "C programming");
x = atoi(st);
printf("String value is %s, Int value = %d\n", st, x);
return(0);
}
输出
String value is 23333444, Int value is 23333444 String value is C programming, Int value is 0
#include <stdio.h>
int main (){
char arr[10] = "100";
int v = atoi(arr);
printf("Value is %d\n", v);
return 0;
}
输出
Value is 100