On this page
  1. sprintf
  2. Include
  3. Signature & Overview
  4. Notes & Pitfalls
  5. Example

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

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