如果用atof
则需要定义x为字符数组.
可以这样
char
x[100]
float
t
scanf("%s",x)
if(strcmp(x,
"stop")==0)
break
t=atof(x)
sum=sum+t
函数名: atoi
功 能: 把字符串转换成长整型数
用 法: #include <stdlib.h>
int atoi(const char *nptr)
程序例:
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
int n
char *str = "1234"
n = atoi(str)
printf("string = %s integer = %d\n", str, n)
return 0
}
运行结果是:
string =1234 integer=1234
函数名: atof
功 能: 把字符串转换成浮点数
用 法: double atof(const char *nptr)
程序例:
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
float f
char *str = "12345.67"
f = atof(str)
printf("string = %s float = %f\n", str, f)
return 0
}