atof()因为不通头文件引发的错误

5154阅读 0评论2012-05-07 wdove
分类:C/C++

好久不用C了,这次用到atof()函数,字符串转成浮点数,但老是不成功,转成的值非常奇怪

  1. #include <stdio.h>

  2. void main()
  3. {
  4.     char str[6] = " 97.2";
  5.     printf("%lf\n", atof(str));
  6. }
编译的时候出现warning:a.c: In function ‘main’:
a.c:6: warning: format ‘%lf’ expects type ‘double’, but argument 2 has type ‘int’

结果是:
$ ./a.out
0.000000
这个结果不算什么,我用在另一个地方转成的值更离谱。

再次查了一下atof()的manpage,终于发现头文件居然是.

  1. #include <stdio.h>
    #include

  2. void main()
  3. {
  4.     char str[6] = " 97.2";
  5.     printf("%lf\n", atof(str));
  6. }
这样才是对的.

可见有一个默认的atof()存在于Linux系统中。
上一篇:Ubuntu update时碰到的无法得到登陆界面的问题
下一篇:mount.nfs: an incorrect mount option was specified