Next: , Previous: , Up: Stdio   [Contents][Index]


4.61 sprintf, fprintf, printf, snprintf, asprintf, asnprintf—format output

Synopsis

#include <stdio.h>

int printf(const char *restrict format, ...);
int fprintf(FILE *restrict fd, const char *restrict format, ...);
int sprintf(char *restrict str, const char *restrict format, ...);
int snprintf(char *restrict str, size_t size, const char *restrict format,
    ...);
int asprintf(char **restrict strp, const char *restrict format, ...);
char *asnprintf(char *restrict str, size_t *restrict size, const char *restrict format,
    ...);

int _printf_r(struct _reent *ptr, const char *restrict format, ...);
int _fprintf_r(struct _reent *ptr, FILE *restrict fd,
    const char *restrict format, ...);
int _sprintf_r(struct _reent *ptr, char *restrict str,
    const char *restrict format, ...);
int _snprintf_r(struct _reent *ptr, char *restrict str, size_t size,
    const char *restrict format, ...);
int _asprintf_r(struct _reent *ptr, char **restrict strp,
    const char *restrict format, ...);
char *_asnprintf_r(struct _reent *ptr, char *restrict str,
    size_t *restrict size, const char *restrict format, ...);

Description
printf accepts a series of arguments, applies to each a format specifier from *format, and writes the formatted data to stdout, without a terminating NUL character. The behavior of printf is undefined if there are not enough arguments for the format. printf returns when it reaches the end of the format string. If there are more arguments than the format requires, excess arguments are ignored.

fprintf is like printf, except that output is directed to the stream fd rather than stdout.

sprintf is like printf, except that output is directed to the buffer str, and a terminating NUL is output. Behavior is undefined if more output is generated than the buffer can hold.

snprintf is like sprintf, except that output is limited to at most size bytes, including the terminating NUL. As a special case, if size is 0, str can be NULL, and snprintf merely calculates how many bytes would be printed.

asprintf is like sprintf, except that the output is stored in a dynamically allocated buffer, pstr, which should be freed later with free.

asnprintf is like sprintf, except that the return type is either the original str if it was large enough, or a dynamically allocated string if the output exceeds *size; the length of the result is returned in *size. When dynamic allocation occurs, the contents of the original str may have been modified.

For sprintf, snprintf, and asnprintf, the behavior is undefined if the output *str overlaps with one of the arguments. Behavior is also undefined if the argument for %n within *format overlaps another argument.

format is a pointer to a character string containing two types of objects: ordinary characters (other than %), which are copied unchanged to the output, and conversion specifications, each of which is introduced by %. (To include % in the output, use %% in the format string.) A conversion specification has the following form:

       %[pos][flags][width][.prec][size]type

The fields of the conversion specification have the following meanings:

_printf_r, _fprintf_r, _asprintf_r, _sprintf_r, _snprintf_r, _asnprintf_r are simply reentrant versions of the functions above.


Returns
On success, sprintf and asprintf return the number of bytes in the output string, except the concluding NUL is not counted. snprintf returns the number of bytes that would be in the output string, except the concluding NUL is not counted. printf and fprintf return the number of characters transmitted. asnprintf returns the original str if there was enough room, otherwise it returns an allocated string.

If an error occurs, the result of printf, fprintf, snprintf, and asprintf is a negative value, and the result of asnprintf is NULL. No error returns occur for sprintf. For printf and fprintf, errno may be set according to fputc. For asprintf and asnprintf, errno may be set to ENOMEM if allocation fails, and for snprintf, errno may be set to EOVERFLOW if size or the output length exceeds INT_MAX.


Bugs
The “”’ (quote) flag does not work when locale’s thousands_sep is not empty.


Portability
ANSI C requires printf, fprintf, sprintf, and snprintf. asprintf and asnprintf are newlib extensions.

The ANSI C standard specifies that implementations must support at least formatted output of up to 509 characters. This implementation has no inherent limit.

Depending on how newlib was configured, not all format specifiers are supported.

Supporting OS subroutines required: close, fstat, isatty, lseek, read, sbrk, write.



Next: , Previous: , Up: Stdio   [Contents][Index]