Skip to content
Tutorials Center
My learning

Topics

Lesson 5 of 39 ยท Language Basics

Variables and Data Types

Learn Fortran's five intrinsic types integer, real, complex, logical and character, how to declare variables and constants with parameter, and how to initialize them.

Fortran is statically typed: every variable has a type fixed at compile time, and the compiler uses it to check your code and generate fast machine instructions. There are five intrinsic types: integer, real, complex, logical and character. This lesson shows how to declare each, give them initial values, make constants with parameter, and convert between types.

Declaring variables

The general form of a declaration is type [, attributes] :: name [= initial value].

FORTRAN
program declare_demo
  implicit none
  integer :: count
  real :: price, tax_rate
  logical :: is_member
  character(len=20) :: customer
  complex :: z

  count = 3
  price = 19.99
  tax_rate = 0.07
  is_member = .true.
  customer = "Ada"
  z = (1.0, 2.0)

  print *, count, price, tax_rate
  print *, is_member, customer
  print *, z
end program declare_demo

Several names can share one declaration: real :: x, y, z. The compiler does not give uninitialized variables a value, so printing one gives garbage. Always assign before you read.

The five intrinsic types

Type Holds Example literals
integer whole numbers 42, -7, 0
real floating-point numbers 3.14, 2.5e-3, 1.0
complex a pair of reals (1.0, -2.0)
logical true or false .true., .false.
character text "hello", 'x'

Fortran has a native complex type, which is one reason it is loved by physicists and engineers. Real literals need a decimal point (1.0, not 1) or an exponent, or they are integers. Use e notation for powers of ten: 6.02e23. Logicals are written with dots, and the dots are part of the literal.

Five intrinsic types

integer Whole numbers.
real Decimal numbers.
complex Real and imaginary part.
logical .true. or .false.

A fifth type, character, holds text.

1 / 2

Initializing at declaration

You can give a value in the declaration:

FORTRAN
integer :: year = 2025
real :: gravity = 9.81

Careful: in a procedure, an initialized local variable is implicitly saved, meaning it keeps that value between calls and is initialized only once. This surprises many programmers. When you want a fresh value on each call, assign it in the executable part instead. In the main program this distinction does not matter.

Constants with parameter

Add the parameter attribute to make a named constant that can never change:

FORTRAN
real, parameter :: pi = 3.14159265
integer, parameter :: max_items = 100
character(len=*), parameter :: greeting = "Hello"

The compiler rejects any attempt to assign to pi later. Constants are the right place for magic numbers, array sizes and kind values. Note len=* gives the string the length of its value.

Integer details

An integer is stored in a fixed number of bits, so there is a largest value. The default integer is usually 32 bits, with a range of about -2.1 billion to 2.1 billion. Go beyond and the result wraps or is undefined, depending on compiler options. The huge intrinsic reports the largest value:

FORTRAN
print *, huge(1)      ! 2147483647 with a 32-bit default integer

The kinds and precision lesson shows how to request 64-bit integers.

Python Stand-in (Python): a 32-bit integer wrap

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Real details

A real is a binary floating-point number, usually 32-bit single precision with about seven significant decimal digits. That means 0.1 cannot be stored exactly:

FORTRAN
real :: x
x = 0.1
print *, x * 3.0     ! close to 0.3, but possibly 0.300000012

Never compare reals with ==; compare the difference to a small tolerance instead. Chapter 4 shows the numeric intrinsics for this.

Python Stand-in (Python): single vs double precision

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Logical and character

A logical holds the result of comparisons and logical operators:

FORTRAN
logical :: ready
ready = (count > 0) .and. (price < 50.0)

A character variable has a fixed length: character(len=10) :: name always holds 10 characters, padded with blanks if the text is shorter, and truncated if longer. Strings get their own lesson: characters and strings.

Mixing types and conversion

Fortran converts automatically when you mix integers and reals in an expression (the integer becomes real), but assignment to an integer truncates:

FORTRAN
integer :: i
real :: r
i = 7.9      ! i becomes 7 (truncation toward zero)
r = 7        ! r becomes 7.0
r = 7 / 2    ! integer division first: r becomes 3.0
r = 7.0 / 2  ! r becomes 3.5

Use conversion functions to be explicit: real(i), int(r) (truncates), nint(r) (rounds to nearest), floor(r) and ceiling(r). Being explicit documents your intent and silences conversion warnings.

Python Stand-in (Python): int, nint, floor, ceiling

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which declaration creates a constant?

    The parameter attribute makes a named constant that cannot be changed.
  2. 2. What is stored in i after integer :: i; i = 7.9?

    Assigning a real to an integer truncates toward zero, so i becomes 7.
  3. 3. Which is a valid logical literal?

    Logical literals are written .true. and .false., with the dots.
  4. 4. What is r after real :: r; r = 7 / 2?

    7 / 2 is integer division and gives 3, which is then converted to 3.0.

Summary

  • The five intrinsic types are integer, real, complex, logical and character.
  • Declare with type :: name; use parameter for constants.
  • Uninitialized variables hold garbage; initializing in a procedure implies save.
  • Mixed expressions convert integers to reals, but assigning a real to an integer truncates.
  • Use real(), int(), nint(), floor() and ceiling() to convert explicitly.

Next: Kinds and Precision