Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

atof() for float instead of double

Tags:

c

ansi-c

atof

atof() returns a double, which results in a warning when I assign it to a float-value (and yes, I definitively have to use float).

So my question: is there a atof()-variant available which returns a plain float? Or do I have to solve this by a cast - which would be a pity because it wastes resources for creating a double which will be thrown away immediately.

like image 238
Elmi Avatar asked Dec 07 '16 08:12

Elmi


People also ask

What does atof () do in C?

The atof() function converts a character string to a double-precision floating-point value. The input string is a sequence of characters that can be interpreted as a numeric value of the specified return type.

What is the difference between double and float type of data?

float and double both have varying capacities when it comes to the number of decimal digits they can hold. float can hold up to 7 decimal digits accurately while double can hold up to 15.

How do you write atof?

Syntax of atof() functiondouble atof (const char* str); It accepts pointer constant to character str which is string representation of floating pointer number. On success, it returns converted floating point number as double type. If the given string str has no possible conversion then returns 0.0 .

Where is atof defined?

Defined in header <stdlib.h> double atof( const char* str ); Interprets a floating-point value in a byte string pointed to by str . Function discards any whitespace characters (as determined by std::isspace()) until first non-whitespace character is found.


1 Answers

Use strtof instead, it will return a float.

See documentation here

like image 157
Henningsson Avatar answered Sep 28 '22 00:09

Henningsson