下列库函数的头文件均为#include<stdlib>
函数原型
1. atof ()
头文件: atof
功 能: 把字符串转换成浮点数
用 法: double atof(const char *nptr);
程序例:
1 #include <stdlib.h> 2 #include <stdio.h> 3 4 int main(void) 5 { 6 float f; 7 char *str = "12345.67"; 8 9 f = atof(str); 10 printf("string = %s float = %f ", str, f); 11 return 0; 12 }
运行结果:
2. atoi()
函数名: atoi
功 能: 把字符串转换成长整型数
用 法: int atoi(const char *nptr);
程序例:
1 #include <stdlib.h> 2 #include <stdio.h> 3 4 int main(void) 5 { 6 int n; 7 char *str = "12345.67"; 8 9 n = atoi(str); 10 printf("string = %s integer = %d ", str, n); 11 return 0; 12 }
输出结果: