FORTRAN Format

FORTRAN formats are used to control the ap

p earance of printed output Its has the follow

ing form

format edit descriptors

There are three dierentways of using format

CHARACTERLEN PARAMETER FMT IF

CHARACTERLEN PARAMETER FMT I E F

CHARACTERLEN STRING

READ Form

WRITE

READFMT Form

WRITEFMT

READFMT

WRITEFMT

STRING I F

READSTRING Form WRITESTRING

Format Edit Descriptors

There are several commonly used edit descriptors where

w numb er of p ositions m minimum number of po

sitions d number of digits to the right of the

p oint and e numb er of digits in the exp onential part

For readingwri ti ng Iw and Iwm

For readingwri ti ng REALs

a Fwd

b Scienti notation Ewd and EwdEe

For readingwri ti ng CHARACTERs A and Aw

For readingwri ti ng LOGICALs Lw

For horizontal spacing nX

For vertical spacing

For tabbing Tc TLc and TRc

Edit descriptors are separated by commas

CHARACTERLEN FRMT

FRMT X I F A

WRITEFRMT

READFRMT

Integer Output

Editing Descriptors r Iw and r Iwm

I output

w width of eld iethenumb er of p ositions

m minimum numberofdigitstobeprinted

r rep etition indicator I is equivalentto

I I I

INTEGER A B

A

B

WRITE I A

WRITE I A

WRITE I A

WRITE I A

WRITE I

WRITE I B

WRITE I B

WRITE I B

WRITE I B

Real Output the F Descriptor

Editing Descriptors r Fwd

F REAL output

w width of eld iethenumb er of p ositions

d numb er of digits to the right of the decimal p oint

r rep etition indicator F is equivalentto

F F F

IMPORTANT the integer part has only w  d

p ositions to use for decimal p oint and for sign

REAL A B

A

WRITE F A

WRITE F A

WRITE F A

WRITE F A

WRITE F A

WRITE F A

WRITE F A

WRITE F A

WRITE F A

WRITE F A

WRITE F A

Real Output the E Descriptor

Editing Descriptors r Ewd and r EwdEe

E REAL output

w width of eld iethenumb er of p ositions

d numb er of digits to the right of the decimal p oint

e numb er of p ositions for exp onent

r rep etition indicator E is equivalentto

E E E

Before printing the number to be IMPORTANT

n

printed is converted to dddd  The actual

n

printout is dddd  rather than the original

For Ewd the w p ositions are arranged as follows

Thus w  d

d

E

w

For EwdEethew p ositions are arranged as follows

Thus w  d e

d e

E

w

REAL PI

WRITE E PI

WRITE EE PI

WRITE EE PI

E

E

E

Logical Output

Editing Descriptors r Lw

L LOGICAL output

w width of eld iethenumb er of p ositions

r rep etition indicator L is equivalentto

L L L

IMPORTANT the output of TRUE and FALSE

are T and F resp ectively and are right justied

LOGICAL A B

A TRUE

B FALSE

WRITELL A B T F

WRITELL A B T F

Character Output

Editing Descriptors r A and r Aw

A CHARACTER output

w width of eld iethenumb er of p ositions

r rep etition indicator A is equivalentto

A A A

IMPORTANT

If w is missing the length of the character value is

used

If w is larger than the length of the character value

that value is right justied

If w is smaller than the length of the character

value that value is truncated ie only the left

most w characters will b e printed

CHARACTERLEN A

CHARACTERLEN B

A ABCDE

B XYZ

WRITEAA A B ABC XYZ

WRITEAA A B ABCXYZ

WRITEAA A B ABCDE XYZ

WRITEA A B ABCDEXYZ

Spacing and Tabbing

Editing Descriptors nX and Tc TLc and TRc

nX insert n spaces

Tc move to p osition c

TLc movebackward c p ositions

TRc moveforward c p ositions

IMPORTANT

No rep etitio n can b e used

Nothing will b e printed

However the p osition for printing next data item

will b e aected

INTEGER A B

A

