Use snprintf from <stdio.h> to build formatted text in a fixed-size buffer without overflowing it: you pass the buffer size, and the function never writes more than that many bytes, including the terminating '\0'. It returns the length the full result would have had, so you can detect truncation. This is the safe replacement for sprintf, which has no size limit and is a classic source of buffer overflows. If you search for "snprintf in c", this guide covers the call, its return value and the common mistakes.
You should already be comfortable with printf format specifiers and char arrays.
sprintf(buf, "%s", input) writes as many bytes as the formatted result needs, whatever the size of buf. If input is longer than the buffer, the write runs past the end and corrupts adjacent memory. Many compilers and style guides flag sprintf for this reason, and some platforms mark it deprecated. snprintf takes a size and stops at it.
The buffer holds 7 characters plus the terminator. snprintf always writes the terminator when the size is greater than zero, so the result is a valid C string even though it was cut off.
s is the destination buffer, n its total size in bytes.n - 1 characters, then a '\0'.n had been big enough, not counting the terminator. A negative return value means an encoding error.That return value is how you detect truncation:
Note the comparison needed >= sizeof name: if the return value equals the buffer size, there was no room for the terminator, so the text was truncated.
Calling snprintf with a NULL buffer and size 0 returns the required length without writing anything. You can use this to allocate exactly the right amount:
The caller owns the returned memory and must call free. On POSIX systems asprintf does this in one call, but it is not standard C.
A frequent bug is using snprintf repeatedly to append and adding the return value blindly. If an earlier call truncated, the return value can exceed the remaining space and your offset runs past the buffer. Clamp the offset:
The loop condition and the clamp after it keep used inside the buffer even if the text did not fit.
| Mistake | Problem | Fix |
|---|---|---|
snprintf(buf, sizeof(buf), ...) where buf is a char * parameter | sizeof gives the pointer size (8), not the array size | Pass the length as a separate argument |
Passing user text as the format: snprintf(buf, n, input) | Format string attack (%n, %s reading garbage) | Use snprintf(buf, n, "%s", input) |
| Ignoring the return value | Silent truncation | Compare with the buffer size |
Assuming n - 1 bytes of content | Off by one when the result fits exactly | Use needed >= size as the truncation test |
Older Microsoft runtimes before Visual Studio 2015 had a non-conforming _snprintf that did not always write the terminator. Modern MSVC provides a conforming snprintf.
Yes, as long as the size argument is greater than zero. With a size of 0 it writes nothing at all.
strncpy copies a string and does not terminate it when the source is too long. snprintf(dst, n, "%s", src) always terminates and reports truncation through its return value.
Yes: snprintf(buf, sizeof buf, "%d", value). A 32-bit int needs at most 11 characters plus the terminator, so a 12-byte buffer is enough.
It takes a va_list instead of ..., which lets you write your own logging or formatting wrapper around it.
Always give snprintf the real buffer size, never pass untrusted text as the format string, and compare its return value with the buffer size to detect truncation. Use a first call with NULL and 0 when you need to allocate the exact length.