Luc*_*cas 1 c string floating-point
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;
}
Run Code Online (Sandbox Code Playgroud)
简短版:用-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'
Run Code Online (Sandbox Code Playgroud)
所以我看了一下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);
Run Code Online (Sandbox Code Playgroud)
这意味着其中一个值必须#define在包括之前stdlib.h.但是,我只是重新编译-std=gnu99,并为我定义其中一个,它的工作原理.
# gcc -std=gnu99 -Wall -o float float.c
# ./float 2.3
2.300000
Run Code Online (Sandbox Code Playgroud)
道德:总是编译-Wall.;-)