Open In App

Convert a Char Array to Double in C

Last Updated : 21 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  1. atof() Function
  2. strtod() Function
  3. 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()
{
    // Define a character array chr containing the string
    // "3.14"
    char chr[] = "3.14";
  
    // Convert the string to a double and store it in the
    // variable num using the atof function
    double num = atof(chr);
  
    // Print the converted double value using printf
    // function
    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()
{
    // Define a character array chr containing the string
    // "3.14"
    char chr[] = "3.14";
  
    // Define a character pointer endptr to be used with the
    // strtod function
    char* endptr;
  
    // Convert the string to a double and store it in the
    // variable num using the strtod function
    double num = strtod(chr, &endptr);
  
    // Check if conversion was successful by comparing chr
    // and endptr
    if (chr == endptr) {
  
        // If chr and endptr are equal, conversion was not
        // successful and print an error message
        printf("Failed to convert to double\n");
    }
    else {
  
        // If chr and endptr are not equal, conversion was
        // successful and print the converted double value
        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()
{
    // declare a character array and initialize it with a
    // string
    char chr[] = "3.14";
  
    // declare a variable to hold the converted double value
    double num;
  
    // use sscanf() to convert the string in chr to a double
    // value
    sscanf(chr, "%lf", &num);
  
    printf("%lf", num);
    return 0;
}


Output

3.140000

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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads