sprintf() function in c – sprintf C Library Function

Sprintf() function in c: The function int sprintf(char *str, const char *format, …); writes a formatted data to a string. The sprintf function is similar to printf function function but instead of printing formatted data on screen, it stores it in the buffer string pointed by str.

If format contains format specifiers (subsequences beginning with %), the additional arguments following format are inserted after formatting in the resulting string by replacing their respective format specifiers.

Function prototype of sprintf

int sprintf(char *str, const char *format, ...);
  • str : This is the pointer to an character array where the formatted string to be stored.
  • format : This is a null-terminated string containing the text to be written on string. It may contains some embedded format specifiers.
  • additional arguments : These arguments will substitute the value of format specifiers in output string.

Format is a null terminated string that contains the string to be written in buffer, it may contains some embedded format specifiers.

The syntax of the format specifier is as follows:
%[flags][width][.precision][length]specifier

Below is the description of each component of format specifier.

Flags

Flags Description
+ Forces to preceed the result with a plus or minus sign (- or +) even for positive numbers. By default, only negative numbers are preceded with a ‘-‘ sign.
Left justify within the given field width. Right justification is the default (see width sub-specifier).
0 Left padding the numbers with zeroes instead of spaces when padding is specified.
# Used with o, x or X specifiers the value to preceeded with 0, 0x or 0X respectively for values other than zero. Used with A, E, F or G it forces the written output to contain a decimal point even if no more digits follow. By default, if no digits follow, no decimal point is written.

Width Specifier

Width Description
* The width is specified as an additional integer value argument preceding the argument that has to be formatted.
(number) Minimum number of characters to be printed on console. If the value to be printed is shorter than this number, the result is padded with blank spaces. The value is result is larger than number it is not truncated.

Precision Specifier

Precision Description
.* The precision is specified as an additional integer value argument preceding the argument that has to be formatted.
.number For integer specifiers precision specifies the minimum number of digits to be displayed. If the value is shorter than number, the result is padded with leading zeros(0). If the value is longer than number the result will not get truncated. A precision of 0 means that no character is written for the value 0. For a, A, e, E, f and F specifiers: this is the number of digits to be printed after the decimal point (this is 6, by default). For g and G specifiers: This is the maximum number of significant digits to be printed. For s: this is the maximum number of characters to be printed. By default all characters gets printed till null character(‘\0’) is encountered. If the period is specified without an explicit value for precision, 0 is assumed by default.

Length Specifier

Length Specifier Description
l The is interpreted as unsigned long int or long int for integer specifiers (i, d, o, u, x and X), and as a wide character or wide character string for specifiers c and s.
L The is interpreted as a long double (only valid for floating point specifiers: e, E, f, g and G).
h The argument is a short int or unsigned short int (only applies to integer specifiers: i, d, o, u, x and X).

Specifier Character

Specifier character is the most significant component, since it defines the data type and it’s interpretation.

Specifier Character Output
i or d Signed decimal integer(653, -236).
u Unsigned decimal integer(7653).
c Character (‘T’).
f Decimal floating point, lowercase (786.425).
F Decimal floating point, uppercase (786.425).
s String of characters (For Example : “fprintf”).
x Unsigned hexadecimal integer (3ab2).
X Unsigned hexadecimal integer (uppercase) (3AB2).
e Scientific notation (mantissa/exponent), lowercase (7.8634e+3).
E Scientific notation (mantissa/exponent), uppercase (7.8634E+3).
g Use the shortest representation: %e or %f (786.43).
G Use the shortest representation: %E or %F (786.43).
o Unsigned octal (425).
p Pointer address (For Example : b6111111).
% A % Character (%).
n Nothing printed.

Return value of sprintf

C programming sprintf: On success, the total number of characters written(excluding null terminating character) is returned otherwise a negative number is returned in case of an error.

C program using sprintf function

The following program shows the use of sprintf function to write formatted data in a string.

sprintf C Library Function

#include <stdio.h>
 
int main (){
   int c, counter = 0;
   FILE *file;
   char *string = "fprintf C Standard Library function";
    
   /* Create a temporary file */
   file = fopen("textFile.txt", "w");
   fprintf(file, "%s", string);
   fclose(file);
    
   /* Opening textFile.txt and printing it's content */
   file = fopen("textFile.txt", "r");
   while(!feof(file)){
       c = fgetc(file);
       printf("%c", c);
   }
    
   return(0);
}

Output

1234321 is a palindrome number