Table Of ContentNUMERICAL MATHEMATICS AND SCIENTIFIC COMPUTATION
Series Editors
A. M. STUART E. SU¨LI
NUMERICAL MATHEMATICS AND SCIENTIFIC COMPUTATION
Books in the series
Monographsmarkedwithanasterisk(∗)appearedintheseries‘MonographsinNumericalAnalysis’
whichiscontinuedbythecurrentseries.
Forafulllistoftitlespleasevisit
http://www.oup.co.uk/academic/science/maths/series/nmsc
∗ J.H.Wilkinson:The algebraic eigenvalue problem
∗ I.Duff,A.Erisman,andJ.Reid:Direct methods for sparse matrices
∗ M.J.Baines:Moving finite elements
∗ J.D.Pryce:Numerical solution of Sturm-Liouville problems
C.Schwab:p- and hp- finite element methods: theory and applications in solid and fluid mechanics
J.W.Jerome:Modellingandcomputationforapplicationsinmathematics,science,andengineering
A.QuarteroniandA.Valli:Domain decomposition methods for partial differential equations
G.EmKarniadakisandS.J.Sherwin:Spectral/hp element methods for CFD
I.BabuˇskaandT.Strouboulis:The finite element method and its reliability
B.MohammadiandO.Pironneau:Applied shape optimization for fluids
S.Succi:The lattice Boltzmann equation: for fluid dynamics and beyond
P.Monk:Finite element methods for Maxwell’s equations
A.BellenandM.Zennaro:Numerical methods for delay differential equations
J.Modersitzki:Numerical methods for image registration
M.Feistauer,J.Felcman,andI.Straˇskraba:Mathematical and computational methods for
compressible flow
W.Gautschi:Orthogonal polynomials: computation and approximation
M.K.Ng:Iterative methods for Toeplitz systems
M.Metcalf,J.Reid,andM.Cohen:Fortran 95/2003 explained
G.EmKarniadakisandS.Sherwin:Spectral/hpelementmethodsforcomputationalfluiddynamics,
second edition
D.A.Bini,G.Latouche,andB.Meini:Numerical methods for structured Markov chains
H.Elman,D.Silvester,andA.Wathen:Finite elements and fast iterative solvers: with applications
in incompressible fluid dynamics
M.ChuandG.Golub:Inverse eigenvalue problems: theory, algorithms, and applications
J.-F.Gerbeau,C.LeBris,andT.Leli`evre:Mathematicalmethodsforthemagnetohydrodynamicsof
liquid metals
G. Allaire and A. Craig: Numerical analysis and optimization: an introduction to mathematical
modelling and numerical simulation
K.Urban:Wavelet methods for elliptic partial differential equations
B.MohammadiandO.Pironneau:Applied shape optimization for fluids, second edition
K.B¨ohmer:Numerical methods for nonlinear elliptic differential equations: a synopsis
M.Metcalf,J.Reid,andM.Cohen:Modern Fortran Explained
Modern Fortran Explained
Michael Metcalf
Formerly of CERN, Geneva, Switzerland
John Reid
JKR Associates, Oxfordshire
and
Malcolm Cohen
The Numerical Algorithms Group, Oxfordshire
1
3
GreatClarendonStreet,Oxfordox26dp
OxfordUniversityPressisadepartmentoftheUniversityofOxford.
ItfurtherstheUniversity’sobjectiveofexcellenceinresearch,scholarship,
andeducationbypublishingworldwidein
Oxford NewYork
Auckland CapeTown DaresSalaam HongKong Karachi
KualaLumpur Madrid Melbourne MexicoCity Nairobi
NewDelhi Shanghai Taipei Toronto
Withofficesin
Argentina Austria Brazil Chile CzechRepublic France Greece
Guatemala Hungary Italy Japan Poland Portugal Singapore
SouthKorea Switzerland Thailand Turkey Ukraine Vietnam
OxfordisaregisteredtrademarkofOxfordUniversityPress
intheUKandincertainothercountries
PublishedintheUnitedStates
byOxfordUniversityPressInc.,NewYork
(cid:2)c MichaelMetcalf,JohnReid,andMalcolmCohen2011
Themoralrightsoftheauthorshavebeenasserted
DatabaserightOxfordUniversityPress(maker)
Firsteditionpublished1987asFortran8xExplained
Secondeditionpublished1989
Thirdeditionpublished1990asFortran90Explained
Fourtheditionpublished1996asFortran90/95Explained
Fiftheditionpublished1999
Sixtheditionpublished2004asFortran95/2003Explained
Thiseditionpublished2011
Allrightsreserved.Nopartofthispublicationmaybereproduced,
storedinaretrievalsystem,ortransmitted,inanyformorbyanymeans,
withoutthepriorpermissioninwritingofOxfordUniversityPress,
orasexpresslypermittedbylaw,orundertermsagreedwiththeappropriate
reprographicsrightsorganization.Enquiriesconcerningreproduction
outsidethescopeoftheaboveshouldbesenttotheRightsDepartment,
OxfordUniversityPress,attheaddressabove
Youmustnotcirculatethisbookinanyotherbindingorcover
andyoumustimposethesameconditiononanyacquirer
BritishLibraryCataloguinginPublicationData
Dataavailable
LibraryofCongressCataloginginPublicationData
LibraryofCongressControlNumber:2010941705
PrintedinGreatBritain
onacid-freepaperby
CPIAntonyRowe,Chippenham,Wiltshire
ISBN 978–0–19–960141–7(Hbk)
978–0–19–960142–4(Pbk)
1 3 5 7 9 10 8 6 4 2
Preface
Fortran remains one of the principal languages used in the fields of scientific, numerical,
and engineering programming, and a series of revisions to the standard defining successive
versions of the language has progressively enhanced its power and kept it competitive with
severalgenerationsofrivals.
Beginning in 1978, the technical committee responsible for the development of Fortran
standards,X3J3(nowPL22.3butstillinformallycalledJ3),labouredtoproduceanew,much-
neededmodernversionofthelanguage,Fortran90. Itspurposewasto‘promoteportability,
reliability, maintainability, and efficient execution... on a variety of computing systems’.
Thatstandardwaspublishedin1991,andworkbeganin1993onaminorrevision,knownas
Fortran95.Subsequently,andwiththesamepurpose,afurthermajorupgradetothelanguage
was prepared by J3 and the international committee, WG5. This revision, which included
object-oriented programming features, is now known as Fortran 2003. This has now been
followedbyafurtherrevision,Fortran2008,and,onceagain,itseemsappropriatetoprepare
adefinitiveinformaldescriptionofthelanguagethatitdefines. Thiscontinuestheseriesof
editionsofthisbook–thetwoeditionsofFortran8xExplainedthatdescribedthetwodrafts
ofthestandard(1987and1989),Fortran90ExplainedthatdescribedtheFortran90standard
(1990),twoeditionsofFortran90/95ExplainedthatincludedFortran95too(1996and1999)
andFortran95/2003(2004),withitsaddedchaptersonFortran2003. Inthatfinalendeavour,
athirdco-authorwaswelcomed.
In this book, an initial chapter sets out the background to the work on new standards,
and the nine following chapters describe Fortran 95 (less its obsolescent features and the
redundantFortran77featureswhoseusewedeprecate)inamannersuitablebothforgrasping
the implications of its features, and for writing programs. We include the allocatable array
extensions that were originally published as an ISO Technical Report and are now part of
Fortran 2003, since they have been implemented in Fortran 95 compilers for many years.
Some knowledge of programming concepts is assumed. In order to reduce the number of
forwardreferencesandalsotoenable,asquicklyaspossible,usefulprogramstobewritten
basedonmaterialalreadyabsorbed,theorderofpresentationdoesnotalwaysfollowthatof
thestandard. Inparticular,wehavechosentodefertoappendicesthedescriptionoffeatures
that are officially labelled as redundant (some of which were deleted from the Fortran 95
standard) and other features whose use we deprecate. They may be encountered in old
programs,butarenotneededinnewones.
Chapter 11 describes another part of Fortran 2003 that was originally defined by an ISO
TechnicalReport.Thisisfollowed,inChapters12to17,bydescriptionsoftheotherfeatures
vi Preface
defined by the Fortran 2003 standard. Chapter 18 describes a part of Fortran 2008 that
was originally defined by an ISO Technical Report and two further chapters describe the
other new features of Fortran 2008. The structure of the book thus allows the reader to
distinguishclearlybetweenFortran95(plusallocatablearrayextensions),Fortran2003,and
thenewFortran2008features. Notethat,apartfromasmallnumberofdeletions,eachofthe
languagesFortran77, Fortran90, Fortran95, Fortran2003, andFortran2008isasubsetof
itssuccessor.
Inordertomakethebookacompletereferencework,itconcludeswithsevenappendices.
Theycontain,successively,alistoftheintrinsicprocedures,adescriptionofvariousfeatures
whose use we deprecate and do not describe in the body of the book, a description of
obsolescent and deleted features, advice on avoiding compilation cascades, an extended
exampleillustratingtheuseofobjectorientation,aglossaryofFortranterms,andsolutions
tomostoftheexercises.
Itisourhopethatthisbook,byprovidingcompletedescriptionsofFortran95,Fortran2003
and Fortran 2008, will continue the helpful role that earlier editions played for the
correspondingversionsofthestandard, andthatitwillserveasalong-termreferencework
forthemodernFortranprogramminglanguage.
∗∗∗
MalcolmCohenwishestothanktheNumericalAlgorithmsGroup(NAG)foritsencour-
agementduringthewritingofthisbook.
Conventionsusedinthisbook
Fortrandisplayedtextissetintypewriterfont:
integer :: i, j
andalineconsistingofacolonindicatesomittedlines:
subroutine sort
:
end subroutine sort
InformalBNFtermsareinitalics:
if (scalar-logical-expr) action-stmt
Squarebracketsinitalicsindicateoptionalitems:
end if [name]
andanellipsisrepresentsanarbitrarynumberofrepeateditems:
[ case selector [name]]
block] ...
Theitalicletterbsignifiesablankcharacter.
Corrections to any significant errors detected in this book will be made available in the
filesedits.psandedits.pdf atftp://ftp.numerical.rl.ac.uk/pub/MRandC.
This page intentionally left blank
Contents
1 WhenceFortran? 1
1.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Fortran’searlyhistory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.3 ThedrivefortheFortran90standard . . . . . . . . . . . . . . . . . . . . . . 3
1.4 Languageevolution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.5 Fortran95 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.6 ExtensionstoFortran95 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.7 Fortran2003. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.8 Fortran2008. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.9 Conformance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2 Languageelements 9
2.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.2 Fortrancharacterset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.3 Tokens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.4 Sourceform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.5 Conceptoftype . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.6 Literalconstantsofintrinsictype . . . . . . . . . . . . . . . . . . . . . . . . 14
2.6.1 Integerliteralconstants . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.6.2 Realliteralconstants . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.6.3 Complexliteralconstants. . . . . . . . . . . . . . . . . . . . . . . . 17
2.6.4 Characterliteralconstants . . . . . . . . . . . . . . . . . . . . . . . 17
2.6.5 Logicalliteralconstants . . . . . . . . . . . . . . . . . . . . . . . . 19
2.7 Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
2.8 Scalarvariablesofintrinsictype . . . . . . . . . . . . . . . . . . . . . . . . 20
2.9 Deriveddatatypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
2.10 Arraysofintrinsictype . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
2.11 Charactersubstrings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
2.12 Objectsandsubobjects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
2.13 Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
2.14 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
3 Expressionsandassignments 33
3.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33