asprintf(3)

asprintf3

Print to allocated string

Usage

#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);

Library

Standard C library (libc, -lc)

Overview

The functions asprintf() and vasprintf() are analogs of sprintf(3) and vsprintf(3), except that they allocate a string large enough to hold the output including the terminating null byte ('\0'), and return a pointer to it via the first argument. This pointer should be passed to free(3) to release the allocated storage when it is no longer needed.

Return value

When successful, these functions return the number of bytes printed, just like sprintf(3). On error, -1 is returned, errno is set to indicate the error, and the contents of strp are undefined.

Errors

See sprintf(3) and malloc(3).

Attributes

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

InterfaceAttributeValue
asprintf(), vasprintf()Thread safetyMT-Safe locale

Versions

The FreeBSD implementation sets strp to NULL on error.

Releases

16 releases since Debian 2.0 (Hamm); the text changed 15 times.

  1. 2.0
  2. 2.1
  3. 2.2
  4. 3.0
  5. 3.1
  6. 4.0
  7. 5.0
  8. 6.0
  9. 7
  10. 8
  11. 9
  12. 10
  13. 11
  14. 12
  15. 13
  16. Arch

Standards

GNU, BSD.

From man-pages 6.19-1 in Arch Linux 2026-11-01. Parsed by carta from the page's man source.