What are the data types in FORTRAN?

FORTRAN was the first programming language, developed by John W. Backus in 1957. There are six basic data types in FORTRAN:

  • Integer
  • Real
  • Complex
  • Character
  • Logical
  • Double Precision.

Categories

These data types are further categorized into two types.

  1. Numeric data types: Integer, Real, Complex, and Double Precision
  2. Non-numeric data types: Character and Logical
Data Types in FORTRAN

1. Numeric data types

  • Integer

Integers are the discrete and exact numbers; they range from -2x10^9 to 2x10^9 on a 32-bit machine. Integers can have an optional sign in them. The basic operations that can be performed on integers are addition, subtraction, multiplication, division, and exponentiation.

Examples of integers are 3, 0, 25, -6, etc.

# Declare integer
integer :: number
# Assign value to integer
number = 4000
# Print number (On execution, it will print: 4000)
Print *, number
  • Real

The real data type represents the real numbers that are used to measure quantities. Real numbers have a mandatory decimal point and an optional sign in them.

They range from -10^77 to 10^77. Just like integers, we can perform addition, subtraction, multiplication, division, and exponentiation on real numbers too.

Some examples of real numbers are 0.3, 7.08, 6.02x10E23, etc.

# Declare real number
real :: div
# Assign value to real number
div = 147.47
# Print div (On execution, it will print: 147.470001)
Print *, div
  • Complex

The complex data type stores complex numbers that have a real and imaginary part.

For example, the complex number 4.0-7.0i is represented as (4.0, -7.0) in FORTRAN.

# Declare complex number
complex :: complexValue
# Assign value to complex number
complexValue = (2.0, 5.0)
# Print complexValue (On execution, it will print: (2.00000000,5.00000000))
Print *, complexValue
  • Double precision

The double precision data type is similar to real numbers, but has greater precision. It has an accuracy of up to 14 digits. The same mathematical operations of integers and real numbers can be performed on the double precision data type.

Some examples include 1.3D+2, 1D-02, etc. Here, D represents the exponent, as E does in real numbers.

# Declare double precision number
double precision :: dub
# Assign value to double precision number
dub = 7777.77777
# Print dub (On execution, it will print: 7777.7778320312500)
Print *, dub

2. Non-numeric data types

  • Character

The character data type shows a sequence of one or more charactersa string surrounded by a pair of single quotes.

Some examples are: ‘Welcome to Educative’, ‘1234A7’, etc. To show a single quote in character data type, two single quotes are placed together, e.g, ‘Educative’’s courses’ will be printed as Educative’s courses.

# Declare character
character(len=40) :: welcomeMessage
# Assign value to character
welcomeMessage = "Welcome to educative"
# Print welcomeMessage (On execution, it will print: Welcome to educative)
Print *, welcomeMessage
  • Logical

The logical data type shows two possible states, true and false only. Logical AND, OR, and NOT operations can result in this data type.

# Declare logical
logical :: check
# Assign value to logical
check = .true.
# Print check (On execution, it will print: T)
Print *, check

Free Resources