Variables and assignment

Variables are the fundamental building blocks of any program. In Fortran, a variable name may consist of up to 31 alphanumeric characters, of which the first character must be a letter. Underscores are allowed but not spaces or special characters.

Variable Types

Fortran defines several distinct types corresponding to the generic types described above. These include
INTEGER
REAL
DOUBLE PRECISION  (this name is obsolet but is still widely used)
COMPLEX
CHARACTER
LOGICAL
Constants are literal representations of a type that are specified by the programmer. In Fortran, different constant formats are required for each type.
declaration
Literal constants


The following program illustrates variable declarations.
declaration
Variable declarations


Characters

Characters can either be declared as a single CHARACTER variable, a string of characters or an array of single characters or character strings.
character                        :: c1 ! Single character
character(len=80)                :: c2 ! String of characters
character, dimension(10)         :: c3 ! Array of single
                                       ! characters
character(len=80), dimension(10) :: c4 ! Array of character
                                       ! strings (10 elements)


Using kind

The current preferred method of specifying the precision of a variable is to use the KIND representation. With KIND, the programmer specifies the number of decimal digits and the exponent range required, and the system matches the request to its underlying machine representation as closely as possible. Since nearly all current systems use the IEEE 754 convention, in practice only two kinds are widely used. Compilers still recognize the REAL and DOUBLE PRECISION keywords. The advantage to the kind convention is that it can be made very simple to switch between real and double precision. For most purposes, it is only necessary to use the SELECTED_REAL_KIND intrinsic and the programmer need never worry about what is going on within the machine. To declare single precision, use
INTEGER, PARAMETER:: rk = SELECTED_REAL_KIND(6,37)
To declare a double precision, use
INTEGER, PARAMETER :: rk = SELECTED_REAL_KIND(15,307)
Quadruple precisions (not very often used):
INTEGER, PARAMETER :: rk = selected_real_kind(33, 4931)
Floating-point variables can now be declared with statements such as
REAL (rk) :: r, s, t
and the precision will be determined by which selected_real_kind statement precedes the declarations.

Here is a complete example showing how to use selected_real_kind:
program select_kind
  implicit none
  integer, parameter   :: i4=SELECTED_INT_KIND(4)
  integer, parameter   :: i8=SELECTED_INT_KIND(8)
  integer, parameter   :: i15=SELECTED_INT_KIND(15)
  integer, parameter   :: r4=SELECTED_REAL_KIND(6,37)
  integer, parameter   :: r8=SELECTED_REAL_KIND(15,307)
  integer(KIND=i4) :: ia
  integer(KIND=i8) :: ib
  integer(KIND=i15) :: ic
  real(KIND=r4) :: ra
  real(KIND=r8) :: rb

  print *,' Integer kind=', i4, ' biggest value=', huge(ia)
  print *,' Integer kind=', i8, ' biggest value=', huge(ib)
  print *,' Integer kind=', i15, ' biggest value=', huge(ic)
  print *,' Real kind=', r4, ' smallest value= ', tiny(ra) , &
          ' biggest value=', huge(ra)
  print *,' Real kind=', r8, ' smallest value= ', tiny(rb) , &
          ' biggest value=', huge(rb)

end program select_kind