Sun Studio 12: Fortran User's Guide

Chapter 4 Fortran 95 Features and Differences

This appendix shows some of the major features differences between standard Fortran 95 and the Fortran 95 compiler, f95.

4.1 Source Language Features

The Fortran 95 compiler provides the following source langauge features and extensions to the Fortran 95 standard.

4.1.1 Continuation Line Limits

f95 allows 999 continuation lines (1 initial and 999 continuation lines). Standard Fortran 95 allows 19 for fixed-form and 39 for free-form.

4.1.2 Fixed-Form Source Lines

In fixed-form source, lines can be longer than 72 characters, but everything beyond column 73 is ignored. Standard Fortran 95 only allows 72-character lines.

4.1.3 Tab Form

The f95 fixed-format source text is defined as follows:

The f95 default maximum line length is 72 columns for fixed form and 132 for free form. Use the -e compiler option to extend the lines in fixed-format source to 132 columns.

Example: The tab form source on the left is treated as shown on the right.

!^IUses of tabs
^IREAL C = 3.0
^IWRITE(*,9) A, B, C
^I1 I3,
^I2 F9.1 )
!       Uses of tabs
        CHARACTER *3 A = ’A’
        INTEGER B = 2
        REAL C = 3.0
        WRITE(*,9) A, B, C
9       FORMAT(1X, A3,
       1 I3,
       2 F9.1 )

In the example above, ”^I” stands for the tab character, and the line starting with “1” and “2” are continuation lines. The coding is shown to illustrate various tab situations, and not to advocate any one style.

Tabs in f95 force the rest of the line to be padded out to column 72. This may cause unexpected results if the tab appears within a character string that is continued onto the next line:

Source file:

^Iprint *, "Tab on next line
^I1this  continuation line starts with a tab."

Running the code:

Tab on next line                                             this  continuation
 line starts with a tab.

4.1.4 Source Form Assumed

The source form assumed by f95 depends on options, directives, and suffixes.

Files with a .f or .F suffix are assumed to be in fixed format. Files with a .f90, .f95, .F90, or .F95 suffix are assumed to be in free format.

Table 4–1 F95 Source Form Command-line Options




Interpret all source files as Fortran fixed form


Interpret all source files as Fortran free form

If the -free or -fixed option is used, it overrides the file name suffix. If either a !DIR$ FREE or !DIR$ FIXED directive is used, it overrides the option and file name suffix. Mixing Forms

Some mixing of source forms is allowed. Case

Sun Fortran 95 is case insensitive by default. That means that a variable AbcDeF is treated as if it were spelled abcdef. Compile with the -U option to have the compiler treat upper and lower case as unique.

4.1.5 Limits and Defaults

4.2 Data Types

This section describes features and extensions to the Fortran 95 data types.

4.2.1 Boolean Type

f95 supports constants and expressions of Boolean type. However, there are no Boolean variables or arrays, and there is no Boolean type statement. Rules Governing Boolean Type Alternate Forms of Boolean Constants

f95 allows a Boolean constant (octal, hexadecimal, or Hollerith) in the following alternate forms (no binary). Variables cannot be declared Boolean. Standard Fortran does not allow these forms.


ddddddB, where d is any octal digit

Within an I/O format specification, the letter B indicates binary digits; elsewhere it indicates octal digits.


X’ddd or X"ddd", where d is any hexadecimal digit


Accepted forms for Hollerith data are:










Above, “…” is a string of characters and n is the character count.

Examples: Octal and hexadecimal constants.

Boolean Constant  

Internal Octal for 32-bit Word  









X’1 2 3’




Examples: Octal and hexadecimal in assignment statements.

i = 1357B
j = X"28FF"
k = X’-5A’

Use of an octal or hexadecimal constant in an arithmetic expression can produce undefined results and do not generate syntax errors. Alternate Contexts of Boolean Constants

f95 allows BOZ constants in the places other than DATA statements.







If these are assigned to a real variable, no type conversion occurs.

Standard Fortran allows these only in DATA statements.

4.2.2 Abbreviated Size Notation for Numeric Data Types

f95 allows the following nonstandard type declaration forms in declaration statements, function statements, and IMPLICIT statements. The form in column one is nonstandard Fortran 95, though in common use. The kind numbers in column two can vary by vendor.

Table 4–2 Size Notation for Numeric Data Types



