Variables and data types
In modern Fortran, five intrinsic data types are available in addition a user may define their own data types.`
Logical
Logical variables can have only have one of two values: .true. or .false.:
logical :: a,b a = .true. b = .false.
Character
A character variable can store a single character. For storing a string of characters, it is possible to specify a length:
-
! a can store a single character character :: a ! b can store a string of max 20 characters character(len=20) :: b a = 'R' b = 'Hello'
Numeric data types
Three numeric data types are available in Fortran: integer, real and complex. An optional kind argument may be supplied to an integer declaration, which allows the programmer to set the range of the integer variable. For real or complex data types, the precision and range of the variable may be set using the "selected real kind" function:
-
! 'double' is a parameter: a new value cannot be ! assigned to it later integer, parameter :: double=selected_real_kind(15 ,307) ! i is in the range −10ˆ8 to 10ˆ8 integer(kind=8) :: i ! a has 15 digits precision and exponent range +/−30 real(double) :: a complex (double) :: c User-defined data types
The basic data types are often inadequate for describing more complex data. For these situations, Fortran allows the programmer to create their own types. The following program illustrates the creation of a user-defined type to store information about a person:
-
character (len=100) :: name integer :: age real :: weight program userType implicit none ! Definition of a new data type called 'personType' type :: personType end type personType ! Declare an instance of personType named A type (personType) :: A ! The individual elements of A are accessed ! assigned using the % operator A%name = 'John Joe' A%age = 45 A%weight = 65.0 print∗, A%name end program userType