FORTRAN 77 Language Reference

Types

Except for specifically typeless constants, any constant, constant expression, variable, array, array element, substring, or function usually represents typed data.

On the other hand, data types are not associated with the names of programs or subroutines, block data routines, common blocks, namelist groups, or structured records.

Rules for Data Typing

The name determines the type; that is, the name of a datum or function determines its data type, explicitly or implicitly, according to the following rules of data typing;

Array Elements

An array element has the same type as the array name.

Functions

Each intrinsic function has a specified type. An intrinsic function does not require an explicit type statement, but that is allowed. A generic function does not have a predetermined type; the type is determined by the type of the arguments, as shown in Chapter 6, Intrinsic Functions .

An external function can have its type specified in any of the following ways:

Example: Explicitly by putting its name in a type statement:


	FUNCTION F ( X ) 
	INTEGER F, X 
	F = X + 1 
	RETURN 
	END

Example: Explicitly in its FUNCTION statement:


	INTEGER FUNCTION F ( X ) 
	INTEGER X 
	F = X + 1 
	RETURN 
	END 

Example: Implicitly by its name, as with variables:


	FUNCTION NXT ( X ) 
	INTEGER X 
	NXT = X + 1 
	RETURN 
	END 

Implicit typing can affect the type of a function, either by default implicit typing or by an IMPLICIT statement. You must make the data type of the function be the same within the function subprogram as it is in the calling program unit. The f77 compiler does no type checking across program units.

Properties of Data Types

This section describes the data types in Sun FORTRAN 77.

Default data declarations, those that do not explicitly declare a data size can have their meanings changed by certain compiler options. The next section, "Size and Alignment of Data Types " summarizes data sizes and alignments and the effects of these options.

BYTE @

The BYTE data type provides a data type that uses only one byte of storage. It is a logical data type, and has the synonym, LOGICAL*1.

A variable of type BYTE can hold any of the following:

If it is interpreted as a logical value, a value of 0 represents .FALSE., and any other value is interpreted as .TRUE.

f77 allows the BYTE type as an array index, just as it allows the REAL type, but it does not allow BYTE as a DO loop index (where it allows only INTEGER, REAL, and DOUBLE PRECISION). Wherever the compiler expects INTEGER explicitly, it will not allow BYTE.

Examples:


	BYTE 	Bit3 / 8 /, C1 / 'W' /, 
&	 	Counter / 0 /, Switch / .FALSE. / 

A BYTE item occupies 1 byte (8 bits) of storage, and is aligned on 1-byte boundaries.

CHARACTER

The character data type, CHARACTER, which has the synonym, CHARACTER*1, holds one character.

