繁体   English   中英

为什么strtof总是输出0.0000?

[英]Why does strtof always output 0.0000?

在我的本地计算机上,使用浮点数调用strtof可以正常运行,但在学校的服务器上strtof始终返回0.000000 我检查了一下是否在errno存储了什么,因为0意味着错误,但是它表示成功。 有谁知道为什么会这样?

这是代码。

#include <stdlib.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
    printf("%f\n", strtof(argv[1],0));
    return 0;
}

简短版本:使用-std=gnu99-std=c99编译。 解释如下。

我在自己的盒子上重现了类似的“问题”。 但是,当我尝试编译时:

# gcc -Wall -o float float.c
float.c: In function 'main':
float.c:6: warning: implicit declaration of function 'strtof'
float.c:6: warning: format '%f' expects type 'double', but argument 2 has type 'int'

所以我看了man页的strtof() ,它说:

SYNOPSIS
   #include <stdlib.h>

   double strtod(const char *nptr, char **endptr);

   #define _XOPEN_SOURCE=600   /* or #define _ISOC99_SOURCE */
   #include <stdlib.h>

   float strtof(const char *nptr, char **endptr);
   long double strtold(const char *nptr, char **endptr);

这意味着在包含stdlib.h之前,这些值之一必须是#define d。 但是,我只是重新编译了-std=gnu99 ,它为我定义了其中之一,并且可以工作。

# gcc -std=gnu99 -Wall -o float float.c
# ./float 2.3
2.300000

道德:始终使用-Wall编译。 ;-)

是否已在定义strtof的标头中包含该标头(stdlib.h),否则可能会得到0.0,因为默认情况下,C中的未知函数被视为返回int。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM