TExcelTAD
Home › Fortran › Fortran Variables & Types

Fortran Variables & Types

A variable is a named piece of memory that holds a value of one fixed type. In Fortran every variable is declared at the top of its program unit with the form type :: name.

fortran · try it yourself
program declarations
  implicit none
  integer   :: apples        ! whole numbers
  real      :: price         ! floating-point numbers
  complex   :: z             ! a pair (real part, imaginary part)
  character :: grade         ! a single character
  logical   :: in_stock      ! .true. or .false.

  apples   = 12
  price    = 0.45
  z        = (3.0, -4.0)
  grade    = 'A'
  in_stock = .true.

  print *, apples, price
  print *, z
  print *, grade, ' ', in_stock
end program declarations
          12  0.449999988
            (3.00000000,-4.00000000)
 A  T

The five intrinsic types

Type Holds Literal examples
integer whole numbers (default: 32-bit, about ±2.1 billion) 42, -7, 0
real floating-point numbers (default: 32-bit, ~7 digits) 3.5, -0.01, 6.02e23
complex complex numbers, two reals (1.0, 2.0)
character text; character(len=n) holds n characters 'hello', "it's"
logical truth values .true., .false.

Note: A literal like 0.45 is a default real, so it is stored with about 7 significant digits. That's why it prints as 0.449999988. Use the real64 kind (next lessons) when you need double precision.

Initialising in the declaration

You can give a variable a starting value right in its declaration, and declare several variables of one type on one line:

fortran · try it yourself
program init_demo
  implicit none
  integer :: width = 8, height = 5
  real :: ratio
  character(len=10) :: label = 'box'

  ratio = real(width) / height
  print '(a, i0, a, i0)', 'Size: ', width, ' x ', height
  print '(a, f5.2)', 'Ratio: ', ratio
  print '(3a)', '[', label, ']'
end program init_demo
Size: 8 x 5
Ratio:  1.60
[box       ]

label has room for 10 characters, so 'box' is padded with blanks on the right. Strings are covered in detail later.

Warning: Initialising in a declaration (integer :: count = 0) implicitly gives the variable the save attribute: inside a procedure it is set only once, not every time the procedure is called. In the main program that doesn't matter, but in functions and subroutines it is a classic bug. Assign the starting value with a separate statement instead.

Constants with parameter

A named constant is declared with the parameter attribute. Its value is fixed at compile time and any attempt to change it is an error:

fortran · try it yourself
program constants
  implicit none
  real, parameter :: pi = 3.14159265
  real, parameter :: g = 9.81          ! m/s^2
  integer, parameter :: days_per_week = 7
  real :: radius = 2.0

  print '(a, f8.4)', 'Circle area:  ', pi * radius**2
  print '(a, f8.4)', 'Fall in 1 s:  ', 0.5 * g * 1.0**2
  print '(a, i0)',   'Days in 3 weeks: ', 3 * days_per_week
end program constants
Circle area:   12.5664
Fall in 1 s:    4.9050
Days in 3 weeks: 21

Constants are also used to set array sizes and kind values, which must be known at compile time.

Type conversion

Fortran converts between numeric types automatically in assignments and mixed expressions, but you can (and often should) be explicit with the conversion functions:

fortran · try it yourself
program conversions
  implicit none
  integer :: n
  real :: x

  x = 7.9
  n = x                  ! real -> integer truncates towards zero
  print *, n
  print *, int(-7.9), nint(7.5), nint(-7.5)   ! truncate, round to nearest
  print *, floor(-7.9), ceiling(7.1)
  print *, real(3) / 2   ! convert before dividing
end program conversions
           7
          -7           8          -8
          -8           8
   1.50000000    
Function Result
int(x) integer, truncated towards zero
nint(x) nearest integer (halves round away from zero)
floor(x) / ceiling(x) rounded down / up
real(n) converts to real
cmplx(a, b) builds a complex number

Test yourself

How do you write the logical value true in Fortran?

  1. true
  2. TRUE
  3. .true.
  4. 1

What does n = 7.9 store in an integer n?

  1. 8
  2. 7
  3. 7.9
  4. A compile error

What does the parameter attribute do?

  1. Marks a procedure argument
  2. Makes the variable a compile-time named constant
  3. Allocates the variable on the heap
  4. Makes the variable public