B

WRITE XIXI A B

XIXI

INTEGER A B

A

B

WRITETITI A B

INTEGER A B C

A

B

C

WRITE TITLITRI A B C

T

I

TL

I

TR

I the result you see

Printer Control

you want to print on a This is necessary ONLY IF

printer

First Char on a Line Eect

space Advance to next line BEFORE printing

Advance two lines BEFORE printing

Advance to next page BEFORE printing

No advancing print on the current line overprinting

Watch out the FIRST Character You Print

INTEGER A B

WRITE I A first space for advancing

WRITE I B

first line on next page in for form feed

The Slash Descriptor

Advance to next line

Between two adjacent s no comma is required

is the same as

INTEGER A B

CHARACTERLEN C ABCDEFGH

CHARACTERLEN ABC

CHARACTERLEN TITLE

WRITE XIITA A B C

ABCDEFGH

TITLE TITLE

ABC ABC

WRITEX A A TITLE ABC

TITLE ABC

next WRITE prints here

The Grouping Descriptor r 

The descriptors within are rep eated r times

X I F A

I F means I F is rep eated three times

Thus the ab ove is equivalentto

X I F I F I F A

Grouping can b e nested

X I I A A

The ab ove is equivalentto

X I I A

I I A

I I A A

which in turn is equivalentto

X I I A I A

I I A I A

I I A I A A

A group with rep etition factor r has

sp ecial meaning

Example

Computing means revisited

PROGRAM Means

IMPLICIT NONE

REAL a b c

REAL Am Gm Hm

CHARACTERLEN PARAMETER FMT X AX A F

READ a b c

Am a b c

Gm a b c

Hm a b c

WRITEFMT Input Data

a a

b b

c c

WRITEFMT Computed Results

Mean Am

Geometric Mean Gm

Harmonic Mean Hm

END PROGRAM Means

Input Data

a

b

c

Computed Results

Arithmetic Mean

Geometric Mean

Harmonic Mean

Example

Printamultiplication table

PROGRAM Table

IMPLICIT NONE

INTEGER PARAMETER MAX

INTEGER i j

CHARACTERLEN FORMAT

FORMAT X I A I A I

DO i MAX

WRITEFORMAT i j ij j MAX

END DO

END PROGRAM MultiplicationTable

                

             

            

           

           

           

           

          

          

Matching Data Values with

Edit Descriptors

When a WRITE starts its format is scanned from left

to right

Edit descriptors are pro cessed until a descriptor that

requires a data value ie I F E L and A is encoun

tered

Take next data item and use the encountered edit

descriptor for printing the value

The typ e of the value and the edit descriptor MUST

match I for INTEGER F and E for REAL L for LOGICAL

and A for CHARACTER

After pro cessing a value go back to step

This pro cess will continue until encounters the right

parenthesis

Matching Data Values with

Edit Descriptors

If the rightmost right parenthesis is reached

If there is no data items left we are done

If there are unpro cessed data items then the format

edit descriptors will b e rescanned

a If there is no grouping parenthesis in a format

then start a new line and rescan the format from

the very b eginning

INTEGER A B C

INTEGER D E F

WRITE X I A B C D E F

Output

b If there are grouping parenthesis then nd the

rightmost rightparenthesis in the format and its

matching left parenthesis and start rescan there

Note that any rep etition preceding this group is in

eect

INTEGER DIMENSION A

INTEGER i

DO i

Ai i

END DO

WRITE X II I Ai i

Output

c Continue this pro cess until all data items are printed

Example

Printanarray on a single column

PROGRAM SingleColumn

IMPLICIT NONE

INTEGER PARAMETER MAXSIZE

INTEGER DIMENSIONMAXSIZEx

INTEGER i

CHARACTERLEN FMT

DO i MAXSIZE

Xi MODi i

END DO

FMT T A T A T A T AT I T I

WRITEFMT Generated Table

No Data

xi i MAXSIZE

END PROGRAM SingleColumn

Generated Table

No Data

Example

Printanarray six elements p er row

