sprintf() in C

Syntax:

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

sprintf stands for “String print”. Instead of printing on console, it store output on char buffer which are specified in sprintf

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Example program to demonstrate sprintf()
#include<stdio.h>
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;
}

chevron_right


Output:

Sum of 10 and 20 is 30

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


3


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.