【问题标题】:Atof is not working in C and without atof is also not working in debugAtof 不能在 C 中工作,没有 atof 也不能在调试中工作
【发布时间】:2023-03-31 21:09:01
【问题描述】:

我的 Atof 功能有问题。我正在尝试将字符串转换为浮点数,但是当我在调试部分的 Coocox 软件中尝试时它没有给出任何错误,输出没有显示任何内容。我尝试了两个函数 Atoi 和 Atof。当我使用 Atoi 时没有输出。当我使用 Atof 时程序开始重新启动。我把 atof 的 stdlib.h 定义放在这里。但是这里的浮点值是 atoff 。我在 C 中的 Dev C++ 中尝试了相同的代码,它工作得很好。我在不使用 Atof 的情况下使用的其他东西,但这次程序又重新启动了。这适用于 Dev C。但不适用于 Coocox。我该如何解决这个问题?只是atoff有区别!有什么关系?我用的是stdlib.h,编译没有错误!

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

int main ()
{
    float c;
    int b;

    char *array[1] = {"52.43525"};

    b=(atoi(array[0])/100);
    c=((atof(array[0]))/100.0)*100.0;
    c/=60;
    c+=b;

    printf ("%f\n", c);

    return 0;
}

-----stdlib.h----
double  _EXFUN(atof,(const char *__nptr));
#if __MISC_VISIBLE
float   _EXFUN(atoff,(const char *__nptr));
#endif
int _EXFUN(atoi,(const char *__nptr));
int _EXFUN(_atoi_r,(struct _reent *, const char *__nptr));
long    _EXFUN(atol,(const char *__nptr));
long    _EXFUN(_atol_r,(struct _reent *, const char *__nptr));
------------------------------------

【问题讨论】:

  • 如果确实是atof() 不起作用,您应该简化代码以确保问题是具体的。
  • 当我删除 atof() 时,一切运行良好。没问题!
  • 很好奇,如果float c改成double c也会出现同样的问题?
  • 请注意,(atoi(array[0]) / 100) 的计算结果为 0。
  • 我又试了atof(双格式)一样!

标签: c debugging atoi atof coocox


【解决方案1】:

更正所有编译器警告后,生成的代码如下:

注意:由于没有用到数组功能,所以我把它改成了一个简单的指针。这对输出没有影响。

#include <stdio.h>   // printf()
//#include <string.h> -- contents not used
#include <stdlib.h>  // atoi(), atof()

int main ()
{
    float c;
    int b;

    char *array = {"52.43525"};

    b =  atoi(array);
    c =  ( (float)( atof(array) ) / 100.0f ) * 100.0f;
    c /= 60.0f;
    c += (float)b;

    printf ("%f\n", c);

    return 0;
}

运行程序导致:

52.873920

因此,如果您的编译器没有找到 atof(),则说明编译器有问题。

【讨论】:

  • 又重启了。如果问题出在编译器上,怎么办?应该加哪个编译器链接?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-01
  • 2011-05-18
  • 2021-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多