sprintf() Function
The sprintf()
function in C stdio.h writes formatted data into a string buffer. It behaves similarly to printf()
by formatting the data as specified, but instead of printing the result to the console, it stores the output in a character array.
Syntax of sprintf()
int sprintf(char *str, const char *format, ...);
Parameters
Parameter | Description |
---|---|
str | Pointer to a buffer where the resulting C-string is stored. The buffer must be large enough to hold the formatted string. |
format | A C string that contains a format string following the same specifications as in printf() . |
... | Additional arguments that provide data to be formatted, corresponding to the format specifiers in the format string. |
Additional points: The sprintf()
function automatically appends a terminating null character at the end of the formatted string. It is crucial to ensure that the buffer provided is large enough to accommodate the entire resulting string; otherwise, it may lead to a buffer overflow. For safer formatting, consider using snprintf()
which allows specifying the buffer size.
Return Value
On success, sprintf()
returns the total number of characters written (excluding the null character). On failure, a negative number is returned.
Examples for sprintf()
Example 1: Formatting and Writing a Simple Greeting
This example demonstrates how to use sprintf()
to format and store a simple greeting message in a buffer.
Program
#include <stdio.h>
int main() {
char buffer[50];
int result;
result = sprintf(buffer, "Hello, %s!", "World");
printf("Formatted string: %s\n", buffer);
printf("Number of characters written: %d\n", result);
return 0;
}
Explanation:
- A buffer named
buffer
is declared with sufficient size. sprintf()
formats the string using the format “Hello, %s!” and the provided argument “World”, then writes it intobuffer
.- The formatted string and the number of characters written (excluding the null terminator) are printed.
Program Output:
Formatted string: Hello, World!
Number of characters written: 13
Example 2: Formatting Multiple Data Types Together
This example shows how sprintf()
can format and combine different data types into one cohesive string.
Program
#include <stdio.h>
int main() {
char buffer[100];
int age = 25;
double score = 89.5;
sprintf(buffer, "Age: %d, Score: %.1f", age, score);
printf("Formatted string: %s\n", buffer);
return 0;
}
Explanation:
- Variables of different types (
int
anddouble
) are declared. sprintf()
formats these variables into a single string with appropriate format specifiers.- The resulting formatted string is stored in
buffer
and then printed.
Program Output:
Formatted string: Age: 25, Score: 89.5
Example 3: Creating a Detailed Report with Multiple Lines
This example illustrates how to use sprintf()
to build a detailed, multi-line report string by formatting various data elements.
Program
#include <stdio.h>
int main() {
char report[150];
int id = 101;
char name[] = "Alice";
double balance = 2450.75;
sprintf(report, "ID: %d\nName: %s\nBalance: $%.2f", id, name, balance);
printf("Report:\n%s\n", report);
return 0;
}
Explanation:
- A report buffer is declared to store the multi-line formatted string.
- Different data elements including an integer, a string, and a double are formatted using
sprintf()
with appropriate format specifiers. - The resulting detailed report is printed, displaying the formatted multi-line information.
Program Output:
Report:
ID: 101
Name: Alice
Balance: $2450.75