- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我的 C 生锈了。我正在尝试将字符串转换为 float (或 double )。我试过 atof() 和 strtof(),并尝试按照说明进行操作 here和 here .运气不好。
这是我的代码:
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[]) {
float x;
char a[20];
strcpy(a,argv[1]);
x = strtof(a);
printf("%s\n",a);
printf("%f\n",x);
return 0;
}
运气不好。这是命令行上的输出:
prompt$ ./a.out 2.34
2.34
1426063.000000
prompt$
如果我改用 atoi(),我会得到 0.0。
显然我缺少一些简单的东西。这是在 ubuntu fwiw 上使用 gcc 4.8.4。
如果我使用 -static 标志编译,或者使用 double 代替 float(和 %e 代替 %f),或者包含 math.h,都没有区别。在某些情况下,输出会发生变化,但仍然是无意义的。
谢谢,彼得
最佳答案
您还没有包括 <stdlib.h>
得到 strtof()
的原型(prototype)。所以返回类型默认为int。但是自 C99 以来,隐式 int 规则已从 C 中删除。
用法也不对。至少应该是:
char *eptr;
x = strtof(a, &eptr);
并检查 eptr
对于转换失败。
编译时启用警告。 GCC 警告:
warning: implicit declaration of function ‘strtof’ [-Wimplicit-function-declaration]
关于将字符串中的数字转换为 c 中的 float : atof(), strtof() 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34188066/
这个问题已经有答案了: Is floating point math broken? (33 个回答) 已关闭 4 年前。 编辑- 所以我明白这个问题没有解决办法。有谁知道 C 中的函数可以从数组(s
这个问题已经有答案了: Why are floating point numbers inaccurate? (5 个回答) Why is scanf taking the wrong input f
我有一个关于分配和释放内存的问题。 我想循环读取一个char buffer,并将float值保存到一个vector中。我通过读取 fstream 获取缓冲区。 但我的方法在最后删除缓冲区时总是崩溃。
在 Visual Studio 2013 中,我有以下测试代码: #include #include #include #include int main ( int argc, const
这个问题在这里已经有了答案: How do I print a double value with full precision using cout? (17 个答案) 关闭 6 年前。 这里:
我根本找不到这个函数在哪个库/头文件中,我看了很多人使用这个函数的例子,但是没有结果......这些是我包含的所有内容: #include "Console.h" #include "Direct3D
首先,使用标志 -Wall、-ansi、-pedantic 进行编译。没有警告或错误。并且没有任何调试有帮助。 基本上我的代码工作正常但是当我使用 strtof() 将标记从字符串转换为 float
假设你有一个像“0.1”这样的字符串,它只能近似表示为二进制 float ,你想将它转换为单精度 float 。这可以作为 strtof(s, 0); 或 (float)strtod(s, 0); 直
这可能是什么问题? It doesn't matter what number I choose for str, it is always 26815615859885194199148049996
#include #include #include int main(){ char aaa[35] = "1.25"; char* bbb = &(aaa[0]); char**
我正在尝试将 CSV 文件解析为 C 中的二维数组。我想制作一个具有结构的矩阵: typedef struct { int row; int col; float **arr;
我的这部分 C 代码有些问题。除了返回 0.000 而不是 float 的函数“strtof”之外,一切都应该运行良好。 代码应该做什么:读一行,例如“一个12”如果第一个字符是“a”,则使用 str
可能这是一个非常愚蠢的问题,但我不明白......为了从 ASCII 表示转换为相关数字,我在 Visual Studio 2012 中使用 strtof()。根据 https://msdn.micr
与 包含以下代码给出了 123.34 的输出. #include int main() { char *str="123.34"; float f= strtof(str,NULL)
我的 C 生锈了。我正在尝试将字符串转换为 float (或 double )。我试过 atof() 和 strtof(),并尝试按照说明进行操作 here和 here .运气不好。 这是我的代码:
是否有任何计划添加在当前语言环境下不变的 C 标准库字符串处理函数版本? 目前有很多脆弱的解决方法,例如,来自 jansson/strconv.c: static void to_locale(str
标准 C 库函数 strtof 和 strtod 具有以下签名: float strtof(const char *str, char **endptr); double strtod(const c
标准 C 库函数 strtof 和 strtod 具有以下签名: float strtof(const char *str, char **endptr); double strtod(const c
我在启动时立即收到以下错误,但仅适用于运行 API (ProfilerService.java:44) at [[packagename]].App.(App.kt
我是一名优秀的程序员,十分优秀!