cs534 - more printf stuff

Write the formatted arguments to the standard output under the control of the format

SYNTAX

printf format [arguments ...]

OPTIONS

SymbolMeaning/Usage
di The argument is printed as a signed decimal (d or i), respectively
oThe argument is printed as an unsigned octal
uThe argument is printed as an unsigned decimal
oXxThe argument is printed as an unsigned hexadecimal
fThe argument is printed in the style [-]ddd.ddd where the number of d's after the decimal point is equal to the preci- sion specification for the argument. If the precision is missing, 6 digits are given; if the precision is explicitly 0, no digits and no decimal point are printed.
eEThe argument is printed in the style [-]d.ddde+-dd where there is one digit before the decimal point and the number after is equal to the precision specification for the argu- ment; when the precision is missing, 6 digits are produced. An upper-case E is used for an `E' format.
gG The argument is printed in style f or in style e (E) whichever gives full precision in minimum space.
b Characters from the string argument are printed with back- slash-escape sequences expanded.
cThe first character of argument is printed
s Characters from the string argument are printed until the end is reached or until the number of characters indicated by the precision specification is reached; however if the precision is 0 or missing, all characters in the string are printed
%Print a `%'; no argument is used

The format is a character string which contains three types of objects:

character stringwhere it goes
plain characterssimply copied to standard output
character escape sequencesconverted and copied to standard output
format specificationscauses printing of the next successive argument

The arguments after the first are treated as strings if the corresponding format is either b, c or s

Otherwise it is evaluated as a C constant, with the following extensions:

Character escape sequences are in backslash notation as defined in ANSI X3.159-1989 (`ANSI C')

The characters and their meanings are as follows:

escape sequencewhat is does
\eWrite an <escape> character
\aWrite a <bell> character
\bWrite a <backspace> character
\fWrite a <form-feed> character
\nWrite a <new-line> character
\rWrite a <carriage return> character
\tWrite a <tab> character
\vWrite a <vertical tab> character
\'Write a <single quote> character
\\Write a backslash character
\numWrite an 8-bit character whose ASCII value is the 1-, 2-, or 3-digit octal number num

EXTREMELY IMPORTANT: Each format specification is introduced by the percent character (`%')

The remainder of the format specification includes zero or more of the following flags, in the following order:

Character escape sequences are converted and copied to standard output

Format specifications cause printing of the next successive argument

.
flagwhat it does
#Signifies that the value should be printed in an `alternative form'
-A minus sign `-' which specifies left adjustment of the output in the indicated field
+ A `+' character specifying that there should always be a sign placed before the number when using signed formats
` 'A space specifying that a blank should be left before a positive number for a signed format. A `+' overrides a space if both are used
0 A zero `0' character indicating that zero-padding should be used rather than blank-padding. A `-' overrides a `0' if both are used
;

Field Width

Precision

Format

A field width or precision may be `*' instead of a digit string

In this case an argument supplies the field width or precision

Examples

# Print a decimal number
$ printf "%d\n" 5
5

# Print as float (default 6 decimal places)
$ printf "%f\n" 5
5.000000

# Using \n to start a new line:
$ printf "Two separate\nlines\n"
Two separate
lines

# Print decimal numbers interspersed with text
$ printf "There are %d orders valued at over %d euros.\n" 64 1500
There are 64 orders valued at over 1500 euros.

# Convert a hex number to decimal
$ printf "%d\n " 0xF
15

# Convert a decimal number to Hex
$ printf "0x%X\n " 15
0xF

# Convert a decimal number to Octal
$ printf "0%o\n " 8
010

Notes