473,327 Members | 1,919 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

string2float

How i can convert *char to float?

Aug 14 '06 #1
6 2802
po**********@gmail.com writes:
>How i can convert *char to float?
atof();

--
Chris,
Aug 14 '06 #2
Chris McDonald wrote:
po**********@gmail.com writes:

>>How i can convert *char to float?


atof();
strtod() is better, but neither actually converts a
string to float: both convert to double. The only way I
can think of to convert directly to float is

if (sscanf(my_string, "%f", &float_variable) == 1)

--
Eric Sosman
es*****@acm-dot-org.invalid

Aug 14 '06 #3

Eric Sosman wrote:
Chris McDonald wrote:
po**********@gmail.com writes:

>How i can convert *char to float?

atof();

strtod() is better, but neither actually converts a
string to float: both convert to double. The only way I
can think of to convert directly to float is

if (sscanf(my_string, "%f", &float_variable) == 1)
C99 has strtof() which will convert directly to float representation.

Robert Gamble

Aug 14 '06 #4
Chris McDonald wrote:
po**********@gmail.com writes:
>How i can convert *char to float?

atof();
Not a very good choice. You can't test to see if it worked and if the
value cannot be represented as a double it invokes undefined behaviour
(i.e. anything can happen). A far better function to use is strtod. This
allows you to test how much of the string was converted and if it is out
of range behave in a defined manner including setting errno, and thus
you can do error checking reliable and report problems such as the user
entering "fiftythree" of "12e456789".
--
Flash Gordon
Still sigless on this computer.
Aug 14 '06 #5
"Robert Gamble" <rg*******@gmail.comwrites:
Eric Sosman wrote:
>Chris McDonald wrote:
po**********@gmail.com writes:
How i can convert *char to float?

atof();

strtod() is better, but neither actually converts a
string to float: both convert to double. The only way I
can think of to convert directly to float is

if (sscanf(my_string, "%f", &float_variable) == 1)

C99 has strtof() which will convert directly to float representation.
And if your implementation doesn't have strtof(), it's easy enough to
call strtod() and assign the result to a float. It's even easier to
use double in the first place.

sscanf() has the disadvantage that, if the floating-point literal in
the string represents a value that can't be represented in the target
type, the behavior is undefined. strtod() has well-defined behavior
on overflow. (It would have been nice if sscanf() had been defined
this way as well.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Aug 14 '06 #6
Keith Thompson wrote:
"Robert Gamble" <rg*******@gmail.comwrites:
Eric Sosman wrote:
Chris McDonald wrote:
po**********@gmail.com writes:
How i can convert *char to float?

atof();

strtod() is better, but neither actually converts a
string to float: both convert to double. The only way I
can think of to convert directly to float is

if (sscanf(my_string, "%f", &float_variable) == 1)
C99 has strtof() which will convert directly to float representation.

And if your implementation doesn't have strtof(), it's easy enough to
call strtod() and assign the result to a float.
Except that if the result can be represented as a double but is outside
the range of float then assigning the return value to a float will
result in undefined behavior. In this case one would need to check the
return value of strtod before assigning it to float, which is a pain
and probably why strtof() was added.
It's even easier to use double in the first place.
Agreed.
sscanf() has the disadvantage that, if the floating-point literal in
the string represents a value that can't be represented in the target
type, the behavior is undefined. strtod() has well-defined behavior
on overflow. (It would have been nice if sscanf() had been defined
this way as well.)
Robert Gamble

Aug 14 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.