PRINTF(3) Linux Programmer's Manual PRINTF(3) NAME printf, fprintf, dprintf, sprintf, snprintf, vprintf, vfprintf, vdprintf, vsprintf, vsnprintf - formatted output conversion SYNOPSIS #include <stdio.h> int printf(const char *restrict format, ...); int fprintf(FILE *restrict stream, const char *restrict format, ...); int dprintf(int 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, ...); #include <stdarg.h> int vprintf(const char *restrict format, va_list ap); int vfprintf(FILE *restrict stream, const char *restrict format, va_list ap); int vdprintf(int fd, const char *restrict format, va_list ap); int vsprintf(char *restrict str, const char *restrict format, va_list ap); int vsnprintf(char *restrict str, size_t size, 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 dprintf(), vdprintf(): Since glibc 2.10: _POSIX_C_SOURCE >= 200809L Before glibc 2.10: _GNU_SOURCE DESCRIPTION The functions in the printf() family produce output according to a format as described below. The functions printf() and vprintf() write output to stdout, the standard output stream; fprintf() and vfprintf() write output to the given output stream; sprintf(), snprintf(), vsprintf(), and vsnprintf() write to the charac‐ ter string str. The function dprintf() is the same as fprintf() except that it outputs to a file descriptor, fd, instead of to a stdio(3) stream. The functions snprintf() and vsnprintf() write at most size bytes (including the terminating null byte ('\0')) to str. The functions vprintf(), vfprintf(), vdprintf(), vsprintf(), vsnprintf() are equivalent to the functions printf(), fprintf(), dprintf(), sprintf(), snprintf(), respectively, except that they are called with a va_list instead of a variable number of arguments. These functions do not call the va_end macro. Because they invoke the va_arg macro, the value of ap is undefined after the call. See stdarg(3). All of these functions write the output under the control of a format string that specifies how subsequent arguments (or arguments accessed via the variable- length argument facilities of stdarg(3)) are converted for output. C99 and POSIX.1-2001 specify that the results are undefined if a call to sprintf(), snprintf(), vsprintf(), or vsnprintf() would cause copying to take place be‐ tween objects that overlap (e.g., if the target string array and one of the supplied input arguments refer to the same buffer). See NOTES. Format of the format string The format string is a character string, beginning and ending in its initial shift state, if any. The format string is composed of zero or more directives: or‐ dinary characters (not %), which are copied unchanged to the output stream; and conversion specifications, each of which results in fetching zero or more subse‐ quent arguments. Each conversion specification is introduced by the character %, and ends with a conversion specifier. In between there may be (in this order) zero or more flags, an optional minimum field width, an optional precision and an optional length modifier. The overall syntax of a conversion specification is: %[$][flags][width][.precision][length modifier]conversion The arguments must correspond properly (after type promotion) with the conversion specifier. By default, the arguments are used in the order given, where each '*' (see Field width and Precision below) and each conversion specifier asks for the next argument (and it is an error if insufficiently many arguments are given). One can also specify explicitly which argument is taken, at each place where an argument is required, by writing "%m$" instead of '%' and "*m$" instead of '*', where the decimal integer m denotes the position in the argument list of the desired argument, indexed starting from 1. Thus, printf("%*d", width, num); and printf("%2$*1$d", width, num); are equivalent. The second style allows repeated references to the same argument. The C99 standard does not include the style using '