[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 918
  • Last Modified:

strtof in C/C++ on UNIX?

My code compiles perfectly on Windows, but I'm now trying to compile it on a UNIX machine using g++.

The error I'm getting, is
`strtof' undeclared (first use this function)

Is this function exclusive to Windows?

The relevant code is:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <errno.h>
...
//Cast a string to a float, and return true if successful;
// it checks that the string is a number, AND a valid float
bool cast(const char* str, float& f)
{
     char *end;
     f=strtof(str, &end);
     return (str[0] && !*end && errno != ERANGE);
}

Is there an alternative way of doing this that will compile?

Thank you very much! (Urgent)
0
InteractiveMind
Asked:
InteractiveMind
  • 6
  • 4
  • 3
  • +2
4 Solutions
 
Infinity08Commented:
strtof is part of the C99 standard. What version of g++ are you using ?
0
 
InteractiveMindAuthor Commented:
3.0.1
0
 
jkrCommented:
Seems to be a conditional compilation issue. Try

#define  __USE_ISOC99 // <---------------
 
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <errno.h>
 
//Cast a string to a float, and return true if successful;
// it checks that the string is a number, AND a valid float
bool cast(const char* str, float& f)
{
     char *end;
     f=strtof(str, &end);
     return (str[0] && !*end && errno != ERANGE);
}

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
masheikCommented:
the strtod function  can be found from the stdlib.h library
just keep try, modifing your  program and run some thing like this
bool cast(char *s)
{
     char *endp;
     double d;
     long l;
     d = strtod(s, &endp);
     if (s != endp && *endp == `\\0')
         printf("It's a float with value %g\n", d);
     else
     {
         l = strtol(s, &endp, 0);
         if (s != endp && *endp == `\0')
             printf("It's an integer with value %ld\n", 1);
         else
             return 1;
     }
     return 0;
}
0
 
InteractiveMindAuthor Commented:
jkr, your suggestion doesn't seem to work.
I'll have a go with masheik's...

Thanks so far
0
 
InteractiveMindAuthor Commented:
Ah, if I replace strtof with strtod, it compiles... why is that?
0
 
Infinity08Commented:
I'm not sure whether 3.0.1 already had support for the strof function.

Can you check the stdlib.h file to see if it's defined in there ? If so, can you post it here ?
0
 
jkrCommented:
'strtof()' is 'guarded' in 'stdlib.h' by

#ifdef  __USE_ISOC99
/* Likewise for `float' and `long double' sizes of floating-point numbers.  */
extern float strtof (__const char *__restrict __nptr,
                     char **__restrict __endptr) __THROW;

extern long double strtold (__const char *__restrict __nptr,
                            char **__restrict __endptr) __THROW;
#endif

whereas 'strtod()' isn't...
0
 
Infinity08Commented:
>> Ah, if I replace strtof with strtod

strtod converts to double, not to float.
It was already available in C89. strtof was added in C99 (10 years later ;) )
0
 
Infinity08Commented:
>> 'strtof()' is 'guarded' in 'stdlib.h' by

In what version of gcc was that, jkr ?
0
 
masheikCommented:
strtod gives the double value for the input string
you can use this too.
it is because of compiler that might not support the function strtof
thanks
-masheik
0
 
jkrCommented:
>>In what version of gcc was that, jkr ?

Let me see...

"gcc version 2.96"

Reminds me of updating that thing ;o)
0
 
itsmeandnobodyelseCommented:
I would recommend to drop all float variables in your prog and replace them by double. There are only rare cases where float type is still appropriate (e. g. huge fixed-sized arrays where there are only low requirements on precision for each value - though even for these arrays it would be better to turn the float to  int type as an int has a precision of 9 to 10 decimals while a float has only a precision of 6 to 7 decimals).

The same applies, IMO, for short integer type which might have had a sense in the last century and of course for 16-bit devices but rarely nowadays where we have  gigabytes for memory and terabytes for discs.

>>>> Reminds me of updating that thing ;o)

You will regret if you do ;-)  

I am sure there are a lot of questioners who don't even know that there were newer versions available let alone that they even would dare updating ;-)
0
 
Infinity08Commented:
It seems that for 3.0.1, you need this (before the #include <stdlib.h>) :
#define _GLIBCPP_USE_C99 1

Open in new window

0
 
InteractiveMindAuthor Commented:
Thanks all, I ended up just converting to double.


(Infinity,
#define _GLIBCPP_USE_C99 1
doesn't seem to work either.)
0
 
Infinity08Commented:
>> doesn't seem to work either.)

Heh, well, there must be some flag that enables it, since 3.0.1 definitely has an implementation for strtof.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 6
  • 4
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now