Sai A Sai A
Updated date Feb 19, 2024
In this blog, we will learn how to convert double numbers to strings in C programming language using different methods like sprintf(), snprintf(), gcvt(), and custom implementations.

Introduction:

In programming, there often arises a need to convert data from one type to another. One common conversion task is converting a double (a floating-point number) to a string in the C programming language. This process can be crucial, especially when dealing with input/output operations or when formatting data for display. In this blog, we will explore several methods to achieve this conversion.

Method 1: Using sprintf()

To convert a double to a string in C is by using the sprintf() function. This function allows us to format and store a string representation of a double into a character array.

#include <stdio.h>

int main() {
    double num = 3.14159;
    char str[20]; // Assuming enough space for the string
    
    sprintf(str, "%f", num);
    
    printf("String representation using sprintf(): %s\n", str);
    
    return 0;
}

Output:

String representation using sprintf(): 3.141590

In this method, we use sprintf() to format the double 'num' into a string stored in the character array 'str'. The "%f" format specifier tells sprintf() to format the double as a floating-point number.

Method 2: Using snprintf()

Similar to sprintf(), we can also use the snprintf() function to convert a double to a string. The difference is that snprintf() allows us to specify the maximum number of characters to write into the buffer, preventing buffer overflow.

#include <stdio.h>

int main() {
    double num = 3.14159;
    char str[20]; // Assuming enough space for the string
    
    snprintf(str, sizeof(str), "%f", num);
    
    printf("String representation using snprintf(): %s\n", str);
    
    return 0;
}

Output:

String representation using snprintf(): 3.141590

Here, snprintf() is used similarly to sprintf(), but with an additional argument specifying the size of the buffer 'str'. This prevents potential buffer overflow by limiting the number of characters written.

Method 3: Using gcvt()

Another method to convert a double to a string is by using the gcvt() function. gcvt() converts a double to a string in a specific format, allowing us to control the number of digits after the decimal point.

#include <stdio.h>
#include <stdlib.h>

int main() {
    double num = 3.14159;
    char str[20]; // Assuming enough space for the string
    
    gcvt(num, 6, str); // Convert 'num' with 6 digits after the decimal point
    
    printf("String representation using gcvt(): %s\n", str);
    
    return 0;
}

Output:

String representation using gcvt(): 3.141590

In this method, gcvt() converts the double 'num' into a string with six digits after the decimal point and stores it in the character array 'str'.

Method 4: Using Custom Implementation

Alternatively, we can implement our custom function to convert a double to a string. This approach allows us to have more control over the conversion process and tailor it to our specific requirements.

#include <stdio.h>

void doubleToString(double num, char* str, int precision) {
    sprintf(str, "%.*f", precision, num);
}

int main() {
    double num = 3.14159;
    char str[20]; // Assuming enough space for the string
    
    doubleToString(num, str, 4); // Convert 'num' with 4 digits after the decimal point
    
    printf("String representation using custom function: %s\n", str);
    
    return 0;
}

Output:

String representation using custom function: 3.1416

In this custom function, we use sprintf() with a precision specifier ("%.*f") to control the number of digits after the decimal point. This allows us to customize the conversion according to our needs.

Conclusion:

In this blog, we explored various methods to convert a double to a string in C. sprintf() and snprintf() are standard library functions that offer simplicity and flexibility, while gcvt() provides control over formatting. 

Comments (0)

There are no comments. Be the first to comment!!!