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

snprintf

Prev: fprintf | Up: stdio.h | Next: vsnprintf
Rel: stdio.h, stdio.h & File I/O Guide
Ref: cppreference — snprintf
Tags: #public #prog #c #stdlib #stdio #tier/core


Include

#include <stdio.h>

Signature & Overview

int snprintf(char *restrict buf, size_t size, const char *restrict fmt, ...);

Bounds-checked formatted write into buf (at most size - 1 chars plus NUL terminator).

Notes & Pitfalls

Example

#include <assert.h>
#include <stdio.h>
#include <string.h>

int main(void) {
    char buf[16];
    int n = snprintf(buf, sizeof(buf), "id=%d", 42);
    assert(n > 0 && (size_t)n < sizeof(buf) && strcmp(buf, "id=42") == 0);
}

Linked from

Related notes