sprintf function : sends formatted output to String



Sprintf function :  sends formatted output to String


Features :

  1. Output is Written into String instead of Displaying it on the Output Devices .
  2. Return value is integer ( i.e Number of characters actually placed in array / length of string )
  3. String is terminated by ‘\0’
  4. Main Per pose : Sending Formatted output to String
  5. Header File : Stdio.h

Syntax :

int sprintf(char *buf,char format,arg_list);

Live Example :

int age = 23 ;
char str[100];
sprintf( str , "My age is %d",age);
puts(str);

Output of Above Program :

My age is 23 

Analysis of Source Code : – Just keep in mind that

  • Assume that we are using printf then we get output “My age is 23”
  • What does printf does ? —– Just Print the Result on the Screen
  • Similarly Sprintf stores result “My age is 23” into string str instead of printing it.

Sprintf Function in C