sprintf() v C

Syntax:

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

Návrat:

If successful, it returns the total number of characters written excluding null-character appended in the string, in case of failure a negative number is returned . 

sprintf je skratka pre String print. Namiesto tlače na konzole ukladá výstup do vyrovnávacej pamäte znakov, ktoré sú špecifikované v sprintf.

C




// Example program to demonstrate sprintf()> #include> int> main()> {> > char> buffer[50];> > int> a = 10, b = 20, c;> > c = a + b;> > sprintf> (buffer,> 'Sum of %d and %d is %d'> , a, b, c);> > // The string 'sum of 10 and 20 is 30' is stored> > // into buffer instead of printing on stdout> > printf> (> '%s'> , buffer);> > return> 0;> }>

Výkon

Sum of 10 and 20 is 30 

Časová zložitosť: O(n) , kde n je počet prvkov uložených vo vyrovnávacej pamäti.
Pomocný priestor: O(n) , kde n je počet prvkov uložených vo vyrovnávacej pamäti.