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.
Similar Reads
How to Convert a String to a Char Array in C?
In C, the only difference between the string and a character array is possibly the null character '\0' but strings can also be declared as character pointer in which case, its characters are immutable. In this article, we will learn how to convert a string to a char array in C. The most straightforw
2 min read
How to convert given number to a character array
Given an integer number N, the task is to convert it into a character array. Example: Input: N = 2020 Output: {2, 0, 2, 0} Explanation: Here char array arr[] = {2, 0, 2, 0}Input: N = 12349 Output: {1, 2, 3, 4, 9} Explanation: Here char array arr[] = {1, 2, 3, 4, 9} Approach 1: The basic approach to
14 min read
Convert Binary to Decimal in C
In this article, we will learn how to write a C program to convert the given binary number into an equivalent decimal number. Binary numbers are expressed in base 2 ( 0, 1 ) and decimal numbers are expressed in base 10 ( 0-9 ). Algorithm to Convert Binary Numbers to DecimalThe idea is to extract the
3 min read
Convert Decimal to Binary in C
In this article, we will learn to write a C program to convert a decimal number into a binary number. The decimal number system uses ten digits from 0 to 9 to represent numbers and the binary number system is a base-2 number system that uses only 0 and 1 to represent numbers. Algorithm to Convert De
2 min read
size of char datatype and char array in C
Given a char variable and a char array, the task is to write a program to find the size of this char variable and char array in C. Examples: Input: ch = 'G', arr[] = {'G', 'F', 'G'} Output: Size of char datatype is: 1 byte Size of char array is: 3 byte Input: ch = 'G', arr[] = {'G', 'F'} Output: Siz
2 min read
Find the Length of Character Array in C
In C, a character array is a collection of elements of the âcharâ data type that are placed in contiguous memory locations and are often used to store strings. The length of a character array is defined as the total number of characters present in the array, excluding the null character ('\0') at th
2 min read
How to Create a Dynamic Array of Strings in C?
In C, dynamic arrays are essential for handling data structures whose size changes dynamically during the program's runtime. Strings are arrays of characters terminated by the null character '\0'. A dynamic array of strings will ensure to change it's size dynamically during the runtime of the progra
3 min read
How to Create an Array of Structs in C?
In C, a structure is a user-defined data type that can be used to group items of different types into a single entity while an array is a collection of similar data elements. In this article, we will learn how to create an array of structs in C. Creating an Array of Structs in CTo create an array of
2 min read
C Program For Char to Int Conversion
Write a C program to convert the given numeric character to integer. Example: Input: '3'Output: 3Explanation: The character '3' is converted to the integer 3. Input: '9'Output: 9Explanation: The character '9' is converted to the integer 9. Different Methods to Convert the char to int in CThere are 3
3 min read
C Program For Int to Char Conversion
To convert the int to char in C language, we will use the following 2 approaches: Using typecastingUsing sprintf() Example: Input: N = 65 Output: A1. Using TypecastingMethod 1:Declaration and initialization: To begin, we will declare and initialize our integer with the value to be converted.Typecast
2 min read