Fortran/data types


Variables and data declarations must occur at the start of all Fortran program units before any executable statements. Variables may have a "kind" that specifies its size in memory. The kind parameter can have different meanings for each compiler or processor, so be sure to check what kinds are available to you in your compiler's documentation.

Declaration styleEdit

There are several ways to declare a variable. The modern Fortran style is to be verbose and explicit. The following example declares an real array with the a kind of 8 (on most compilers this means 8 bytes long, i.e. double precision).

! Modern variable declaration
! <datatype> [(kind=<num>), <attribute>, ... ::] <identifier>[, ...]
! Example:
real (kind=8), dimension (3) :: variable

The dimension attribute is quite long to type out, but may be more concise when declaring multiple arrays on a single line. If only a single variable is to be declared then it may be more concise to specify the dimension with parentheses next to the identifier.

real (kind=8) :: variable(3)

Variables may also be initialized with an assignment.

real (kind=8) :: variable(3) = 1.0

The :: is optional for backwards compatibility with older versions of Fortran. The older style of declarations use a * to denote the kind of a variable.

REAL*8 variable(3)

Furthermore, the kind may be omitted entirely to simply use the default kind for your compiler.

real variable(3)

Intrinsic data typesEdit

IntegerEdit

The integer data type stores signed integer values (i.e. ..., -3, -2, -1, 0, 1, 2, 3, ...). On most compilers, the default kind stores integers as short integers 4 bytes in size (kind=4). Long integers are usually 8 bytes (kind=8). The allowed attributes are: allocatable, intrinsic, public, asynchronous, optional, save, parameter, bind, pointer, target, dimension (dims), private, value, external, protected, volatile and intent (inout).

integer :: variable

LogicalEdit

The logical data type stores boolean vales and can only contain values .true. or .false.. The default logical kind for most compilers is 4, occupying 4 bytes. Therefore, logicals are much like integers in terms of memory, but integers and logicals are generally not compatible for most operations. The allowed attributes are: allocatable, intrinsic, public, asynchronous, optional, save, parameter, bind, pointer, target, dimension (dims), private, value, external, protected, volatile, intent (inout).

logical :: variable

RealEdit

The real data type stores floating point data. Vales are stored in memory in scientific notation as a mantissa and an exponent. The default kind for real variables is 4, which consists of 4 bytes (32 bits). In this case 24 bits are used for the mantissa and 8 bits are used for the exponent. The allowed attributes are: allocatable, intrinsic, public, asynchronous, optional, save, parameter, bind, pointer, target, dimension (dims), private, value, external, protected, volatile, intent (inout).

All compilers support at least two kinds of real kinds for low precision and high precision numbers. But the standard does not specify what size these precisions should be. Most compilers use 32 bits for single precision and 64 bits for double precision. Therefore, many compilers support a double precision real data type that uses the higher precision that is available. However, for portablility it is much better to use the selected_real_kind intrinsic function to select the required kind parameter for your variables precision.

real :: variable
double precision :: variable2

ComplexEdit

In mathematics, a complex number has a real and an imaginary component. Complex numbers in Fortran are stored in rectangular coordinates as a pair of real numbers (real part first, followed by the imaginary part). The default kind of complex numbers is always the same as the default kind of the real data type. So a complex variable of kind 4 will contain two reals of kind 4. If kind 4 reals corresponds to 4 bytes, then the default complex variables will be 8 bytes in size. The allowed attributes are: allocatable, intrinsic, public, asynchronous, optional, save, parameter, bind, pointer, target, dimension (dims), private, value, external, protected, volatile, intent (inout).

complex :: variable

Complex operationsEdit

All of the arithmetic operators can take a complex number on either side. Fortran automatically handles complex arithmetic and the special rules of imaginary numbers.

Care must be taken in using function calls that might cause an error. For instance, taking the square root of the real number -1.0 will result in an error, because -1 is outside of the domain of real square root. Taking the square root of the complex number (-1.0,0.0) is allowed because -1 is in the domain of complex square root.

CharacterEdit

The character data type stores strings of characters. The default kind of character variables is usually 1 which represents ASCII characters. Kind 2 usually represents the ISO 10646 standard characters. Character variables are unique in that they also have a len parameter in addition to kind which specifies the number of characters in the string. Typically a single character occupies 1 byte in memory. In addition to this, character data types can also be arrays.

character (len=5,kind=1), dimension (2) :: strings

The old style declarations does not have a kind parameter and only has the length parameter.

CHARACTER*5 strings(2)

ConstantsEdit

Literal constantsEdit

Data embedded in expressions are referred to as literals or constants. Literals can have a particular data type depending on how they are written. For example, in the below line the value 1 is an integer.

a = a + 1

The below table demonstrate how to type literal constants.

Literal constants
Type Example literals
integer 0, -1, 9999
logical .true., .false., T, F
real 1.1, 0.0005, -99.9e-99
complex (-1.0,3), (0.5,-3e5)
character 'Hello'

Note that the parentheses notation for complex numbers cannot be used with variables. For example, (a, b) is invalid. To convert real variables to complex, use the cmplx function:

cmplx(a, b)

Any expression involving complex numbers and other numbers is promoted to complex.

Parameter constantsEdit

Constants literals are simply unnamed data. Variables can be constants too. They are declared with the parameter attribute. These variables are immutable and assigning to them after they have been declared will cause an error. They must be initialized with a value when they are declared.

real, parameter :: PI = 3.141592