Short Form 





One-byte signed integers 




Two-byte signed integers 




Four-byte signed integers 




One-byte logicals 




Two-byte logicals 




Four-byte logicals 




IEEE single-precision four-byte floating-point 




IEEE double-precision eight-byte floating-point 




IEEE quad-precision sixteen-byte floating-point 




Single-precision complex (four bytes each part) 




Double-precision complex (eight bytes each part) 




Quad-precision complex (sixteen bytes each part) 

4.2.3 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 default 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 -aligncommon, -f, -dalign, -dbl_align_all,- xmemalign,, and- xtypemap. The default descriptions in this manual assume that these options are not in force.

There is additional information in the Fortran Programming Guide regarding special cases of data types and alignment on certain platforms.

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

Table 4–3 Default Data Sizes and Alignments (in Bytes)

Fortran 95 Data Type 



Alignment inCOMMON  





























Note the following:

Options -f or -dalign 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.

4.3 Cray Pointers

A Cray pointer is a variable whose value is the address of another entity, called the pointee.

f95 supports Cray pointers; Standard Fortran 95 does not.

4.3.1 Syntax

The Cray POINTER statement has the following format:

POINTER  ( pointer_name, pointee_name [array_spec] ), …

Where pointer_name, pointee_name, and array_spec are as follows:


Pointer to the corresponding pointee_name.

pointer_name contains the address of pointee_name.Must be a scalar variable name (but not a derived type).Cannot be: a constant, a name of a structure, an array, or a function.


Pointee of the corresponding pointer_name

Must be: a variable name, array declarator, or array name


If array_spec is present, it must be explicit shape, (constant or non-constant bounds), or assumed-size.

For example, we can declare Cray pointers to two pointees:

    POINTER ( p, b ),  ( q, c )

The above example declares Cray pointer p and its pointee b, and Cray pointer q and its pointee c.

We can also declare a Cray pointer to an array:

     POINTER ( ix, x(n, 0:m) )

The above example declares Cray pointer ix and its pointee x; and declares x to be an array of dimensions n by m+1.

4.3.2 Purpose of Cray Pointers

You can use pointers to access user-managed storage by dynamically associating variables to particular locations in a block of storage.

Cray pointers allow accessing absolute memory locations.

4.3.3 Declaring Cray Pointers and Fortran 95 Pointers

Cray pointers are declared as follows:

POINTER ( pointer_name, pointee_name [array_spec] )

Fortran 95 pointers are declared as follows:

POINTER object_name

The two kinds of pointers cannot be mixed.

4.3.4 Features of Cray Pointers

4.3.5 Restrictions on Cray Pointers

4.3.6 Restrictions on Cray Pointees

4.3.7 Usage of Cray Pointers

Cray pointers can be assigned values as follows:

Example: Use Cray pointers as described above.

    SUBROUTINE  sub ( n )
    COMMON pool(100000)
    INTEGER blk(128), word64
    REAL a(1000), b(n), c(100000-n-1000)
    POINTER ( pblk, blk ), (ia, a ), ( ib, b ), &
            ( ic, c ), ( address, word64 )
    DATA address / 64 /
    pblk = 0
    ia = LOC( pool )
    ib = ia + 4000
    ic = ib + n

Remarks about the above example:

4.4 STRUCTURE and UNION (VAX Fortran)

To aid the migration of programs from f77, f95 accepts VAX Fortran STRUCTURE and UNION statements, a precursor to the “derived types” in Fortran 95. For syntax details see the FORTRAN 77 Language Reference manual.

The field declarations within a STRUCTURE can be one of the following:

As with f77, a POINTER statement cannot be used as a field declaration.

f95 also allows:

4.5 Unsigned Integers

The Fortran 95 compiler accepts a new data type, UNSIGNED, as an extension to the language. Four KIND parameter values are accepted with UNSIGNED: 1, 2, 4, and 8, corresponding to 1-, 2-, 4-, and 8-byte unsigned integers, respectively.

The form of an unsigned integer constant is a digit-string followed by the upper or lower case letter U, optionally followed by an underscore and kind parameter. The following examples show the maximum values for unsigned integer constants:


Expressed without a kind parameter (12345U), the default is the same as for default integer. This is U_4 but can be changed by the -xtypemap option, which will change the kind type for default unsigned integers.

