C Reference ยท stdio.h
C sprintf() and snprintf() functions
Reference for C sprintf() and snprintf(): format text into a char array, why snprintf prevents buffer overflows, return values and truncation.
sprintf() and snprintf() work like printf(), but instead of printing, they store the formatted text in a character array and add the terminating '\0'. They are the standard way to build a string from numbers and other strings.
Prefer snprintf(). It takes the buffer size and never writes past it, while sprintf() has no way of knowing how big the buffer is and overflows it if the result is too long. That overflow is undefined behavior and a classic security hole.
#include <stdio.h> int sprintf(char *restrict s, const char *restrict format, ...); int snprintf(char *restrict s, size_t n, const char *restrict format, ...);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
int sprintf(char *restrict s, const char *restrict format, ...); int snprintf(char *restrict s, size_t n, const char *restrict format, ...);
| Name | Description |
|---|---|
s |
Destination buffer. With snprintf it may be a null pointer when n is 0. |
n |
Size of the buffer in bytes, including room for the terminating null character. |
format, ... |
Format string and arguments, as for printf. |
Returns: the number of characters that would have been written, not counting the terminating null, had the buffer been large enough (for sprintf that is simply the number written). A negative value signals an encoding error.
Errors / undefined behavior: sprintf overflowing the buffer is undefined behavior. Overlapping source and destination (for example printing a string into itself) is undefined. snprintf itself is always safe about the buffer, but format mismatches are still undefined.
Usage notes
- Truncation check. If the return value is
>= n, the output was cut off. Compare it with the buffer size when truncation would be a bug (file paths, commands, SQL). - Always terminated. For
n > 0,snprintfwrites at mostn - 1characters and then a'\0', even when it truncates. Withn == 0, nothing is written. This is the key difference fromstrncpy(). - Size measuring idiom.
snprintf(nullptr, 0, ...)tells you how large a buffer you need; allocate that plus one and call it again. - Appending. Keep a running offset and pass
buffer + usedwithsize - used, and stop if the result does not fit. Do not use the return value to advance beyond the buffer when it was truncated. - Never print a string into itself.
snprintf(buf, sizeof buf, "%s!", buf)has undefined behavior because the argument overlaps the destination. - Avoid
sprintf. Even when you "know" the buffer is big enough, a later change to the format or the data can silently break that assumption. Compilers and linters (-Wformat-overflow,-Wformat-truncation) catch obvious cases. - Number to string conversion.
snprintf(buf, sizeof buf, "%d", n)is the portable way; for the reverse direction usestrtol()orsscanf(). - Variants.
vsnprintftakes ava_listand is used to write your own printf-like functions;asprintf(GNU/BSD, not ISO C) allocates the buffer for you. - Locale. Decimal separators follow the current locale just like in
printf.