Next: sprintf
Up: Printing
Previous: Printing
printf
The printf command (format printing) translates internal values
into characters for sending to the standard output. The simplest form
of this command echos back the text pattern it is passed.
> printf('The simplest form of this command echos');
However, we can use the text pattern to control the exact
format we would like for the output through the use of conversion
patterns and cursor control characters. For example,
> printf('\nFirst a carriage return & new line\n');
First a carriage return & new line
> printf('then print an integer %d', 101);
then print an integer 101
> printf('\nstep\vstep\vstep\vstep\vstep\vstep\vstep\n');
step
step
step
step
step
step
step
A text pattern is an ordinary name with the
addition of zero or more conversion patterns. Each conversion
pattern begins with a percent symbol (%
) and is followed by
- a minus symbol (
-
) indicating the argument should be left
justified,
- a number that specifies the minimum field width,
- a period which separates the field width from the precision,
- a number indicating the precision for a real value (this is
the number of digits after the decimal point),
- a character from the set {a, c, d, e, f, g, l, o, s, u, x,
%}. (Table
contains a description of the
function for each.)
Table:
Conversion characters for the printf command.
1.1
Table:
Conversion characters for the printf command.
Character |
Description |
a |
prints any Darwin value including lists and sets |
c |
prints a single character |
d |
prints an integer |
e |
prints a number in exponential notation |
f |
prints a real number |
g |
prints a real number in the same manner as the |
|
e character if the exponent is greater than 8. |
|
Otherwise equivalent to the f or d characters. |
o |
prints the octal conversion of an integer |
s |
prints out an item of type name |
u |
prints an unsigned integer |
x |
prints the hexadecimal conversion of an integer |
% |
prints a percent sign % |
|
> printf('%a, %a', ['L', 'I', 'S', 'T'], 'a means any structure');
[L, I, S, T], a means any structure
> int := 1234:
> printf('\n|%d|%10d|%-10d|', int, int, int, int); # integers
|1234| 1234|1234 |
> printf('\n|%11s|%12s|%12s|%12s|', 'normal', 'field of 12', '5 decimal', 'left flush');
| normal| field of 12| 5 decimal| left flush|
> r := 1234.567:
> printf('\n|%f|%12f|%12.5f|%-12.5f|', r, r, r, r);
|1234.567000| 1234.567000| 1234.56700|1234.56700 |
The backslash symbol (\
) is treated as a special symbol in a
text
pattern. Followed by a letter from Table
, it can
be used to control the position of the cursor.
> printf(' <- The number five goes here\r 5\n');
5 <- The number five goes here
> printf('A newline\vfollowed by a single quote \''');
A tab
followed by a single quote '
Table:
The cursor control sequences for the printf command.
1.1
Table:
The cursor control sequences for the printf command.
Character |
Description |
\b |
backspace |
\n |
carriage return |
|
and newline |
\t |
tab |
\v |
newline |
\r |
carriage return |
\\ |
\ |
\'' |
' |
|
The syntax for a printf command is as follows:
printf(
textpattern,
)
The value of expression expi is placed in the ith conversion
pattern found in the string item
textpattern according to the type of this
conversion pattern. If the number of expressions is greater than the
number of conversion patterns, the extraneous ones are ignored. If
the number of conversion patterns is greater than the number of
expressions, Darwin responds with an error. One must take care to
match correct conversion patterns with the type of the corresponding
expression. In some cases, Darwin will not respond
with an error and the resulting text may appear nonsensical.
The best way to become familiar with the printf command in
Darwin is through examples and experimentation.
> printf('\n%e \b %g \b %g' , 1234567, 1234567, 123456);
1.234567e+06 1.23457e+06 123456
> printf('\nOctal: %o, Hexidecimal: %x',888, 888);
Octal: 1570, Hexidecimal: 378
> printf('\nA character %c followed by a string %s', 'z', 'hello');
A character z followed by a string hello
> printf('\nA percent sign %% followed by a \'' symbol.');
A percent sign % followed by a ' symbol.
> printf('\nfirst a new line and carriage return\vnow just a newline');
first a new line and carriage return
now just a newline
Next: sprintf
Up: Printing
Previous: Printing
Gaston Gonnet
1998-09-15