Manual / printf
printf(1)
| PRINTF(1) | General Commands Manual | PRINTF(1) |
NAME
printf - write formatted output
SYNOPSIS
printf format [ argument ] ...
DESCRIPTION
Printf writes format to standard output, taking successive argument operands to satisfy the conversions format contains. Format is reused as often as necessary to satisfy every argument, but only while it contains at least one argument-consuming conversion; a format with none is written exactly once regardless of how many argument words follow it. An argument printf runs out of before format does is treated as an empty string or the value 0, whichever the conversion consuming it wants.
Within format, an unescaped % introduces a conversion and a backslash introduces one of the escapes below; every other byte is copied through unchanged.
ESCAPES
- \\
- a literal backslash.
- \a \b \f \n \r \t \v
- alert, backspace, form feed, newline, carriage return, tab, vertical tab. On this machine \n and \r are the SAME byte (CR, $8D) -- there is no separate linefeed to preserve.
- \nnn
- a byte whose value is the 1-, 2- or 3-digit octal number nnn, taken directly after the backslash (no leading 0 required).
Every escaped byte, and every digit/sign/prefix a conversion below manufactures itself, carries bit 7 set -- this machine's own text convention -- so \101 prints as a plain A rather than an inverse-video byte. Argument text passed through by %s/%c/%b is never touched this way: it already carries that convention from the shell.
CONVERSIONS
Each is %, zero or more flags (- + (space) # 0), an optional decimal field width, an optional .precision and one of the letters below.
- d or i
- the argument, parsed as a (16-bit) signed decimal integer.
- u
- the argument, parsed and printed as unsigned decimal.
- o
- unsigned octal; # forces a leading 0 if the digits don't already have one.
- x or X
- unsigned hex, lower or upper case; # prepends 0x/0X, unless the value is 0.
- c
- the first character of the argument (nothing, for an empty or missing argument).
- s
- the argument, truncated to precision characters if given.
- %
- a literal %; consumes no argument.
- b
- the argument, with ITS OWN backslash escapes interpreted (the same set as above, plus \0nnn for an octal byte, and \c, which stops all further output -- this argument, any later arguments, and any of format still to come).
- left-justifies within the field width (the default is right- justified); 0 zero-pads a right-justified numeric field, but is ignored once a precision is given; + forces a leading sign on d/i; a leading space does the same but with a blank instead of +, and is itself ignored when + is also given. A precision of 0 on a value of 0 prints no digits at all for d/i/o/u/x/X.
16-BIT ARGUMENTS
This machine's only integer size is 16 bits. d/i/o/u/x/X each parse their argument into ONE 16-bit register -- d/i then display that register's bit pattern SIGNED, o/u/x/X display the SAME bits UNSIGNED -- so printf %x 40000 prints that value's hex bit pattern, and printf %d 40000 prints the identical bits as a negative decimal number, the same int16_t/uint16_t pun a real machine would make. A magnitude that fits neither encoding at all (over 65535, or over 32768 behind a leading -), or an argument that is not a decimal integer, is diagnosed: a message to standard error, the value 0 is used in its place, and printf's own exit status is 1 -- but conversion of the rest of format and the remaining argument operands continues regardless. A leading 'c or "c argument is accepted (POSIX's own extension): it takes c's own value, low 7 bits, and is never a failure.
DIAGNOSTICS
0 if every conversion succeeded; 1 if printf was called with no format at all, or if any conversion failed as described above (a diagnostic to standard error, the value 0 used, and the run continues to completion regardless).
FILES
/usr/man/PRINTF this page
SEE ALSO
BUGS
No floating-point conversions (a/A/e/E/f/F/g/G) -- this machine has no floating point. No %n$-style numbered arguments. A format with NO argument-consuming conversion, given MORE argument operands than it needs, is written exactly once (this build's own reading of an unspecified corner -- some historic implementations diagnose it instead).
| September 14, 2026 | maux |