Declare an unsigned integer variable or array with the UNSIGNED type specifier:

        UNSIGNED U
        UNSIGNED(KIND=2) :: A
        UNSIGNED*8 :: B

4.5.1 Arithmetic Expressions

4.5.2 Relational Expressions

Signed and unsigned integer operands may be compared using intrinsic relational operations. The result is based on the unaltered value of the operands.

4.5.3 Control Constructs

4.5.4 Input/Output Constructs

4.5.5 Intrinsic Functions

4.6 Fortran 2003 Features

A number of new features in the Fortran 2003 standard appear in this release of the f95 compiler. For details, refer to the Fortran 2003 standard.

4.6.1 Interoperability with C Functions

The new standard for Fortran provides:

The ISO_C_BINDING module provides access to named constants that are kind type parameters representing data that is compatible with C types.

The draft standard also introduces the BIND(C) attribute. A Fortran derived type is interoperable with C if it has the BIND attribute.

This release of the Fortran 95 compiler implements these features as described in the chapter 15 of the draft standard. Fortran also provides facilities for defining derived types and enumerations that correspond to C types, as described in chapter 4 of the standard.

4.6.2 IEEE Floating-Point Exception Handling

New intrinsic modules IEEE_ARITHMETIC, and IEEE_FEATURES provide support for exceptions and IEEE arithmetic in the Fortran language. Full support of these features is provided by:



The INTRINSIC keyword is new in Fortran 2003.These modules define a set of derived types, constants, rounding modes, inquiry functions, elemental functions, kind functions, and elemental and non-elemental subroutines. The details are contained in Chapter 14 of the draft standard for Fortran 2003.

4.6.3 Command-Line Argument Intrinsics

The Fortran 2003 standard introduces three new intrinsics for processing command-line arguments and environment variables. These are:

4.6.4 PROTECTED Attribute

The Fortran 95 compiler now accepts the Fortran 2003 PROTECTED attribute. PROTECTED imposes limitations on the usage of module entities. Objects with the PROTECTED attribute are only definable within the module that declares them.

4.6.5 Fortran 2003 Asynchronous I/O

The compiler recognizes the ASYNCHRONOUS specifier on I/O statements:


This syntax is as proposed in the Fortran 2003 standard, Chapter 9. In combination with the WAIT statement it allows the programmer to specify I/O processes that may be overlapped with computation. While the compiler recognizes ASYNCHRONOUS=’YES’, the draft standard does not require actual asynchronous I/O. In this release of the compiler, I/O is always synchronous.

4.6.6 Extended ALLOCATABLE Attribute

Fortran 2003 extends the data entities allowed for the ALLOCATABLE attribute. Previously this attribute was limited to locally stored array variables. It is now allowed with:

Allocatable entities remain forbidden in all places where they may be storage-associated: COMMON blocks and EQUIVALENCE statements. Allocatable array components may appear in SEQUENCE types, but objects of such types are then prohibited from COMMON and EQUIVALENCE.

4.6.7 VALUE Attribute

The f95 compiler accepts the Fortran 2003 VALUE type declaration attribute.

Specifying a subprogram dummy input argument with this attribute indicates that the actual argument is passed “by value”. The following example demonstrates the use of the VALUE attribute with a C main program calling a Fortran 95 subprogram with a literal value as an argument:

C code:
#include <stdlib.h>
int main(int ac, char *av[])

Fortran code:
       subroutine to_fortran(i)
       integer, value :: i
       print *, i

4.6.8 Fortran 2003 Stream I/O

The Fortran 2003 standard defines a new “stream” I/O scheme. Stream I/O access treats a data file as a continuous sequence of bytes, addressable by a positive integer starting from 1. The data file can be connected for either formatted or unformatted access.

Declare a stream I/O file with the ACCESS=’STREAM’ specifier on the OPEN statement. File positioning to a byte address requires a POS=scalar_integer_expression specifier on a READ or WRITE statement. The INQUIRE statement accepts ACCESS=’STREAM’, a specifier STREAM=scalar_character_variable, and POS=scalar_integer_variable.

4.6.9 Fortran 2003 Formatted I/O Features

Three new Fortran 2003 formatted I/O specifiers have been implemented in f95. They may appear on OPEN, READ, WRITE, PRINT, and INQUIRE statements:

When used in INQUIRE statements, these specifiers declare a character variable for returning the current values.

