On this page
sprintf
Prev: tmpfile | Up: stdio.h | Next: gets
Rel: snprintf(), stdio.h, stdio.h & File I/O Guide
Ref: cppreference — sprintf
Tags: #public #prog #c #stdlib #stdio #tier/legacy
Include
#include <stdio.h>
Signature & Overview
int sprintf(char *restrict buf, const char *restrict fmt, ...);
Unbounded formatted string write with no buffer-size check.
Legacy / Avoid: Prefer snprintf() in all new code.
Notes & Pitfalls
- Unbounded write (classic buffer overflow): Never use
sprintf()—always replace withsnprintf(buf, sizeof(buf), ...).
Example
#include <assert.h>
#include <stdio.h>
int main(void) {
char buf[32];
// Safe replacement for sprintf():
int n = snprintf(buf, sizeof(buf), "%s:%d", "host", 80);
assert(n == 7);
}
Linked from
Related notes
- tuts/c/stdlib/stdio/gets Linked here; links to this page; shared tags: #c, #prog, #stdio, #stdlib, #tier
- tuts/c/stdlib/stdio/tmpfile Linked here; links to this page; shared tags: #c, #prog, #stdio, #stdlib, #tier
- tuts/c/stdlib/stdio Linked here; links to this page; shared tags: #c, #prog, #stdio, #stdlib
- tuts/c/stdlib/stdio/snprintf Linked here; shared tags: #c, #prog, #stdio, #stdlib, #tier
- tuts/c/stdlib/stdio_and_file_io Linked here; shared tags: #c, #prog, #stdlib
- tuts/c/stdlib/stdio/fclose shared tags: #c, #prog, #stdio, #stdlib, #tier
- tuts/c/stdlib/stdio/feof shared tags: #c, #prog, #stdio, #stdlib, #tier
- tuts/c/stdlib/stdio/ferror shared tags: #c, #prog, #stdio, #stdlib, #tier
- tuts/c/stdlib/stdio/fflush shared tags: #c, #prog, #stdio, #stdlib, #tier
- tuts/c/stdlib/stdio/fgets shared tags: #c, #prog, #stdio, #stdlib, #tier