我试着不看C语言书就重现了atof()函数,我写了它,每一行代码似乎都是正确的,但是当我编译程序时,它告诉我存在冲突类型错误。
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
float atof(char s[]); // the error is produced here
int main()
{
char line[100] = "672";
float num = atof(line);
printf("%f", num);
return 0;
}
// I didn't finish the function yet...
float atof(char s[])
{
int i, sign;
float n;
for(i = 0; s[i] == ' '; i++) // skip white spaces
;
// set sign to the -1 if it is negative, 1 if else
sign = (s[i] == '-') ? -1 : 1;
if(s[i] == '+' || s[i] == '-')
i++;
for(n = 0.0; isdigit(s[i]); i++)
{
n = 10.0 * n + (s[i] - '0');
}
return n * sign;
}
我试过了
return 2.5F
并加倍,return 2.5
但这并不能解决问题。(顺便说一句我使用gcc编译器版本9.2.0)
我以为出现问题是因为该函数已经存在于我所包含的其中一个库中,所以我将其更改为,
strtof()
并且问题相同。
你几乎有:strtof()
和atof()
对你的平台都库函数。使用类似my_strtof()
或的东西my_atof()
。
参考:
strtof()
-原型于stdlib.h
atof()
-原型于stdlib.h
两者都是标准的C库函数。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句