- Form: PRINTF 'Format string' [expressions] [redirection]
This command displays character strings and variables in specified formats,
thus producing tables of results.
The simplest form of PRINTF is PRINTF 'string'. This prints the specified
string. Examples are:
- PRINTF HELLO
- prints HELLO
- PRINTF 'Hello, world'
- prints Hello, world
You can print the values of expressions by specifying in the character
string (1) that expression is to be evaluated and its value printed, and
(2) the format for the printing of the variable. The character % in the
string does the job. It tells that an expression is to be evaluated and
printed where the % appears. The rest of the word following the % is used
to specify the format of the string. The format specifiers are the same as
they are in FORTRAN. ANY valid FORTRAN specifier appropriate for
displaying numeric values may be used. You can specify multiple variables
with the same format specification by putting the number of variables for
which you wish to use the specifier for before the format specifier.
For example, if you want to print a single floating point variable with a
field width of 9 characters with 3 after the decimal point, use
%f9.3, and give one argument after the format specifier. If you have
5 variables which you want all printed in this format, you can use
either '%f9.3 %f9.3 %f9.3 %f9.3 %f9.3', or '%5f9.3'.
For users familiar with C, you can also use the %d specifier for an
integer (as well as %i); note, however, that the width/precision
specification comes after the format specifier, not before it (e.g.,
arguments to be printed with the desired format.
Examples:
Suppose we have the variables A with value 1.0 and PI with value
3.14159. Then
- PRINTF '%F4.1 %F9.4' A PI
- prints ' 1.0 3.1416'
- PRINTF '%I6 and %F9.5' A PI
- prints ' 1 and 3.14159'
- PRINTF 'The value of pi is %F9.7' A PI
- prints
'The value of pi is 3.1415900'
Note that spaces between % specifiers are printed. The output of PRINTF
can be redirected.
To force a new line when printing, use the pattern \n
in the format
statement (the newline character). An example is:
- A=5.1234
-
- PRINTF 'The value of A is
n %F9.3' A
- prints
The value of A is
- 5.123
See the section SUBSTITUTE (type 'HELP SUBSTITUTE' if you are on a
terminal) for instructions on printing string variables.