Atof is not working in C and without atof is also not working in debug

2k views Asked by At

I have problems with Atof function. I am trying to convert string to float but it is not giving any error when I try in Coocox software in Debug section, Output is not showing anything. I tried two functions Atoi and Atof. When I use Atoi there is no output.When I use Atof The program starting restart. I put stdlib.h definition for atof in here.But it is atoff for float value in here.I tried same code in Dev C++ in C it is working very well. Other things I use without working Atof but this time again the program is restarting. This is working on Dev C. But not in Coocox. How can I solve the problem? There is only difference atoff! What can it be related? I used stdlib.h and there is no error in compilation!

#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));
------------------------------------
1

There are 1 answers

1
user3629249 On

after correcting all the compiler warnings, this was the resulting code:

Note: since the array feature was not used, I changed it to a simple pointer. This made no difference int the output.

#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;
}

running the program resulted in:

52.873920

So if your compiler is not finding atof() it is a problem with the compiler.