How to usesprintf in C Language

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

C Program For Double to String Conversion

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

Input

n = 456321.7651234 

Output: 

string: 456321.7651234  

Similar Reads

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....

Contact Us