Convert a Char Array to Double in C
Last Updated :
21 May, 2023
Converting a char array to a double is a common operation in C programming. It involves taking a string of characters that represent a numerical value and converting it to a double-precision floating-point value. This can be done by using various approaches listed below:
Convert char array into double
Methods to convert the char array into double are mentioned below:
- atof() Function
- strtod() Function
- sscanf() Function
1. Using atof() function
atof() is a standard library function in C that converts a string of characters representing a floating-point number to its equivalent double-precision floating-point number. The atof() function is included in the stdlib.h library and takes a string as its argument. It then returns the floating-point value (double) equivalent of the string.
Syntax:
double atof(const char *str);
Where str is the string representation of the floating-point number to be converted.
Example 1: Converting char “3.14” to a double
In the given code, a character array chr[] is initialized with the string “3.14”. Then, the atof() function is called with chr as its argument and the resulting double value is stored in the num variable. Finally, the converted value is printed using the printf() function.
C
#include <stdio.h>
#include <stdlib.h>
int main()
{
char chr[] = "3.14" ;
double num = atof (chr);
printf ( "The converted double is: %f" , num);
return 0;
}
|
Output
The converted double is: 3.140000
2. Using strtod() function
strtod() is a standard C library function that converts a string to a double-precision floating-point number. It takes two arguments: the first argument is the string to convert, and the second argument is a pointer to a character string that points to the first character of a string of characters. This pointer is used to store the remaining characters after the conversion. The strtod() function provides greater precision and can handle a wider range of values compared to atof(). strtod() can handle long double values.
Syntax:
double strtod(const char *str, char **endptr);
Here, str is the string to be converted to a double-precision floating-point number, and endptr is a pointer to a character pointer. endptr is used to store the pointer to the first character after the numeric value in the string. If str does not contain a valid floating-point number, then endptr is set to str. If the string represents a valid floating-point number, then endptr points to the first character following the floating-point number.
Example 1:
In this example, we define a character array chr[] that contains the string “3.14” and a character pointer endptr that will be used to store the pointer to the first character after the numeric value in the string. We then call strtod() with chr and endptr as arguments to convert the string to a double. If the conversion is successful, we print the converted double value. If the conversion fails, it will print an error message.
C
#include <stdio.h>
#include <stdlib.h>
int main()
{
char chr[] = "3.14" ;
char * endptr;
double num = strtod (chr, &endptr);
if (chr == endptr) {
printf ( "Failed to convert to double\n" );
}
else {
printf ( "The converted double is: %f\n" , num);
}
return 0;
}
|
Output
The converted double is: 3.140000
3. Using sscanf() function
The function sscanf() is a standard library function that resembles scanf(), but it takes a string buffer as its input source.
Syntax:
int sscanf(const char *str, const char *format, ...);
This function takes two arguments:
str: This is a pointer to the string to be read.
Format: This is a string specifying the format of the input to be read.
Example:
C
#include <stdio.h> // header file for input and output functions
int main()
{
char chr[] = "3.14" ;
double num;
sscanf (chr, "%lf" , &num);
printf ( "%lf" , num);
return 0;
}
|
Above are the methods to convert a char array to Double using built-in functions in C. We can choose any of the above methods.
Share your thoughts in the comments
Please Login to comment...