Skip to content
Related Articles

Related Articles

Improve Article

sprintf() in C

  • Difficulty Level : Easy
  • Last Updated : 02 Nov, 2020


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


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 stands for “String print”. Instead of printing on console, it store output on char buffer which are specified in sprintf.


// 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;
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.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up
Recommended Articles
Page :