This function is same as printf() function but it sends the formatted output to a string instead of screen.
So we can convert a variable data type into string using sprintf() function.
/*Program to illustrate the use of sprintf() function*/
#include
int main()
{
//declare two strings
char s1[20];
char s2[20];
//declare int and float variables
int n=7890;
float f=2345.67;
//convert the variables into string
sprintf(s1,"%d",n);
sprintf(s2,"%0.1f",f);
//print the converted strings
printf("s1=%s,s2=%s\n",s1,s2);
}
0 comments: