Interviews Questions, Algorithms, Aptitude, C Interview Program, C Theory Question, Aptitude Tricks, Test Series,

Thursday 28 March 2019

C to convert a number to a string

Question:

What is the best way in C to convert a number to a string?

Solution: Use sprintf() function.


C program:

#include<stdio.h>
int main()
{
    char result[50];

    float num = 23.34;

    sprintf(result, "%f", num);

printf("\n The string for the num is %s",
result);

}

Output:

 The string for the num is 23.340000

0 comments:

Post a Comment