printf(3)

๐Ÿ’– NAME ๐Ÿ’–

okay so bestie, welcome to the printf(3) family group chat โ€” this is the Library Functions Manual page, and it's a lot, but we're gonna get through it together ๐Ÿซถ

printf, fprintf, dprintf, vprintf, vfprintf, vdprintf, โ€” formatted output conversion โœจ

(also, lowkey the manual's name is just printf(3), that's the whole title, slay)

๐Ÿ“š LIBRARY

Standard C library (libc, -lc)


โœจ 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 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);

๐Ÿงš Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

  • dprintf(), vdprintf():
    • Since glibc 2.10:
      • _POSIX_C_SOURCE >= 200809L
    • Before glibc 2.10:
      • _GNU_SOURCE

๐ŸŒธ DESCRIPTION ๐ŸŒธ

so like. the functions in the printf() family produce output according to a format, as described down below ๐Ÿ‘‡ and honestly it's giving "I contain multitudes."

printf() and vprintf() write output to stdout, the standard output stream. fprintf() and vfprintf() write output to the given output stream. classic.

dprintf() is the same as fprintf() except that it outputs to a file descriptor, fd, instead of to a stdio(3) stream. no stream, just raw fd energy ๐Ÿ’…

vprintf(), vfprintf(), vdprintf() are equivalent to printf(), fprintf(), dprintf(), respectively, except that they're called with a va_list instead of a variable number of arguments. important: these functions do not call the va_end macro. and 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 โœจ

๐ŸŽ€ 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:

  • ordinary characters (not %), which are copied unchanged to the output stream; and
  • conversion specifications, each of which results in fetching zero or more subsequent 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:

