Data Types and Declarations

Data Types

FORTRAN 77 supports six data types:

CHARACTER
character
COMPLEX
single precision complex number
DOUBLE PRECISION
double precision floating point number
INTEGER
integer
LOGICAL
boolean (true or false)
REAL
single precision floating point number

Numerical data in FORTRAN 77 can be represented in one of four types. An INTEGER is any signed number that has no fractional part and no decimal point.

Example

INTEGER numbers are also referred to as fixed point numbers. The other three numerical types are called floating point numbers. A REAL number is a signed number with a decimal point.

Example

Very large or very small numbers are often represented in scientific notation. In this notation, a number is represented as

±b × 10±n

where b is a number between 1 and 10 and n is the appropriate power of ten. FORTRAN 77 offers a similar representation called exponential notation:

±0.mE±p

In this case, the mantissa m is a number between 0.1 and 1.0 and p is again the appropriate power of ten.

Example

Decimal Scientific Exponential
0.000135 1.35 × 10-4 0.135E-03
-246.8 -2.468 × 102 -0.2468E+03
235700000000000000000.0 2.357 × 1020 0.2357E+21

A number stored in a computer is limited in magnitude and precision. The limits depend on the particular computer. Thus, a REAL number has only a certain number of significant digits. If more significant digits are required for a calculation, then DOUBLE PRECISION numbers must be used. A DOUBLE PRECISION constant is written in the same exponential form as a single precision REAL constant except with a D instead of an E separating the mantissa from the exponent.

In practice, most computers use 32 bits to store INTEGER and REAL numbers. This means that an INTEGER is limited to numbers between -2,147,483,648 and +2,147,483,647 (a sign bit and 31 magnitude bits). If the IEEE standard is used, then a REAL number will have about seven decimal digits and be within the magnitude range of 10-38 to 10+38. DOUBLE PRECISION numbers usually have at least twice the number of significant decimal digits and a magnitude range of 10-308 to 10+308.

Complex numbers are common in many fields of science and engineering so it is not surprising that FORTRAN 77 offers a COMPLEX data type. The complex number a+ib where i is the imaginary unit (square root of -1) is represented in FORTRAN 77 as (a,b) where a and b themselves are single precision REAL numbers. There are no double precision complex numbers available in FORTRAN 77.

The other two data types deal with non-numerical information. A LOGICAL value is either .TRUE. or .FALSE. (note the full stops!) whilst a CHARACTER value can contain any combination of characters from the FORTRAN 77 character set. In fact, on most computers, a CHARACTER string can contain any combination of printable characters. A CHARACTER constant is any set of characters enclosed in apostrophes. If an apostrophe is needed as part of the string, then two apostrophes (not a double quote) are used.

Example

CHARACTER Constant Result Length
'a character string' a character string 18
'Let''s go!' Let's go! 9
' $ 10.25 '  $ 10.25  9
' ' 1

Type Declarations

The type of any constant, variable or array used in a FORTRAN 77 program must be specified either implicitly or explicitly. In implicit typing, all constants, variables and arrays beginning with the letters I, J, K, L, M, or N are automatically taken to be of type INTEGER. Constants, variables and arrays beginning with all other letters are assumed to be REAL. Thus, with implicit typing, the variable COUNT is REAL whilst the variable KOUNT is an INTEGER. Implicit typing can be overridden with explicit type declaration. To explicitly declare a constant, variable or array to be of a given type, simply put a statement of the form

type name-list

where type is one of the six data types and the name-list is a list of the names of the constants, variables or arrays of the chosen type separated by commas.

Example

The declarations

      COMPLEX          FALL,TRIP
      DOUBLE PRECISION BIGJMP
      INTEGER          A,AA,AAA
      LOGICAL          DECIDE
      REAL             BOUND,JUMP,LEAP

at the beginning of a program unit define the variables named FALL and TRIP to be of type COMPLEX; BIGJMP to be DOUBLE PRECISION; A, AA, and AAA to be INTEGER; DECIDE to be LOGICAL; and BOUND, JUMP, and LEAP to be REAL.

CHARACTER declarations are a little more subtle in that they require prior knowledge of the length of the string that will be stored in the CHARACTER variable. The syntax for a CHARACTER variable is

CHARACTER*m variable-list

where all of the variables in the variable-list are m characters long. Some or all of these characters may be blank. It is also possible to use one declaration statement to specify several variables of different lengths:

CHARACTER variable1*m1, variable2*m2, …, variablen*mn

In this case, the first variable is of length m1, the second variable is of length m2 and so on.

Example

The declarations

      CHARACTER*3 CONST,GREEK
      CHARACTER   CATLOG*10,NAME*20

at the beginning of a program unit define CONST and GREEK to be CHARACTER variables of length 3 whilst CATLOG is of length 10 and NAME is of length 20.

IMPLICIT Statement

Although only INTEGER and REAL constants, variables and arrays have implicit types, it is possible to assign defaults for all data types through the use of the IMPLICIT statement. The syntax for this statement is

IMPLICIT type1 (range1), type2 (range2), …, typen (rangen)

Explicit type declarations override implicit type declarations.

Example

A program that has only DOUBLE PRECISION variables might contain the statement

      IMPLICIT DOUBLE PRECISION(A-Z)

at the beginning.

Example

Because of the declarations

      IMPLICIT COMPLEX(A-C),DOUBLE PRECISION(D),INTEGER(E-Z)
      LOGICAL  HELP

all variables beginning with the letters A through C are of type COMPLEX, all variables beginning with the letter D are of type DOUBLE PRECISION, and everything else is an INTEGER. The explicit type declaration that HELP is of type LOGICAL overrides the INTEGER default.