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

vsnprintf

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


Include

#include <stdio.h>

Signature & Overview

int vsnprintf(char *restrict buf, size_t size, const char *restrict fmt, va_list ap);

Variadic va_list version of snprintf for building custom logging or formatting helpers.

Notes & Pitfalls

Example

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

static int fmt(char *dst, size_t cap, const char *f, ...) {
    va_list ap; va_start(ap, f);
    int n = vsnprintf(dst, cap, f, ap);
    va_end(ap); return n;
}
int main(void) {
    char b[16];
    assert(fmt(b, sizeof(b), "v=%d", 7) == 3 && strcmp(b, "v=7") == 0);
}

Linked from

Related notes