| 1 |
'\" t |
| 2 |
.\" Copyright 2001, Andries E. Brouwer <[email protected]> |
| 3 |
.\" Copyright, the authors of the Linux man-pages project |
| 4 |
.\" |
| 5 |
.\" SPDX-License-Identifier: Linux-man-pages-copyleft |
| 6 |
.\" |
| 7 |
.TH asprintf 3 2026-02-08 "Linux man-pages 6.18" |
| 8 |
.SH NAME |
| 9 |
asprintf, vasprintf \- print to allocated string |
| 10 |
.SH LIBRARY |
| 11 |
Standard C library |
| 12 |
.RI ( libc ,\~ \-lc ) |
| 13 |
.SH SYNOPSIS |
| 14 |
.nf |
| 15 |
.BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */" |
| 16 |
.B #include <stdio.h> |
| 17 |
.P |
| 18 |
.BI "int asprintf(char **restrict " strp ", const char *restrict " fmt ", ...);" |
| 19 |
.BI "int vasprintf(char **restrict " strp ", const char *restrict " fmt , |
| 20 |
.BI " va_list " ap ); |
| 21 |
.fi |
| 22 |
.SH DESCRIPTION |
| 23 |
The functions |
| 24 |
.BR asprintf () |
| 25 |
and |
| 26 |
.BR vasprintf () |
| 27 |
are analogs of |
| 28 |
.BR sprintf (3) |
| 29 |
and |
| 30 |
.BR vsprintf (3), |
| 31 |
except that they allocate a string large enough to hold the output |
| 32 |
including the terminating null byte (\[aq]\[rs]0\[aq]), |
| 33 |
and return a pointer to it via the first argument. |
| 34 |
This pointer should be passed to |
| 35 |
.BR free (3) |
| 36 |
to release the allocated storage when it is no longer needed. |
| 37 |
.SH RETURN VALUE |
| 38 |
When successful, these functions return the number of bytes printed, |
| 39 |
just like |
| 40 |
.BR sprintf (3). |
| 41 |
On error, |
| 42 |
\-1 is returned, |
| 43 |
.I errno |
| 44 |
is set to indicate the error, |
| 45 |
and the contents of |
| 46 |
.I strp |
| 47 |
are undefined. |
| 48 |
.SH ERRORS |
| 49 |
See |
| 50 |
.BR sprintf (3) |
| 51 |
and |
| 52 |
.BR malloc (3). |
| 53 |
.SH ATTRIBUTES |
| 54 |
For an explanation of the terms used in this section, see |
| 55 |
.BR attributes (7). |
| 56 |
.TS |
| 57 |
allbox; |
| 58 |
lbx lb lb |
| 59 |
l l l. |
| 60 |
Interface Attribute Value |
| 61 |
T{ |
| 62 |
.na |
| 63 |
.nh |
| 64 |
.BR asprintf (), |
| 65 |
.BR vasprintf () |
| 66 |
T} Thread safety MT-Safe locale |
| 67 |
.TE |
| 68 |
.SH VERSIONS |
| 69 |
The FreeBSD implementation sets |
| 70 |
.I strp |
| 71 |
to NULL on error. |
| 72 |
.SH STANDARDS |
| 73 |
GNU, BSD. |
| 74 |
.SH SEE ALSO |
| 75 |
.BR free (3), |
| 76 |
.BR malloc (3), |
| 77 |
.BR printf (3) |