snprintf() Function
The snprintf()
function in C stdio.h writes formatted output to a fixed-size buffer. It behaves similarly to printf()
, except that the resulting string is stored in a provided buffer rather than being printed to standard output. This function ensures that the output does not exceed the specified buffer capacity, automatically appending a null terminator at the end.
Syntax of snprintf()
int snprintf(char *s, size_t n, const char *format, ...);
Parameters
Parameter | Description |
---|---|
s | Pointer to the buffer where the resulting C-string is stored. The buffer must be large enough to hold at least n characters. |
n | The maximum number of bytes to be used in the buffer. The resulting string will have a maximum length of n-1 characters to leave room for the terminating null character. |
format | A C string containing the format specifiers, similar to those used in printf() . |
... | Additional arguments corresponding to the format specifiers in the format string. |
If the formatted string exceeds the buffer size (n-1 characters), the extra characters are discarded, but the function returns the total number of characters that would have been written if enough space were available. This behavior allows you to detect truncation.
Return Value
The function returns the number of characters that would have been written if the buffer were sufficiently large, not including the terminating null character. If an encoding error occurs, a negative number is returned.
Examples for snprintf()
Example 1: Basic Usage of snprintf() for Formatted Output
This example demonstrates how to use snprintf()
to format a string with an integer value and store the result in a buffer.
Program
#include <stdio.h>
int main() {
char buffer[50];
int value = 42;
snprintf(buffer, sizeof(buffer), "The answer is %d.", value);
printf("%s\n", buffer);
return 0;
}
Explanation:
- A buffer of 50 characters is declared to store the formatted string.
- An integer variable is initialized with the value 42.
snprintf()
formats the string using the provided integer and writes it to the buffer.- The formatted string is then printed using
printf()
.
Program Output:
The answer is 42.
Example 2: Handling Buffer Truncation with snprintf()
This example illustrates how snprintf()
handles cases where the formatted string exceeds the size of the buffer.
Program
#include <stdio.h>
int main() {
char buffer[10];
int ret = snprintf(buffer, sizeof(buffer), "Hello, World!");
printf("Formatted string: %s\n", buffer);
printf("Return value: %d\n", ret);
return 0;
}
Explanation:
- A buffer of 10 characters is declared.
snprintf()
attempts to write the string “Hello, World!” into the buffer.- Since the buffer can hold only 9 characters plus the null terminator, the output is truncated.
- The function returns the total number of characters that would have been written (which is greater than the buffer size), allowing detection of the truncation.
Program Output:
Formatted string: Hello, Wo
Return value: 13
Example 3: Using snprintf() with Multiple Format Specifiers
This example demonstrates the use of snprintf()
with several format specifiers to compose a detailed formatted string.
Program
#include <stdio.h>
int main() {
char buffer[100];
int age = 30;
const char *name = "Alice";
double height = 5.7;
snprintf(buffer, sizeof(buffer), "%s is %d years old and %.1f feet tall.", name, age, height);
printf("%s\n", buffer);
return 0;
}
Explanation:
- A buffer of 100 characters is allocated to hold the resulting string.
- Multiple variables of different types are declared and initialized.
snprintf()
is used with a format string that includes string, integer, and floating-point specifiers.- The fully formatted string is stored in the buffer and then printed to the console.
Program Output:
Alice is 30 years old and 5.7 feet tall.