The character is enclosed in apostrophes (') or quotes ("). @ Allowing quotes (") is nonstandard; if you compile with the -xl option, quotes mean something else, and you must use apostrophes to enclose a string.

The data of type CHARACTER is always unsigned. A CHARACTER item occupies 1 byte (8 bits) of storage and is aligned on 1-byte boundaries.

CHARACTER*n

The character string data type, CHARACTER*n, where n > 0, holds a string of n characters.

A CHARACTER*n data type occupies n bytes of storage and is aligned on 1-byte boundaries.

Every character string constant is aligned on 2-byte boundaries. If it does not appear in a DATA statement, it is followed by a null character to ease communication with C routines.

COMPLEX

A complex datum is an approximation of a complex number. The complex data type, COMPLEX, which defaults to a synonym for COMPLEX*8, is a pair of REAL*4 values that represent a complex number. The first element represents the real part and the second represents the imaginary part.

The default size for a COMPLEX item (no size specified) is 8 bytes. The default alignment is on 4-byte boundaries. However, these defaults can be changed by compiling with certain special options (see "Size and Alignment of Data Types ").

COMPLEX*8 @

The complex data type COMPLEX*8 is a synonym for COMPLEX, except that it always has a size of 8 bytes, independent of any compiler options.

COMPLEX*16 (Double Complex) @

The complex data type COMPLEX*16 is a synonym for DOUBLE COMPLEX, except that it always has a size of 16 bytes, independent of any compiler options.

COMPLEX*32 (Quad Complex) @

(SPARC only) The complex data type COMPLEX*32 is a quadruple-precision complex. It is a pair of REAL*16 elements, where each has a sign bit, a 15-bit exponent, and a 112-bit fraction. These REAL*16 elements in f77 conform to the IEEE standard.

The size for COMPLEX*32 is 32 bytes.

DOUBLE COMPLEX @

The complex data type, DOUBLE COMPLEX, which usually has the synonym, COMPLEX*16, is a pair of DOUBLE PRECISION (REAL*8) values that represents a complex number. The first element represents the real part; the second represents the imaginary part.

The default size for DOUBLE COMPLEX with no size specified is 16.

DOUBLE PRECISION

A double-precision datum is an approximation of a real number. The double-precision data type, DOUBLE PRECISION, which has the synonym, REAL*8, holds one double-precision datum.

The default size for DOUBLE PRECISION with no size specified is 8 bytes.

A DOUBLE PRECISION element has a sign bit, an 11-bit exponent, and a 52-bit fraction. These DOUBLE PRECISION elements in f77 conform to the IEEE standard for double-precision floating-point data. The layout is shown in Appendix C, Data Representations .

INTEGER

The integer data type, INTEGER, holds a signed integer.

The default size for INTEGER with no size specified is 4, and is aligned on 4-byte boundaries. However, these defaults can be changed by compiling with certain special options (see "Size and Alignment of Data Types ").

INTEGER*2 @

The short integer data type, INTEGER*2, holds a signed integer. An expression involving only objects of type INTEGER*2 is of that type. Using this feature may have adverse performance implications, and we do not recommend it.

Generic functions return short or long integers depending on the default integer type. If a procedure is compiled with the -i2 flag, all integer constants that fit and all variables of type INTEGER (no explicit size) are of type INTEGER*2. If the precision of an integer-valued intrinsic function is not determined by the generic function rules, one is chosen that returns the prevailing length (INTEGER*2) when the -i2 compilation option is in effect. With -i2, the default length of LOGICAL quantities is 2 bytes.

Ordinary integers follow the FORTRAN rules about occupying the same space as a REAL variable. They are assumed to be equivalent to the C type long int, and 2-byte integers are of C type short int. These short integer and logical quantities do not obey the standard rules for storage association.

An INTEGER*2 occupies 2 bytes.

INTEGER*2 is aligned on 2-byte boundaries.

INTEGER*4 @

The integer data type, INTEGER*4, holds a signed integer.

An INTEGER*4 occupies 4 bytes.

INTEGER*4 is aligned on 4-byte boundaries.

INTEGER*8 @

The integer data type, INTEGER*8, holds a signed 64-bit integer.

An INTEGER*8 occupies 8 bytes.

INTEGER*8 is aligned on 8-byte boundaries.

LOGICAL

The logical data type, LOGICAL, holds a logical value .TRUE. or .FALSE. The value 0 represents .FALSE.; any other value represents .TRUE.

The usual default size for an LOGICAL item with no size specified is 4, and is aligned on 4-byte boundaries. However, these defaults can be changed by compiling with certain special options.

LOGICAL*1 @

The one-byte logical data type, LOGICAL*1, which has the synonym, BYTE, can hold any of the following:

The value is as defined for LOGICAL, but it can hold a character or small integer. An example:


	LOGICAL*1 		Bit3 / 8 /, C1 / 'W' /, 
& 			Counter / 0 /, Switch / .FALSE. / 

A LOGICAL*1 item occupies one byte of storage.

LOGICAL*1 is aligned on one-byte boundaries.

LOGICAL*2 @

The data type, LOGICAL*2, holds logical value .TRUE. or .FALSE. The value is defined as for LOGICAL.

A LOGICAL*2 occupies 2 bytes.

LOGICAL*2 is aligned on 2-byte boundaries.

LOGICAL*4 @

The logical data type, LOGICAL*4 holds a logical value .TRUE. or .FALSE. The value is defined as for LOGICAL.

A LOGICAL*4 occupies 4 bytes.

LOGICAL*4 is aligned on 4-byte boundaries.

LOGICAL*8 @

The logical data type, LOGICAL*8, holds the logical value .TRUE. or .FALSE. The value is defined the same way as for the LOGICAL data type.

A LOGICAL*8 occupies 8 bytes.

LOGICAL*8 is aligned on 8-byte boundaries.

REAL

A real datum is an approximation of a real number. The real data type, REAL, which usually has the synonym, REAL*4, holds one real datum.

The usual default size for a REAL item with no size specified is 4 bytes, and is aligned on 4-byte boundaries. However, these defaults can be changed by compiling with certain special options.

A REAL element has a sign bit, an 8-bit exponent, and a 23-bit fraction. These REAL elements in f77 conform to the IEEE standard.

REAL*4 @

The REAL*4 data type is a synonym for REAL, except that it always has a size of 4 bytes, independent of any compiler options.

REAL*8 (Double-Precision Real) @

The REAL*8, data type is a synonym for DOUBLE PRECISION, except that it always has a size of 8 bytes, independent of any compiler options.

REAL*16 (Quad Real) @

(SPARC only) The REAL*16 data type is a quadruple-precision real. The size for a REAL*16 item is 16 bytes. A REAL*16 element has a sign bit, a 15-bit exponent, and a 112-bit fraction. These REAL*16 elements in f77 conform to the IEEE standard for extended precision.

Size and Alignment of Data Types

Storage and alignment are always given in bytes. Values that can fit into a single byte are byte-aligned.

The size and alignment of types depends on various compiler options and platforms, and how variables are declared. The maximum alignment in COMMON blocks is to 4-byte boundaries.

Default data alignment and storage allocation can be changed by compiling with special options, such as -f, -dalign, -dbl_align_all, -dbl, -r8, -i2, and -xtypemap. The default descriptions in this manual assume that these options are not in force.

Refer to the Fortran User's Guide for details of specific compiler options.

The following table summarizes the default size and alignment, ignoring other aspects of types and options.

Table 2-1 Default Data Sizes and Alignments (in Bytes)

Fortran 77 Data Type 

Size  

  Default Alignment  

SPARC x86 

  Alignment in COMMON  

SPARC  x86 

BYTE X

CHARACTER X

CHARACTER*n X

COMPLEX X

COMPLEX*8 X

DOUBLE COMPLEX X

COMPLEX*16 X

COMPLEX*32 X

16 

16 

32 

8/16 

-- 

-- 

DOUBLE PRECISION X

REAL X

REAL*4 X

REAL*8 X

REAL*16 X

16 

8/16 

-- 

-- 

INTEGER X

INTEGER*2 X

INTEGER*4 X

INTEGER*8 X

LOGICAL X

LOGICAL*1 X

LOGICAL*2 X

LOGICAL*4 X

LOGICAL*8 X

Note the following:

Compiling with options -i2,-r8, or -dbl changes the defaults for certain data declarations that appear without an explicit size:

Table 2-2 Data Defaults Changed by -i2, -r8, -dbl

Default Type 

With -i2

With -r8 or -dbl

INTEGER INTEGER*2 INTEGER*8
LOGICAL LOGICAL*2 LOGICAL*8
REAL REAL*4 REAL*8
DOUBLE REAL*8 REAL*16
COMPLEX COMPLEX*8 COMPLEX*16
DOUBLE COMPLEX COMPLEX*16 COMPLEX*32

Do not combine -i2 with -r8 as this can produce unexpected results. REAL*16 and COMPLEX*32 are SPARC only.

With -dbl or -r8, INTEGER and LOGICAL are allocated the larger space indicated above. This is done to maintain the FORTRAN requirement that an integer item and a real item have the same amount of storage. However, with -r8 8 bytes are allocated but only 4-byte arithmetic is done. With -dbl, 8 bytes are allocated and full 8-byte arithmetic is done. In all other ways, -dbl and -r8 produce the same results. A disadvantage of using -r8 or -dbl is that it also promotes DOUBLE PRECISION data to QUAD PRECISION, possibly degrading performance.

Use of the more flexible -xtypemap option is preferred over the older -r8 and -dbl options. Both -dbl and -r8 have their -xtypemap equivalents:

The mapping integer:mixed indicates 8 byte integers but only 4 byte arithmetic.

There are two additional possibilities on SPARC:

-xtypemap=real:64,double:64,integer:mixed -xtypemap=real:64,double:64,integer:64

which map both default REAL and DOUBLE to 8 bytes, and should be preferable over using -r8 or -dbl.

Note that INTEGER and LOGICAL are treated the same, and COMPLEX is mapped as two REAL values. Also, DOUBLE COMPLEX will be treated the way DOUBLE is mapped.

Options -f or -dalign (SPARC only) force alignment of all 8, 16, or 32-byte data onto 8-byte boundaries. Option -dbl_align_all causes all data to be aligned on 8-byte boundaries. Programs that depend on the use of these options may not be portable.

See the Fortran User's Guide for details on these compiler options.