New edit descriptors DP, DC, RP, and RC change the defaults within a single FORMAT statement to decimal point, decimal comma, processor-defined rounding, and compatible rounding respectively. For example:

WRITE(*,’(I5,DC,F10.3)’) N, W

prints a comma instead of a period in the F10.3 output item.

See also the -iorounding compiler command-line option for changing the floating-point rounding modes on formatted I/O. (3.4.49 –iorounding[={compatible|processor-defined}].)

4.6.10 Fortran 2003 FLUSH I/O Statement

The f95 compiler accepts the Fortran 2003 FLUSH statement. The FLUSH statement makes data written to an external file available to other processes, or causes data placed in an external file by means other than Fortran to be available to a READ statement.

4.7 Additional I/O Extensions

The section describes extensions to Fortran 95 Input/Output handling that are accepted by the f95 compiler that are not part of the Fortran 2003 draft standard. Some are I/O extensions that appeared in the Fortran 77 compiler, f77, and are now part of the Fortran 95 compiler.

4.7.1 I/O Error Handling Routines

Two new functions enable the user to specify their own error handling routine for formatted input on a logical unit. When a formatting error is detected, the runtime I/O library calls the specified user-supplied handler routine with data pointing at the character in the input line causing the error. The handler routine can supply a new character and allow the I/O operation to continue at the point where the error was detected using the new character; or take the default Fortran error handling.

The new routines, SET_IO_ERR_HANDLER(3f) and GET_IO_ERR_HANDLER(3f), are module subroutines and require USE SUN_IO_HANDLERS in the routine that calls them. See the man pages for these routines for details.

4.7.2 Variable Format Expressions

Fortran 77 allowed any integer constant in a format to be replaced by an arbitrary expression enclosed in angle brackets:

1 FORMAT(< expr > … )

Variable format expressions cannot appear as the n in an nH… edit descriptor, in a FORMAT statement that is referenced by an ASSIGN statement, or in a FORMAT statement within a parallel region.

This feature is enabled natively in f95, and does not require the -f77 compatibility option flag.

4.7.3 NAMELIST Input Format

4.7.4 Binary Unformatted I/O

Opening a file with FORM=’BINARY’ has roughly the same effect as FORM=’UNFORMATTED’, except that no record lengths are embedded in the file. Without this data, there is no way to tell where one record begins, or ends. Thus, it is impossible to BACKSPACE a FORM=’BINARY’ file, because there is no way of telling where to backspace to. A READ on a ’BINARY’ file will read as much data as needed to fill the variables on the input list.

4.7.5 Miscellaneous I/O Extensions

4.8 Directives

A compiler directive directs the compiler to do some special action. Directives are also called pragmas.

A compiler directive is inserted into the source program as one or more lines of text. Each line looks like a comment, but has additional characters that identify it as more than a comment for this compiler. For most other compilers, it is treated as a comment, so there is some code portability.

Sun-style parallelization directives are the default with f95 -explicitpar. To switch to Cray-style directives, use the -mp=cray compiler command-line flag. Explicit parallelization with OpenMP directives requires compiling with -openmp.

A complete summary of Fortran directives appears in Table C–1.

4.8.1 Form of Special f95 Directive Lines

f95 recognizes its own special directives in addition to those described in 1.9 Command-Line Help. These have the following syntax:

!DIR$ d1, d2, … Fixed-Form Source Free-Form Source

Thus, !DIR$ in columns 1 through 5 works for both free-form source and fixed-form source.

4.8.2 FIXED and FREE Directives

These directives specify the source form of lines following the directive line. Scope

They apply to the rest of the file in which they appear, or until the next FREE or FIXED directive is encountered. Uses Restrictions

The FREE/FIXED directives:

Example: A FREE directive.

    DO i = 1, n
        a(i) = b(i) * c(i)
    END DO

4.8.3 Parallelization Directives

A parallelization directive is a special comment that directs the compiler to attempt to parallelize the next DO loop. These are summarized in Appendix D and described in the chapter on parallelization in the Fortran Programming Guide. f95 recognizes both Sun and Cray style parallelization directives, as well as the OpenMP Fortran API directives. OpenMP parallelization is described in the OpenMP API User’s Guide.

4.9 Module Files

Compiling a file containing a Fortran 95 MODULE generates a module interface file (.mod file) for every MODULE encountered in the source. The file name is derived from the name of the MODULE; file xyz.mod (all lowercase) will be created for MODULE xyz.

Compilation also generates a .o module implementation object file for the source file containing the MODULE statements. Link with the module implementation object file along with the all other object files to create an executable.

The compiler creates module interface files and implementation object files in the directory specified by the -moddir=dir flag or the MODDIR evironment variable. If not specified, the compiler writes .mod files in the current working directory.

The compiler looks in the current working directory for the interface files when compiling USE modulename statements. The- Mpath option allows you to give the compiler an additional path to search. Module implementation object files must be listed explicitly on the command line for the link step.

Typically, programmers define one MODULE per file and assign the same name to the MODULE and the source file containing it. However, this is not a requirement.

In this example, all the files are compiled at once. The module source files appear first before their use in the main program.

demo% cat mod_one.f90
demo% cat mod_two.f90
demo% cat main.f90
 USE one
 USE two
demo% f95 -o main mod_one.f90 mod_two.f90 main.f90

Compilation creates the files:


The next example compiles each unit separately and links them together.

demo% f95 -c mod_one.f90 mod_two.f90
demo% f95 -c main.f90
demo% f95 -o main main.o mod_one.o mod_two.o

When compiling main.f90, the compiler searches the current directory for one.mod and two.mod. These must be compiled before compiling any files that reference the modules on USE statements. The link step requires the module implementation object files mod_one.o and mod_two.o appear along with all other object files to create the executable.

4.9.1 Searching for Modules

With the release of the version 7.0 of the Fortran 95 compiler, .mod files can be stored into an archive (.a) file. An archive must be explicitly specified in a -Mpath flag on the command line for it to be searched for modules. The compiler does not search archive files by default.

Only .mod files with the same names that appear on USE statements will be searched. For example, the Fortran 95 statement USE mymod causes the compiler to search for the module file mymod.mod by default.

While searching, the compiler gives higher priority to the directory where the module files are being written. This can be controlled by the -moddir=dir option flag and the MODDIR environment variable. This implies that if only the -Mpath option is specified, the current directory will be searched for modules first, before the directories and files listed on the -M flag.

4.9.2 The -use=list Option Flag

The -use=list flag forces one or more implicit USE statements into each subprogram or module subprogram compiled with this flag. By using the flag, it is not necessary to modify source programs when a module or module file is required for some feature of a library or application.

Compiling with -use=module_name has the effect of adding a USE module_name to each subprogram or module being compiled. Compiling with -use=module_file_name has the effect of adding a USE module_name for each of the modules contained in the module_file_name file.

4.9.3 The fdumpmod Command

Use the fdumpmod(1) command to display information about the contents of a compiled module information file.

demo% fdumpmod x.mod group.mod
x 1.0 v8,i4,r4,d8,n16,a4 x.mod
group 1.0 v8,i4,r4,d8,n16,a4 group.mod

The fdumpmod command will display information about modules in a single .mod file, files formed by concatenating .mod files, and in .a archives of .mod files. The display includes the name of the module, a version number, the target architecture, and flags indicating compilation options with which the module is compatible. See the fdumpmod(1) man page for details.

4.10 Intrinsics

f95 supports some intrinsic procedures that are extensions beyond the standard.

Table 4–4 Nonstandard Intrinsics



Function Type 

Argument Types 








P, E 


Positive difference 

double precision 

double precision 


P, E 


Get the number of leading 0 bits 


Boolean, integer, real, or pointer 


NP, I 


Get the number of set bits 


Boolean, integer, real, or pointer 


NP, I 


Calculate bit population parity 


Boolean, integer, real, or pointer 


NP, I 

Notes:P: The name can be passed as an argument. NP: The name cannot be passed as an argument. E: External code for the intrinsic is called at run time. I: f95 generates inline code for the intrinsic procedure.

See the Fortran Library Reference for a more complete discussion of intrinsics, including those from Fortran 77 that are recognized by the Fortran 95 compiler.

4.11 Forward Compatibility

Future releases of f95 are intended to be source code compatible with this release.

Module information files generated by this release of f95 are not guaranteed to be compatible with future releases.

4.12 Mixing Languages

Routines written in C can be combined with Fortran programs, since these languages have common calling conventions. See the C/Fortran Interface chapter in the Fortran Programming Guide for details on how to interoperate between C and Fortran routines.