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.
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;
A symbolic name of a constant, variable, array, or function has only one data type for each program unit, except for generic functions.
If you explicitly list a name in a type statement, then that determines the data type.
If you do not explicitly list a name in a type statement, then the first letter of the name determines the data type implicitly.
The default implicit typing rule is that if the first letter of the name is I, J, K, L, M, or N, then the data type is integer, otherwise it is real.
You can change the default-implied types by using the IMPLICIT statement, even to the extent of turning off all implicit typing with the IMPLICIT NONE statement. You can also turn off all implicit typing by specifying the -u compiler flag on the command line; this is equivalent to beginning each program unit with the IMPLICIT NONE statement.
An array element has the same type as the array name.
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:
Explicitly by putting its name in a type statement
Explicitly in its FUNCTION statement, by preceding the word FUNCTION with the name of a data type
Implicitly by its name, as with variables
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.
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.
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:
One character
An integer between -128 and 127
The logical values, .TRUE. or .FALSE.
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.
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.
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.
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.
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 ").
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.
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.
(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.
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.
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 .
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 ").
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.
The integer data type, INTEGER*4, holds a signed integer.
An INTEGER*4 occupies 4 bytes.
INTEGER*4 is aligned on 4-byte boundaries.
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.
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.
The one-byte logical data type, LOGICAL*1, which has the synonym, BYTE, can hold any of the following:
One character
An integer between -128 and 127
The logical values .TRUE. or .FALSE.
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.
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.
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.
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.
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.
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.
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.
(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.
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 |
1 1 n |
1 1 1 |
1 1 1 |
1 1 1 |
1 1 1 |
COMPLEX X COMPLEX*8 X DOUBLE COMPLEX X COMPLEX*16 X COMPLEX*32 X |
8 8 16 16 32 |
4 4 8 8 8/16 |
4 4 4 4 -- |
4 4 4 4 4 |
4 4 4 4 -- |
DOUBLE PRECISION X REAL X REAL*4 X REAL*8 X REAL*16 X |
8 4 4 8 16 |
8 4 4 8 8/16 |
4 4 4 4 -- |
4 4 4 4 4 |
4 4 4 4 -- |
INTEGER X INTEGER*2 X INTEGER*4 X INTEGER*8 X |
4 2 4 8 |
4 2 4 8 |
4 2 4 4 |
4 2 4 4 |
4 2 4 4 |
LOGICAL X LOGICAL*1 X LOGICAL*2 X LOGICAL*4 X LOGICAL*8 X |
4 1 2 4 8 |
4 1 2 4 8 |
4 1 2 4 4 |
4 1 2 4 4 |
4 1 2 4 4 |
Note the following:
REAL*16 and COMPLEX*32 are only available on SPARC only. In 64-bit environments (compiling with -xarch=v9 or v9a) the default alignment is on 16-byte (rather than 8-byte) boundaries, as indicated by 8/16 in the table.
Arrays and structures align according to their elements or fields. An array aligns the same as the array element. A structure aligns the same as the field with the widest alignment.
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:
On SPARC:
-dbl same as: -xtypemap=real:64,double:128,integer:64 -r8 same as: -xtypemap=real:64,double:128,integer:mixed
On x86:
-dbl same as: -xtypemap=real:64,double:64,integer:64 -r8 same as: -xtypemap=real:64,double:64,integer:mixed
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.