Open In App

C Program For Double to String Conversion

Last Updated : 01 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

To convert double to string in C language, we will use the sprintf function as follows:

Input

n = 456321.7651234 

Output: 

string: 456321.7651234  

Method: Using sprintf

By specifying the precision in sprintf, we can convert double to string or character array with custom precision. We can use sprintf to add extra text (as required) to the string at the same time.

C




// C Program to demonstrate
// Double to String Conversion
#include <stdio.h>
  
int main()
{
    double n = 456321.7651234;
    char str[100];
    sprintf(str, "%f", n);
    printf("the string is: %s\n", str);
    return 0;
}


Output

the string is: 456321.765123

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads