asprintf(3)
π asprintf β print to allocated string π
okay bestie, so like. this is asprintf(3) and it lives in the Library Functions Manual section of the Linux man-pages, which is the part of the library manuals that's not the syscalls, iykyk. it's giving standard C library energy (libc, -lc) and honestly? it ate. π
β¨ NAME β¨
asprintf, vasprintf β print to allocated string
okay so no cap: these two are the "print into a string that I didn't have to size myself" girlies. π
π¦ LIBRARY π¦
Standard C library β libc, link it with -lc obviously. πΈ
β¨ SYNOPSIS β¨
like, the whole vibe:
#define _GNU_SOURCE /* See feature_test_macros(7) */
#include <stdio.h>
int asprintf(char **restrict strp, const char *restrict fmt, ...);
int vasprintf(char **restrict strp, const char *restrict fmt,
va_list ap);
note that _GNU_SOURCE up top, bestie β you literally need it. it's a feature test macro thing, go read feature_test_macros(7) if you want the lore. π
π DESCRIPTION π
so asprintf() and vasprintf() are the analogs of sprintf(3) and vsprintf(3). same girls, different fit. π
the difference? they allocate a string large enough to hold the output β including the terminating null byte ('\0') β and they hand you back a pointer to it through the first argument.
and like, that pointer? bestie, you MUST pass it to free(3) to release the allocated storage when you're done. it's not gonna free itself, it's not that kind of situation. π«Ά
π RETURN VALUE π
- when it works: the functions return the number of bytes printed, literally just like
sprintf(3). slay. β¨ - on error:
-1is returned,errnogets set to tell you what went wrong, and the contents ofstrpare undefined.
β οΈ undefined means undefined, bestie. do not go looking in there. do not pass go. do not read strp. π
π ERRORS π
see sprintf(3) and malloc(3). that's where the error lore lives. π
π ATTRIBUTES π
for the explanation of the terms used in this section, see attributes(7).
- Interface:
asprintf(),vasprintf() - Attribute: Thread safety
- Value: MT-Safe locale
so yeah, they're thread safe, but only in a locale-y way. noted. π
π VERSIONS π
the FreeBSD implementation sets strp to NULL on error. just fyi β that's a them thing, and now you know it. πΈ
β STANDARDS β
GNU, BSD. so like, both. iconic behaviour. π
π SEE ALSO π
mwah π that's the whole page, bestie. go free your strings.
Linux man-pages 6.20 β dated 2026-02-08. that's the source, the version, and the date, all cute and accounted for, and that's on me being thorough. periodt. π β¨
π¦ 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: Linux-man-pages-copyleft
- Copyright 2001, Andries E. Brouwer <aeb@cwi.nl>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms