You are here

sprintf in c

Sprintf in C : It's just like printf but output is sent to a string, rather than standard output.
It's used when we wish to store integers or floats in a string. The first argument is a pointer to string. Previous content of string are overwritten.

C programming code

  1. #include<stdio.h>
  2.  
  3. int main()
  4. {
  5.   int a = 1;
  6.   float b = 1.23;
  7.   char c = 'u', string[100] = "My initial character array.";
  8.        
  9.   printf("%s\n", string);
  10.        
  11.   sprintf(string, "Integer = %d, Float = %f, Character = %c.", a, b, c);
  12.        
  13.   printf("%s\n", string);
  14.        
  15.   return 0;
  16. }