#include<stdlib.h> #include<errno.h> int main() { char a[24] = {0}; char *b = NULL; float c1 = 0.0; float c2 = 0.0; scanf("%s", a); c1=atof(a); printf("b = %d/n",b); c2=strtod(a,&b); if(b[0]==0) { printf("b is null/n"); } else if(b[0] == ' ') { printf("b is spaces/n"); } printf("b = %d/n",b); printf("b = %s/n",b); printf("c1=%.2f, c2=[%f]/n",c1, c2); printf("errno = [%d]/n", errno);
return 0; }
輸入"11.1a"
輸出
b = 0 b = 804399012 b = a c1=11.10, c2=[11.100000] errno = [0]
可見atof不能判斷出字符串是否是合法字符串,
用函數strtof可以根據strtod(a,&b),b是是否是""(空串,但b的地址不爲空)來判斷字符串是否是合法的字符串