The sprintf() Function in C

The sprintf() works just like printf() but instead of sending output to console it returns the formatted string.

Syntax: int sprintf(char *str, const char *control_string, [ arg_1, arg_2, ... ]);

The first argument to sprintf() function is a pointer to the target string. The rest of the arguments are the same as for printf() function.

The function writes the data in the string pointed to by str and returns the number of characters written to str, excluding the null character. The return value is generally discarded. If an error occurs during the operation it returns -1.

The following program demonstrates how to use sprintf() function.

Expected Output:

Another important use of sprintf() function is to convert integer and float values to strings.

Expected Output:

1 thought on “The sprintf() Function in C

Leave a Comment