PROGRAM SixPerRow

IMPLICIT NONE

INTEGER PARAMETER SIZE

INTEGER DIMENSIONSIZEx

INTEGER i

CHARACTERLEN String

String X A X A X I

DO i SIZE

xi MODi i

END DO

WRITEString Generated Table

xi i SIZE

END PROGRAM SixPerRow

Generated Table

Integer Input

Editing Descriptors r Iw

I INTEGER input

w width of eld iethenumb er of p ositions

m minimum numberofdigitstobeprinted

r rep etition indicator I is equivalentto

I I I

The content in the next w p ositions is considered to

b e an integer whose value go es into the corresp onding

variable

All blanks in the eld of w p ositions are IGNORED

by default

INTEGER A B C D

READI A B C D

A B C D

A B C and D receives and resp ectively

Real Input

Editing Descriptors r Fwd

F REAL input

w width of eld iethenumb er of p ositions

d numb er of digits to the right of the decimal p oint

r rep etition indicator F is equivalentto

F F F

Three cases are p ossible

If there is no decimal p oint in the eld the right

most d p ositions are considered to the rightofthe

decimal p oint

REAL A B C

READ F A B C

A B C

The rst eld is the second is and the

third is Taking the last two digits to b e the

part A B and C are and

If the input has a decimal p oint the actual value

will b e taken and the d in Fwd has no eect

REAL A B C

READ F A B C

A B C

A B and C receives and

You can use scientic notation by adding an E fol

lowed by an exp onent The remaining part is han

dled by the ab ovetwo rules based on if a decimal

point presents

REAL A B

READ F A B E E

A B

The eld for A after removing spaces is E

Since the edit descriptor is Fthe part is

read as two decimal places and the actual

value is  Thus A receives and B

has 

Logical Input

Editing Descriptors r Lw

L LOGICAL input

w width of eld iethenumb er of p ositions

r rep etition indicator L is equivalentto

L L L

The leftmost T or F in the eld yields the value of

TRUE or FALSE

LOGICAL A B C D

READL A B C D T F ABT XYFT

A and B receive TRUE and FALSE For Citsinput

eld consists of a blank character Acharacter B and char

acter T Thus C receives TRUE Similarly D receives

FALSE

Character Input

Editing Descriptors r A and r Aw

A CHARACTER input

w width of eld iethenumb er of p ositions

r rep etition indicator A is equivalentto

A A A

IMPORTANT

If w is missing the length of the CHARACTER vari

able is used

If w is larger than the length of the CHARACTER

variable only the rightmost part is taken and put

into the variable

If w is smaller than the length of the CHARACTER

variable spaces will be added and put into the

variable

CHARACTERLEN A B C

READA A LMNOP

READA B LMNOP

READA C LMNOPQ

A receives LMNO B receives LMNOP and C receives

MNOPQ

Spacing and Tabbing

Editing Descriptors nX and Tc Note that no rep etition

can b e used

nX insert n spaces

Tc move to p osition c

INTEGER A B C D

READXITIXITI A B C D

Input

X

I A receives

T

I B receives

X

I C receives

T

I D receives

The Slash Descriptor

Advance to next input line The remaining input on

the current line will b e IGNORED

Between two adjacent s no comma is required

is the same as

INTEGER A B C D

READITIII A B C D

Input I A receives

T I B receives

I C receives

I D receives

next READ starts here

Example

Read in twoarrays and compute their inner pro duct

PROGRAM InnerProduct

IMPLICIT NONE

INTEGER PARAMETER SIZE

INTEGER Number Sum i

INTEGER DIMENSIONSIZExy

CHARACTERLEN Fmt Fmt Fmt

Fmt II

Fmt XAXAXI

Fmt X A I

READFmt Number xi yi i Number

WRITEFmt Input Data No X Y

i xi yi i Number

Sum

DO i Number

Sum Sum xiyi

END DO

WRITEFmt Input product Sum

END PROGRAM InnerProduct

Input Output

Input Data

No X Y

Input product