Pointer to the destination buffer where the formatted string will be stored.
2nd Parameter Type : const char *
Pointer to read-only string
2nd Parameter
A null-terminated format string specifying how to format the output, containing format specifiers like `%d`, `%s`, or `%f`.
3rd Parameter Type : ...
Represents a variable number of arguments in a function. Used in functions like `printf` or `fprintf` to accept additional parameters beyond those explicitly declared.
3rd Parameter
A variable number of arguments to format and store in the destination buffer.
Read more about parameters of sprintf in parameters section
The sprintffunction in C language Writes formatted output to a string.
The sprintf function writes formatted output to a string. It formats and stores a series of characters and values in the buffer pointed to by str. The format argument is a string containing literal text and format specifiers that describe how subsequent arguments are converted for output.
The sprintffunction takes 3
parameters:
•
char * `str`: Pointer to the destination buffer where the formatted string will be stored.
•
const char * `format`: A null-terminated format string specifying how to format the output, containing format specifiers like `%d`, `%s`, or `%f`.
•
... `args`: A variable number of arguments to format and store in the destination buffer.
Writes formatted output to the buffer `str` based on the `format` string and additional arguments. The resulting string is null-terminated. Returns the number of characters written, excluding the null terminator. The caller must ensure the buffer is large enough to store the output, as no bounds checking is performed.
The sprintf function return value :
Returns the number of characters written if successful or a negative value if an error occurred
Output
This example demonstrates basic usage of sprintf to format a string with an integer and a float.