sprintf(3)

πŸ’– sprintf(3) β€” string print formatted πŸ’–

okay bestie, sit down, hydrate, and let me tell you about sprintf and vsprintf β€” two functions from the Library Functions Manual that are giving iconic but slightly feral energy. ✨ They live in the standard C library, ngl.


🌈 LIBRARY 🌈

Standard C library (libc, -lc) 🫢


πŸ“œ SYNOPSIS πŸ“œ

#include <stdio.h>

int sprintf(char *restrict str,
            const char *restrict format, ...);
int vsprintf(char *restrict str,
             const char *restrict format, va_list ap);

πŸŽ€ Feature Test Macro Requirements for glibc (see feature_test_macros(7)): πŸŽ€

snprintf(), vsnprintf():

_XOPEN_SOURCE >= 500 || _ISOC99_SOURCE
    || /* glibc <= 2.19: */ _BSD_SOURCE

(yes, the macro list really does say snprintf() and vsnprintf() β€” I'm just the messenger, bestie πŸ’…)


🌸 DESCRIPTION 🌸

So these two? They're literally equivalent to snprintf(3) and vsnprintf(3)… except there's no check for buffer overrun performed. None. Zero. Not a vibe check in sight. 😭 (peep BUGS below, it's important, iykyk).


πŸ‘‘ ATTRIBUTES πŸ‘‘

For an explanation of the terms used in this section, see attributes(7).

  • sprintf(), vsprintf() β€” Thread safety: MT-Safe locale

⭐ STANDARDS ⭐

C11, POSIX.1-2008. queen behavior, fr fr πŸ’…


πŸ¦‹ HISTORY πŸ¦‹

C89, POSIX.1-2001. she's been around, she's seen things 🌸


πŸ’€ BUGS πŸ’€

See printf(3).

πŸ™ˆ sprintf(), vsprintf()

Because sprintf() and vsprintf() assume an arbitrarily long string, callers must be careful not to overflow the actual space β€” and honestly? this is often impossible to assure. 🫠

Also note that the length of the strings produced is locale-dependent and difficult to predict. so like. unpredictable AND dangerous. not the duo we wanted. 😭

Use snprintf() and vsnprintf() instead (or asprintf(3) and vasprintf(3)). periodt. protect your buffers, protect your peace. 🫢

🌈 SEE ALSO 🌈


mwah πŸ’– that's the whole page, bestie β€” brought to you by Linux man-pages 6.20, dated 2025-12-07. go forth and not overflow your buffers βœ¨πŸŽ€πŸ¦‹

πŸ“¦ where this page comes from
  • arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • πŸ“œ the page's license: GPL-2.0-or-later
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms