c 字符串转数字

atoi, atol, atoll,atof

函数原型

#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 = %d\n", a);
	//长整形
	long int b = atol(buf);
	printf("b = %ld\n", b);
	//long long 
	long long c = atoll(buf);
	printf("c = %lld\n", c);
        //double
	double d = atof(buf);
	printf("d = %lf\n", d);
	return 0;
}

注意,atof的返回值是double类型的,但是用float去接受也没错,和字符串的大小有关系。

只可以转换buf中最前面的数字,一旦出现别的字符,就无法继续识别

猜你喜欢

转载自blog.csdn.net/qq_34759481/article/details/81780138