Question #601

In C, how to convert a char* to double ?

Answer

In C / C++, atof() is a dedicated function for converting strings to floats (or doubles). First, include stdlib.h:

#include <stdlib.h>

Then, simply convert strings to floats (or doubles) :

char string[] = "10.66814";
float value = atof(string);

Test the previous code online on OneCompiler.


If the first sequence of non-whitespace characters in string does not form a valid floating-point number, the function atof() returns 0.0.

11 events in history
Question by Alphonsio 01/07/2021 at 07:17:16 PM
In C, how to convert a ASCII floating-point number to double ?
Question by Alphonsio 01/07/2021 at 07:17:02 PM
In C, how to convert a floating-point number in a string to double ?
Question by Alphonsio 01/07/2021 at 07:16:52 PM
In C, how to convert a string to double ?
Question by Alphonsio 01/07/2021 at 07:16:37 PM
In C, how to convert a char* to double ?
Answer by Alphonsio 01/07/2021 at 07:15:46 PM

In C / C++, atof() is a dedicated function for converting strings to floats (or doubles). First, include stdlib.h:

#include <stdlib.h>

Then, simply convert strings to floats (or doubles) :

char string[] = "10.66814";
float value = atof(string);

Test the previous code online on OneCompiler.


If the first sequence of non-whitespace characters in string does not form a valid floating-point number, the function atof() returns 0.0.

Answer by Alphonsio 01/07/2021 at 07:11:41 PM

In C / C++, there is a dedicated function for converting strings to doubles : atof(). First, you'll need to include stdlib.h :

#include <stdlib.h>

Then, simply convert string to double (or float) :

char string[] = "10.66814";
double value = atof(string);

Test the previous code online on OneCompiler.


If the first sequence of non-whitespace characters in string does not form a valid floating-point number, the function atof() returns 0.0.

Question by Alphonsio 01/07/2021 at 07:09:40 PM
In C, how to convert a ASCII floating-point number to float ?
Question by Alphonsio 01/07/2021 at 07:09:20 PM
In C, how to convert a floating-point number in a string to float ?
Question by Alphonsio 01/07/2021 at 07:08:19 PM
In C, how to convert a string to float ?
Answer by Alphonsio 01/07/2021 at 07:08:08 PM

In C / C++, there is a dedicated function for converting strings to floats : atof(). First, you'll need to include stdlib.h :

#include <stdlib.h>

Then, simply convert string to float :

char string[] = "10.66814";
float value = atof(string);

Test the previous code online on OneCompiler.


If the first sequence of non-whitespace characters in string does not form a valid floating-point number, the function atof() returns 0.0.

Question by Alphonsio 01/07/2021 at 06:56:26 PM
In C, how to convert a char* to float ?
# ID Query URL Count

Icons proudly provided by Friconix.