To these were added DOUBLE PRECISION and COMPLEX as numeric types, and LOGICAL and CHARACTER as non-numeric types. In Fortran 90/95, DOUBLE PRECISION is being replaced by an extended precision REAL, but may still be used for compatibility with older code.
In Fortran-90/95, identifiers can be up to 31 characters long. (cf. EF 2.1) Fortran-2003 now allows 63 characters. The first character must be a letter of the alphabet and the other characters may be either letters or numbers. The underline is permitted after the first character, but no other special character is permitted.
Variable identifiers for FORTRAN-77 and earlier versions could be at maximum 6 characters long, NO underline was allowed nor were any special characters allowed. (cf. EF 2.1)
In FORTRAN-77 or earlier, one could use blanks within variables, but this is not permitted in the new Free Source Code format in Fortran-90/95/2003.
There is NO difference between upper case and lower case letters. (cf. EF 1.2)
If a variable is NOT declared, it is given a default type based on the first letter of the identifier:
IMPLICIT NONE
immediately before any declaration statement and after the initial
PROGRAM (or similar) line of the segment. (cf. EF 2.2 [note 1], 2.5)
One declares variables in a manner similar to the style of C/C++ by indicating the type first and the list of identifiers of that type afterwards. For example,
INTEGER A, B
REAL C, D
CHARACTER*8 J, K !indicating storage of 8-character strings
Fortran-90/95 permits a newer style (which allows including additional
attributes). (NOTE the addition of the double colon after the data type.)
INTEGER :: A, B
REAL :: C, D
CHARACTER (LEN=8) :: J, K
IMPORTANT NOTE:
In Fortran, all declarations must occur before any
executable statement (e.g., assignment, input/output, subroutine
call). Unlike C++, there can be no declarations in the body of the
program, after the executable code has begun.
The new development of "compiler theory" indicated that early versions of FORTRAN was a disaster for code translation!
FORTRAN was commonly blamed for a space disaster because of the "default type" declaration feature. (Although this "blame" is thought to be false nowadays.) The first U.S. Mariner I rocket, launched on July 22, 1962 from Cape Canaveral on the first mission to Venus, was destroyed at a cost of $18.5 million because it started veering toward populated areas. The blame was commonly placed on a mistake by a programmer. It was commonly said that instead of writing
DO 3 I=1,3 ! comma between 1 and 3
a programmer had written
DO 3 I=1.3 ! period between 1 and 3
The first statement is a legitimate initial statement for a counted
loop, but the second statement is a legitimate assignment statement
giving the REAL variable DO3I the value of 1.30.
Arithmetic operators are as in C/C++, i.e., +, -, *, /. As in C/C++, the division operator, /, is used for both real and integer division with the expected output. e.g., 5/8 gives the value of 0 but 5.0/8 gives the value of 0.625. (cf. DCS 2.6)
Fortran has always included the ** operator for exponentiation. Some local variants also permit the caret, ^, as an alternative. Remember that ** has higher precedence than * or /, and that standard rules of arithmetic means that A=B**C**D is evaluated RIGHT TO LEFT as far as the repeated use of ** is concerned. In other words, B**C**D means B**(C**D). If there is any chance of confusion, one should always use parentheses.
There is an older routine that can be used as a "cast" function to convert between REAL and INTEGER, which is named IFIX, (e.g., I = IFIX(X)), but a simple assignment also will perform the necessary conversion between numeric data types. For example, if IA is of type INTEGER, then
IA = 3.1415926
means that IA will receive the value of 3.
READ(idevice,iformat) var_list
For OUTPUT:
WRITE(idevice,iformat) var_list
idevice is the "device" number which historically was
required to distinguish between different input/output devices on single user
machines.
We can either use an INTEGER variable which is assigned a specific value, or use the actual number in the READ/WRITE statements.
We use 5 in a READ statement to indicate that input comes from the keyboard.
We use 6 in a WRITE statement to indicate that output goes to the screen.
NOTE: Unlike C++ cin and cout, in Fortran, each occurrence of a READ or WRITE starts its action on a NEW line. (In the case of a READ, whatever was left on the previous line that was not read is ignored.)
On other systems (e.g., some PC compilers) a similar type of association occurs except the filename is slightly different, e.g., 20 is associated with file fort.20.
This simplifies your work (life?), but makes it impossible to control how the output looks.
E.g.,
READ(5,*) A,B,J
Like C++, this takes the next numeric data and puts it into the
next variable, skipping over "white space."
WRITE(6,*) I,A,B
I, A, B are written out according to default field widths,
which vary from compiler to compiler.
WRITE(6,*) "Hello", X
NOTE: NO "carriage control" exists with Format-Free I/O (to
be covered later). In other
words, the first item in a WRITE statement actually appears in
column 1 and "column 0" is left blank. Also, Format-Free I/O always leaves
at least one blank between successive items in a WRITE statement.
FORMAT statements MUST have a statement number. (Cf. EF 1.3 note 2, for use of statement numbers in Free Source Code Form. The statement number can be placed anywhere, and is no longer restricted to the first 5 columns of a line.)
E.g.,
100 FORMAT( specification_list )The specification_list is a list of "field descriptors," separated by commas. The function is vaguely similar to what the setw(n) and cin.precision(n) statements can do in stream i/o in C++, or to what the descriptors do in standard C i/o.
Fundamentally, these field descriptors describe the input/output line, SPACE BY SPACE!!!
Each FORMAT statement describes at least one line.
FORMAT statments can be put (physically) anywhere in the program. In general there are two styles:
==> Also note that each READ/WRITE begins its action on a NEW line.
The basic FIELD DESCRIPTORS are: [EF 3.5]
In for Integers:
n indicates the number of spaces needed.Ew.d Real number in Exponential form:
w corresponds to the total number of spaces needed.
d corresponds to the number of digits to the right of the decimal point.
An E descriptor (for output) needs one space for the letter E, three spaces for the exponent and its sign, three more spaces for the decimal point, an initial zero before the decimal point, and a possible minus sign. Thus, in general (for output), w should be greater than or equal to d + 7.Fw.d Real number in Fixed form:
w corresponds to the total number of spaces needed.
d corresponds to the number of digits to the right of the decimal point.
PROGRAM TEST1
REAL A,B
INTEGER I
A = 2.0
B = 5.321
I = 126
WRITE(6,121) A,I,B
121 FORMAT(F6.3,I5,F6.2)
STOP
END
The output would look as follows. For several reasons, to be
explained later, we label the first output column as 0 or "cc" (for
"carriage control"). On some systems, what goes in this column will
not be printed and
may not appear on the terminal screen, but may be used by the printer,
and thus needs to be taken into account when determining
what the output looks like. The actual effect may vary depending on the compiler
and version of Fortran used. Fortran 2003 is phasing out the use of
"carriage control."
OUTPUT:
field descript>>|----F6.3-------| |----I5------| |-----F6.2------| column number>> 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 output char's>> 2 . 0 0 0 1 2 6 5 . 3 2There is an alternative method to describe the FORMAT spacing which is permitted in FORTRAN 77 and encouraged in Fortran-90/95/2003. This method avoids the use of an independent FORMAT statement (and eliminates the need to use a statement number) (cf. EF 3.9). In this case one includes the formatting field descriptors in the WRITE statement where the statement number would normally appear. One must use the parentheses and quote marks. For example, the WRITE statement and the corresponding FORMAT statement in the example above could be combined as follows:
WRITE(6, "(F6.3,I5,F6.2)") A,I,B
Repetition factors are numbers placed before a field descriptor which "multiply" their presence, i.e., 3I2 is equivalent to I2,I2,I2 (cf. EF 3.10).
nX Blank Spacing:
The number n before the X indicates how many blanks should be left (on output), or spaces skipped (on input).
E.g., 3X indicates 3 blank spaces.'string'
or
"string"
Literal string:
The string contained between the single or double quotes will be printed out literally.
Note: in FORTRAN-77 only the single quote (normally) was permitted. To include a single quote within a string, one was required to use two single quotes next to each other.
The older "Hollerith" descriptor is discussed at the end of EF 3.6./ End of Record separator:
On Output: the slash will force a new line and print any data associated with subsequent descriptors in the FORMAT statement to be printed on next line (assuming the new carriage control does not force some other action).
On Input: the slash will cause the rest of the input line to be ignored and start reading data from the next input line. Slashes can be used instead of commas, and more than one can be indicated together.
(This is similar to the endl in C++ or the \n in C.)
For ease of intepretation, I suggest thinking that an output line begins with column 0 or column "cc" (carriage control) and the first VISIBLE column is the next column, i.e., column 1. Whatever appears in column 0 is not printed but is intepreted.
To avoid unintended things from happening on output, common practice is to begin output FORMAT statements with a 1X as the first field descriptor.
blank the rest of the line is printed on the same line 1 a "top-of-form" (page eject) is performed before the remaining information is printed (ignoring column 0 on the top of the next page). 0 "double space," i.e., a line is skipped before the rest of the line is printed (ignoring column 0 on the next line). + the previous line is overstruck (double printing).
NOTE: Most other characters are ignored (i.e., are intepreted as if they were a blank).
{7+}
PROGRAM EXAMPLE2
INTEGER I,J,K
REAL A,B,C
A = 123.45 ; B = 6.7
C = .89 ; I = 1
J = 234; K = 56789
WRITE(6,521) A,I,B,J,C,K
521 FORMAT(1X,F10.3,I5///'0',F8.2,
1 2X,I5,///1H ,'C=',E10.3,I7)
STOP
END
Assuming the older Fixed Source Code format, we indicate that the
FORMAT statement continues onto another line by putting a 1 in
column 6 of the second line.
col. num.>> 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
________________________________________________________
|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|
|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|
|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|
|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|
|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|
|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|
|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|
|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|
|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|
|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|
This page is maintained by Dennis C. Smolarski, S.J.
dsmolarski@math.scu.edu
© Copyright 1998, 1999, 2006 Dennis C. Smolarski, SJ, All rights reserved.
Last changed: 18 January 2006.