C program to convert any number to string using sprintf function

  • Write a program in C to convert any number to string using sprintf function.
  • How to convert any number to a string in one line.

Required knowledge: sprintf function
Her eis the function prototype od sprintf function:

int sprintf(char *str, const char *format, ...);

The sprintf function is similar to printf function but instead of printing formatted data on screen, it stores it in the buffer string pointed by str.

C program to convert any number to string using sprintf function in one line

C program to convert any number to string using sprintf function

#include<stdio.h>
 
int main() {
    char string[100];
    int i = 100;
    float f = 23.34;
     
    /* Convert integer to string using sprintf function */
    sprintf(string, "%i", i);
    printf("The string of integer %d is %s\n", i, string);
     
    /* Convert float to string using sprintf function */
    sprintf(string, "%f", f);
    printf("The string of float %f is %s", f, string);
     
    return 0;
}

Output

The string of integer 100 is 100
The string of float 23.340000 is 23.340000