> 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
-) indicating the argument should be left
justified,
contains a description of the
function for each.)
> 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 '
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