Pages

Thursday, April 16, 2009

Fortran by Fortran: The Format statement

One of the things in Fortran I always have some doubt is the format specification fields of FORMAT statement. Time to time I see myself looking for its specification fields. PRINT, WRITE, READ and FORMAT are the ones that uses the formating specifications. Here is the list of the most important ones.

FORMAT SPECIFICATION FIELDS:

Format fieldPropertiesData type
Iw[.m]w - width
m - leading zeros
Integer values
Fw.dw - width
d - decimal point precision
Float values
Ew.d[Ee]w - width
d - decimal point precision
e - optional exponent width
Float values (Engineering mode)
Ew.d[Ee]w - width
d - decimal point precision
e - optional exponent width
Float values (Engineering mode)
Dw.dw - width
d - decimal point precision
Double precision values
Lww - widthLogical values
A[w]w - widthString values
Zww - widthHexadecimal values
Ow[.m]w - width
m - leading zeros
Octal values
"a" or 'a'a - string sequenceOutputs the string sequence
nXn - number of spacesOutputs n space characters
nHn - number of charsHollerith constant - Outputs the next n characters
$
Doesn't output line break


HOW TO USE:

How to use the format statement and its format specification fields? One way you declare a FORMAT statement is something like below:

r FORMAT (f1,f2,...,fn)

r - Label number
f1,f2,...,fn - format specification fields

Its easy to use it, the only thing you must remember is the label number r.

PRINT r,e1,e2,...,en
WRITE (unit, r) e1,e2,...,en
READ r,e1,e2,...,en
READ (unit, r),e1,e2,...,en

Where e1,e2,...,en are expressions matching each one a format specification field.

The other way to declare the format specification fields is explicitly writing it. You change the label number by a quoted format specifications fields like below.

PRINT '(f1,f2,...,fn)',e1,e2,...,en