%[argument$][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 '$', which comes from the Single UNIX Specification.

If the style using '$' is used, it must be used throughout for all conversions taking an argument and all width and precision arguments, but it may be mixed with "%%" formats, which do not consume an argument. There may be no gaps in the numbers of arguments specified using '$' โ€” for example, if arguments 1 and 3 are specified, argument 2 must also be specified somewhere in the format string. no skipping, bestie ๐Ÿ™…โ€โ™€๏ธ

For some numeric conversions a radix character ("decimal point") or thousands' grouping character is used. The actual character used depends on the LC_NUMERIC part of the locale. (See setlocale(3).) The POSIX locale uses '.' as radix character, and does not have a grouping character. Thus,

printf("%'.2f", 1234567.89);

results in "1234567.89" in the POSIX locale, in "1234567,89" in the nl_NL locale, and in "1.234.567,89" in the da_DK locale ๐ŸŒ

๐Ÿ’… Flag characters

The character % is followed by zero or more of the following flags:

  • # โ€” The value should be converted to an "alternate form". For o conversions, the first character of the output string is made zero (by prefixing a 0 if it was not zero already). For x and X conversions, a nonzero result has the string "0x" (or "0X" for X conversions) prepended to it. For a, A, e, E, f, F, g, and G conversions, the result will always contain a decimal point, even if no digits follow it (normally, a decimal point appears in the results of those conversions only if a digit follows). For g and G conversions, trailing zeros are not removed from the result as they would otherwise be. For m, if errno contains a valid error code, the output of strerrorname_np(errno) is printed; otherwise, the value stored in errno is printed as a decimal number. For other conversions, the result is undefined ๐Ÿ’€
  • 0 โ€” The value should be zero padded. For d, i, o, u, x, X, a, A, e, E, f, F, g, and G conversions, the converted value is padded on the left with zeros rather than blanks. If the 0 and - flags both appear, the 0 flag is ignored. If a precision is given with an integer conversion (d, i, o, u, x, and X), the 0 flag is ignored. For other conversions, the behavior is undefined ๐Ÿ‘€
  • - โ€” The converted value is to be left adjusted on the field boundary. (The default is right justification.) The converted value is padded on the right with blanks, rather than on the left with blanks or zeros. A - overrides a 0 if both are given.
  • ' ' (a space) โ€” A blank should be left before a positive number (or empty string) produced by a signed conversion.
  • + โ€” A sign (+ or -) should always be placed before a number produced by a signed conversion. By default, a sign is used only for negative numbers. A + overrides a space if both are used.

The five flag characters above are defined in the C99 standard. POSIX specifies one further flag character โœจ

  • ' โ€” For decimal conversion (i, d, u, f, F, g, G) the output is to be grouped with thousands' grouping characters as a non-monetary quantity. Misleadingly, this isn't necessarily every thousand: for example Karbi ("mjw_IN"), groups its digits into 3 once, then 2 repeatedly ๐Ÿซ  Compare locale(7) grouping and thousands_sep, contrast with mon_grouping/mon_thousands_sep and strfmon(3). This is a no-op in the default "C" locale.

glibc 2.2 adds one further flag character ๐Ÿ‘‘

  • I โ€” For decimal integer conversion (i, d, u) the output uses the locale's alternative output digits, if any. For example, since glibc 2.2.3 this will give Arabic-Indic digits in the Persian ("fa_IR") locale ๐ŸŒ™

๐Ÿ“ Field width

An optional decimal digit string (with nonzero first digit) specifying a minimum field width. If the converted value has fewer characters than the field width, it will be padded with spaces on the left (or right, if the left-adjustment flag has been given). Instead of a decimal digit string one may write "*" or "*m$" (for some decimal integer m) to specify that the field width is given in the next argument, or in the m-th argument, respectively, which must be of type int.

A negative field width is taken as a '-' flag followed by a positive field width. In no case does a nonexistent or small field width cause truncation of a field โ€” if the result of a conversion is wider than the field width, the field is expanded to contain the conversion result ๐Ÿ’ช no data lost, periodt.

๐Ÿ” Precision

An optional precision, in the form of a period (.) followed by an optional decimal digit string. Instead of a decimal digit string one may write "*" or "*m$" (for some decimal integer m) to specify that the precision is given in the next argument, or in the m-th argument, respectively, which must be of type int.

If the precision is given as just '.', the precision is taken to be zero. A negative precision is taken as if the precision were omitted. This gives:

  • the minimum number of digits to appear for d, i, o, u, x, and X conversions,
  • the number of digits to appear after the radix character for a, A, e, E, f, and F conversions,
  • the maximum number of significant digits for g and G conversions,
  • or the maximum number of characters to be printed from a string for s and S conversions ๐ŸŒˆ

๐Ÿงฌ Length modifier

Here, "integer conversion" stands for d, i, o, u, x, or X conversion.

  • hh โ€” A following integer conversion corresponds to a signed char or unsigned char argument, or a following n conversion corresponds to a pointer to a signed char argument.
  • h โ€” A following integer conversion corresponds to a short or unsigned short argument, or a following n conversion corresponds to a pointer to a short argument.
  • l (ell) โ€” A following integer conversion corresponds to a long or unsigned long argument, or a following n conversion corresponds to a pointer to a long argument, or a following c conversion corresponds to a wint_t argument, or a following s conversion corresponds to a pointer to wchar_t argument. On a following a, A, e, E, f, F, g, or G conversion, this length modifier is ignored (C99; not in SUSv2).
  • ll (ell-ell) โ€” A following integer conversion corresponds to a long long or unsigned long long argument, or a following n conversion corresponds to a pointer to a long long argument.
  • q โ€” A synonym for ll. This is a nonstandard extension, derived from BSD; avoid its use in new code ๐Ÿ™ˆ
  • L โ€” A following a, A, e, E, f, F, g, or G conversion corresponds to a long double argument. (C99 allows %LF, but SUSv2 does not.)
  • j โ€” A following integer conversion corresponds to an intmax_t or uintmax_t argument, or a following n conversion corresponds to a pointer to an intmax_t argument.
  • wN โ€” A following integer conversion corresponds to an integer of a width of N bits (C23) โญ
  • z โ€” A following integer conversion corresponds to a size_t or ssize_t argument, or a following n conversion corresponds to a pointer to a size_t argument.
  • Z โ€” A nonstandard synonym for z that predates the appearance of z. Do not use in new code ๐ŸŒธ
  • t โ€” okay so a t before an integer conversion means the arg is a ptrdiff_t, and if it's sitting in front of an n conversion then you're pointing at a ptrdiff_t. That's the whole deal. โœจ

SUSv3 specifies literally all of the above โ€” except the ones we explicitly called out as nonstandard extensions, bestie, those are the wild ones. SUSv2 was way more limited: it only knew the length modifiers h (in hd, hi, ho, hx, hX, hn) and l (in ld, li, lo, lx, lX, ln, lc, ls) and L (in Le, LE, Lf, Lg, LG). That's it. She was doing her best. ๐Ÿ’…

As a nonstandard extension (key words, it's giving not-portable), the GNU implementations treat ll and L as synonyms, so you can literally write llg (as a synonym for the standards-compliant Lg) and Ld (as a synonym for the standards compliant lld). Such usage is nonportable. ๐Ÿšจ Like, it works on GNU, don't go shipping it everywhere and acting surprised.


โœจ Conversion specifiers โœจ

A character that specifies the type of conversion to be applied. The conversion specifiers and their meanings are, bestie, a whole vibe:

  • d, i โ€” the int argument gets converted to signed decimal notation. The precision, if any, gives the minimum number of digits that must appear; if the converted value needs fewer digits, it gets padded on the left with zeros. Default precision is 1. And when 0 is printed with an explicit precision 0, the output is empty. Nothing. Silence. Iconic. ๐Ÿซถ
  • o, u, x, X โ€” the unsigned int argument gets converted to unsigned octal (o), unsigned decimal (u), or unsigned hexadecimal (x and X) notation. The letters abcdef are used for x conversions; the letters ABCDEF are used for X conversions. The precision, if any, gives the minimum number of digits that must appear; if the converted value needs fewer digits, it gets padded on the left with zeros. Default precision is 1. When 0 is printed with an explicit precision 0, the output is empty. Same energy as above, different base. ๐Ÿ”ข
  • e, E โ€” the double argument is rounded and converted in the style [-]d.dddeยฑdd where there's one digit (which is nonzero if the argument is nonzero) before the decimal-point character, and the number of digits after it is equal to the precision; if the precision is missing, it's taken as 6; if the precision is zero, no decimal-point character appears. An E conversion uses the letter E (rather than e) to introduce the exponent. The exponent always contains at least two digits; if the value is zero, the exponent is 00. ๐Ÿ’…
  • f, F โ€” the double argument is rounded and converted to decimal notation in the style [-]ddd.ddd, where the number of digits after the decimal-point character is equal to the precision specification. If the precision is missing, it's taken as 6; if the precision is explicitly zero, no decimal-point character appears. If a decimal point does appear, at least one digit appears before it. Validating.

(Okay so: SUSv2 does not know about F and says that character string representations for infinity and NaN may be made available. SUSv3 adds a specification for F. The C99 standard specifies "[-]inf" or "[-]infinity" for infinity, and a string starting with "nan" for NaN, in the case of f conversion, and "[-]INF" or "[-]INFINITY" or "NAN" in the case of F conversion. So yes, she yells it in caps. ๐Ÿ—ฃ๏ธ)

  • g, G โ€” the double argument gets converted in style f or e (or F or E for G conversions). The precision specifies the number of significant digits. If the precision is missing, 6 digits are given; if the precision is zero, it's treated as 1. Style e is used if the exponent from its conversion is less than -4 or greater than or equal to the precision. Trailing zeros are removed from the fractional part of the result; a decimal point appears only if it's followed by at least one digit. She's efficient, she doesn't keep the baggage. โœ‚๏ธ
  • a, A โ€” (C99; not in SUSv2, but added in SUSv3.) For a conversion, the double argument is converted to hexadecimal notation (using the letters abcdef) in the style [-]0xh.hhhhpยฑd; for A conversion the prefix 0X, the letters ABCDEF, and the exponent separator P is used. There is one hexadecimal digit before the radix point, and the number of digits after it is equal to the precision. The default precision suffices for an exact representation of the value if an exact representation in base 2 exists and otherwise is sufficiently large to distinguish values of type double. The digit before the radix point is unspecified for nonnormalized numbers, and nonzero but otherwise unspecified for normalized numbers. The exponent, d, is the appropriate exponent of 2 expressed as a decimal integer; it always contains at least one digit; if the value is zero, the exponent is 0. That's a lot of words but she ate. ๐Ÿ”ฅ
  • c โ€” if no l modifier is present, the int argument is converted to an unsigned char, and the resulting character is written. If an l modifier is present, the wint_t (wide character) argument is converted to a multibyte sequence by a call to the wcrtomb(3) function, with a conversion state starting in the initial state, and the resulting multibyte string is written. Wide girl summer. ๐ŸŒˆ
  • s โ€” if no l modifier is present: the const char * argument is expected to be a pointer to an array of character type (pointer to a string). Characters from the array are written up to (but not including) a terminating null byte ('\0'); if a precision is specified, no more than the number specified are written. If a precision is given, no null byte need be present; if the precision is not specified, or is greater than the size of the array, the array must contain a terminating null byte.

If an l modifier is present: the const wchar_t * argument is expected to be a pointer to an array of wide characters. Wide characters from the array are converted to multibyte characters (each by a call to the wcrtomb(3) function, with a conversion state starting in the initial state before the first wide character), up to and including a terminating null wide character. The resulting multibyte characters are written up to (but not including) the terminating null byte. If a precision is specified, no more bytes than the number specified are written, but no partial multibyte characters are written. Note that the precision determines the number of bytes written, not the number of wide characters or screen positions. The array must contain a terminating null wide character, unless a precision is given and it's so small that the number of bytes written exceeds it before the end of the array is reached. ๐Ÿฆ‹

  • C โ€” (Not in C99 or C11, but in SUSv2, SUSv3, and SUSv4.) Synonym for lc. Don't use. ๐Ÿšซ That's it, that's the review.
  • S โ€” (Not in C99 or C11, but in SUSv2, SUSv3, and SUSv4.) Synonym for ls. Don't use. ๐Ÿšซ Same energy, equally don't.
  • p โ€” the void * pointer argument is printed in hexadecimal (as if by %#x or %#lx). Pointers, but make it โœจhexโœจ.
  • n โ€” the number of characters written so far is stored into the integer pointed to by the corresponding argument. That argument shall be an int *, or variant whose size matches the (optionally) supplied integer length modifier. No argument is converted. (This specifier is not supported by the bionic C library.) The behavior is undefined if the conversion specification includes any flags, a field width, or a precision. > Girl. Do not. This one is the villain of the entire manpage, and it's the reason printf(foo) is dangerous (see BUGS, we'll get there ๐Ÿ’€).
  • m โ€” (glibc extension; supported by uClibc and musl, and on Android from API level 29.) Print output of strerror(errno) (or strerrorname_np(errno) in the alternate form). No argument is required. Free error string, no arg needed, she's generous. ๐ŸŽ€
  • % โ€” a % is written. No argument is converted. The complete conversion specification is %%. Escaping herself, love that for her. ๐Ÿ’–

๐Ÿ’ซ RETURN VALUE ๐Ÿ’ซ

Upon successful return, these functions return the number of bytes printed (excluding the null byte used to end output to strings). So, the count, not the vibe. ๐Ÿ“

On error, a negative value is returned, and errno is set to indicate the error. Rude, but informative.


๐Ÿ˜ญ ERRORS ๐Ÿ˜ญ

See write(2) and putwc(3). In addition, the following error may occur:

  • EOVERFLOW โ€” the value to be returned is greater than INT_MAX. Bestie you did too much. She can't count that high.

The dprintf() function may fail additionally if:

  • EBADF โ€” the fd argument is not a valid file descriptor. That's not a real fd, babe. ๐Ÿ™ˆ

๐Ÿซถ ATTRIBUTES ๐Ÿซถ

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

  • printf(), fprintf(), vprintf(), vfprintf() โ€” Thread safety: MT-Safe locale. Slay, she's thread safe. (Well, locale thread safe. iykyk.)

๐Ÿ‘‘ STANDARDS ๐Ÿ‘‘

  • fprintf(), printf(), vprintf(), vfprintf() โ€” C11, POSIX.1-2008.
  • dprintf(), vdprintf() โ€” GNU, POSIX.1-2008.

๐ŸŒธ HISTORY ๐ŸŒธ

  • fprintf(), printf(), vprintf(), vfprintf() โ€” C89, POSIX.1-2001.
  • dprintf(), vdprintf() โ€” GNU, POSIX.1-2008.

Issue 4 of the X/Open Portability Guide (SUSv1, 1994) adds '.

glibc 2.1 adds length modifiers hh, j, t, and z and conversion characters a and A. The glow up. โœจ

glibc 2.2 adds the conversion character F with C99 semantics, and the flag character I.

glibc 2.35 gives a meaning to the alternate form (#) of the m conversion specifier, that is %#m. New lore dropped in 2.35. ๐Ÿ“–


๐Ÿ’€ BUGS ๐Ÿ’€

Code such as printf(foo); often indicates a bug, since foo may contain a % character. If foo comes from untrusted user input, it may contain %n, causing the printf() call to write to memory and creating a security hole. ๐Ÿ’€๐Ÿ’€๐Ÿ’€

No fr, this is the real one. Keep your user input out of the format string. Not me watching people pass untrusted strings straight into printf. ๐Ÿšจ

โœจ EXAMPLES โœจ

Okay bestie, time for the fun part โ€” actually using this thing. ๐ŸŽ€

Wanna print Pi to five decimal places? Say less:

#include <math.h>
#include <stdio.h>
fprintf(stdout, "pi = %.5f\n", 4 * atan(1.0));

Look at that. Five decimals. Iconic behaviour, no notes. ๐Ÿ’…


Now say you want a date and time looking like "Sunday, July 3, 10:02", where weekday and month are pointers to strings. Boom:

#include <stdio.h>
fprintf(stdout, "%s, %s %d, %.2d:%.2d\n",
        weekday, month, day, hour, min);

Cute, right? Month name first, day second โ€” very American of it. ๐Ÿ‡บ๐Ÿ‡ธ


Here's the plot twist though: so many countries do day-month-year, because honestly why would you do it any other way. So an internationalized version has to be able to print the arguments in an order that the format itself decides. Like this:

#include <stdio.h>
fprintf(stdout, format,
        weekday, month, day, hour, min);

where format depends on the locale, and โ€” this is the juicy bit โ€” it may permute the arguments. Rearrange them. Shuffle them. The format is in charge. ๐Ÿ˜ค

So if format has the value:

"%1$s, %3$d. %2$s, %4$d:%5$.2d\n"

then one might obtain:

"Sonntag, 3. Juli, 10:02"

SEEE. The position arguments (%1$s, %3$d, %2$sโ€ฆ) let you pull the args back out of order, so German gets its day-first moment. Lรคnder representation, we love to see it. ๐Ÿ‡ฉ๐Ÿ‡ชโœจ


๐ŸŒˆ SEE ALSO ๐ŸŒˆ

Your reading list, bestie โ€” go be nosy:


And that's a wrap on printf(3)! ๐Ÿซถ This little masterpiece is from Linux man-pages 6.20, dated 2026-02-16 โ€” go forth and format responsibly, my love. ๐Ÿ’–โœจ

๐Ÿ“ฆ 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: GPL-2.0-or-later
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms