On this page
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
- Pairs with
<stdarg.h>: Ideal for customlog_fmt(const char *fmt, ...)helpers.
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
- tuts/c/stdlib/stdio/fgets Linked here; links to this page; shared tags: #c, #prog, #stdio, #stdlib, #tier
- tuts/c/stdlib/stdio/snprintf 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/stdarg/va_list links to this page; shared tags: #c, #prog, #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/fopen shared tags: #c, #prog, #stdio, #stdlib, #tier