Questions tagged [atof]

atof() is the C runtime library function for converting the ASCII representation of a number to a floating point double.

Use this tag on for all questions about the use of atof() or where it does not seem to be working correctly.

Closely related are:

  • for functions converting text to int, long, or long long
  • for converting to long from text in any base from 2 through 36. Or choose a base automatically as the C compiler does depending on how the number is written. strtoul() converts to an unsigned long.
  • for double
  • for converting one or more values at a time directed by a format specification

SYNPOSIS

#include <stdlib.h>

double atof(const char *nptr);

atof() returns a value of zero if there is any problem during the conversion, though the documentation says that it does not detect errors. Since converting a zero also returns zero, there is no easy way of distinguishing an error from a correct conversion of zero. Use either scanf() or strtod() if error checking is needed.

atof() accepts integers, fixed decimal notation, and scientific notation. Some implementations also recognize the strings INF or INFINITY (disregarding case), NAN (disregarding case) optionally followed by (, a sequence of characters, followed by ). The character string specifies an implementation-dependent type of NAN. (Documentation of these strings are scant.)

Some implementations support hexadecimal formatted floats. These begin with 0x or 0X, followed by at least one hex digit, an optional decimal point (locale-specific), more digits, an optional p or P followed by the binary exponent in hex.

129 questions
2
votes
2 answers

atof reads string as 0

I suppose I am just stupid (I tried all). Help me out of here: #include #include int main() { const char * y = "747.0"; double x; x = atof(y); printf("%.5f\n",x); printf("%e\n",x); return…
varantir
  • 6,624
  • 6
  • 36
  • 57
2
votes
2 answers

memory leak strtok & atof

int fromVectoEigen(vector & source, double ** & array, int & n) { cout<<"n:"<= 0 ; j--) // iterate through each line { string mystring = source.back(); // take last…
dieHellste
  • 252
  • 1
  • 2
  • 11
2
votes
10 answers

any one know how to convert a huge char array to float, very huge array, performance better than the atof/strtod/sscanf

I got a char array, a huge array char p[n] read from a txt like. //1.txt 194.919 -241.808 234.896 195.569 -246.179 234.482 194.919 -241.808 234.896 ... foo(char *p, float x, float y, float z) { } I tried to use atof, strtod, but they are real…
user236515
  • 51
  • 7
2
votes
2 answers

Why this atof() returns 0.0 for non-0.0 string numbers?

I have to read a line like this: 0.000000 0.000000 -1.000000 -1.000000 0.230392 0.562016 -1.000000 -1.000000 Using strtok() and a while loop, I'm trying to extract each float numer and store it on my program. There is a problem when converting the…
TheUnexpected
  • 3,077
  • 6
  • 32
  • 62
1
vote
3 answers

atof, structs and pointers in c++

This is a sample of code that I've written for a school science fair. #include #include using namespace std; struct FUNC{ char token; FUNC *left; FUNC *right; }; double eval (FUNC *head){ if (head->left==NULL){…
Stem
  • 11
  • 1
1
vote
1 answer

Is there any difference in the way atof and strtod work?

I know that strtod() and atof() functions are used for conversion from string to double. But I can't figure out the difference between these two functions. Is there any difference between these two functions, if yes then please let me know... Thanks…
Abhishek Jaiswal
  • 288
  • 2
  • 14
1
vote
1 answer

Is there an alternative to atof in C++

I am finding that atof is limited in the size of a string that it will parse. Example: float num = atof("49966.73"); cout << num; shows 49966.7 num = atof("499966.73"); cout << num; shows 499966 I need something that will parse the whole string…
Deek880
  • 29
  • 3
1
vote
2 answers

C++: strod() and atof() are not returning double as expected

I am trying to make strings into doubles using the values that I obtained from a .txt file. The doubles I am obtaining have no decimals. I believe that this is because, in the .txt, the decimals of the numbers are separated by a comma instead of a…
ao_martinv
  • 55
  • 6
1
vote
1 answer

Weird std::atof bug with gcc 7.4.0

I've got this weird behavior with std::atof in Ubuntu 18.0.4 with Qt Creator(4.10) as IDE / gcc 7.4.0 : It parses strings as normal when i run in debug mode from QtCreator. But it floors when i run normally. Example code with this…
hsyntsy
  • 21
  • 4
1
vote
1 answer

in C++ using atof to convert string to decimal not working

I'm trying to convert the string variable passed by reference to a decimal. When I put in a string in quotes using atoi it works, but not with a string variable. What should I do instead? void stringDecision(string& assembledString) { double…
Jason
  • 119
  • 8
1
vote
3 answers

How to read a file, pick up data and calculate

I am reading a book and I can't figure out this try it out : (it is in a non-english language so I translated it) Write a program that ask for a number of students n, select n students (in a dynamic way), the name is 10 characters and note on 5…
Bon_chan
  • 331
  • 2
  • 5
  • 15
1
vote
2 answers

Why is there a loss in precision when converting char * to float using sscanf_s or atof?

I am trying to convert a char * containing just a floating point value to a type float, but both sscanf_s and atof both produce the same invalid result. char t[] = "2.10"; float aFloat( 0.0f ), bFloat( 0.0f ); sscanf_s( t, "%f", &aFloat ); bFloat =…
MikeO
  • 391
  • 4
  • 17
1
vote
3 answers

Why does the atof() function not return 0 when I pass it a string with invalid characters after a number?

I need to convert a string into a float. If the string is not a number I wish to return 0. I have tried to test if the atof() function would work for this by using the following code: printf("%f", atof("1a")); From my understanding of atof, the…
JinKazama
  • 81
  • 3
  • 15
1
vote
1 answer

File contents falsification with bitwise operation in C

I have this little program which reads a long text document from allomany.txt.It contains a long text where we have some numbers.Now I need to falsify every number: Let is assume the program needs to read the text from file and I need to find a…
Zsombi
  • 84
  • 1
  • 9
1
vote
2 answers

How convert very long string to double in portable C

I want to convert a very long string of numbers to a double in a portable way in C. In my case, portable means that it would work in Linux and Windows. My ultimate goal is to be able to pack a string of numbers into an 8-byte double and…
PaeneInsula
  • 2,010
  • 3
  • 31
  • 57
1 2
3
8 9