C Language: sprintf function(Formatted String Write)

In the C Programming Language, the sprintf function writes formatted output to an object pointed to by s.


The syntax for the sprintf function in the C Language is:

int sprintf(char *s, const char *format, ...);

Parameters or Arguments


An array where the output will be written.


Describes the output as well as provides a placeholder to insert the formatted string. Here are a few examples:

Format Explanation Example
%d Display an integer 10
%f Displays a floating-point number in fixed decimal format 10.500000
%.1f Displays a floating-point number with 1 digit after the decimal 10.5
%e Display a floating-point number in exponential (scientific notation) 1.050000e+01
%g Display a floating-point number in either fixed decimal or exponential format depending on the size of the number (will not display trailing zeros) 10.5


The sprintf function returns the number of characters stored in the array (not including the null character).

Required Header

In the C Language, the required header for the sprintf function is:

#include <stdio.h>

Applies To

In the C Language, the sprintf function can be used in the following versions:

  • ANSI/ISO 9899-1990

stdio.h Functions