函数原型
#include <stdlib.h>int atoi(const char *nptr);long atol(const char *nptr);long long atoll(const char *nptr);double atof(const char *nptr);例子
#include <stdio.h>#include <stdlib.h>int main(){char buf[] = "34536";//整形int a = atoi(buf);printf("a = %dn", a);//长整形long int b = atol(buf);printf("b = %ldn", b);//long long long long c = atoll(buf);printf("c = %lldn", c); //doubledouble d = atof(buf);printf("d = %lfn", d);return 0;}注意,atof的返回值是double类型的,但是用float去接受也没错,和字符串的大小有关系。
只可以转换buf中最前面的数字,一旦出现别的字符,就无法继续识别