Programma C per convertire qualsiasi numero in stringa usando la funzione sprintf

Programma C per convertire qualsiasi numero in stringa usando la funzione sprintf
  • Scrivi un programma in C per convertire qualsiasi numero in stringa usando la funzione sprintf.
  • Come convertire qualsiasi numero in una stringa in una riga.
Conoscenze richieste :funzione sprintf

È il prototipo della funzione della funzione sprintf:

int sprintf(char *str, const char *format, ...);
La funzione sprintf è simile alla funzione printf ma invece di stampare i dati formattati sullo schermo, li memorizza nella stringa del buffer indicata da str.

Programma C per convertire qualsiasi numero in stringa utilizzando la funzione sprintf in una riga

#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;
}
Uscita
The string of integer 100 is 100
The string of float 23.340000 is 23.340000