100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

Fortran Data Types and Variables

An overview of Fortran's intrinsic data types, integer, real, character, logical, and complex, and how to declare and initialize variables.

FoundationsBeginner9 min readJul 10, 2026
Analogies

Fortran Data Types and Variables

Fortran provides five intrinsic data types: integer for whole numbers, real for floating-point numbers, complex for numbers with real and imaginary parts, character for text, and logical for true/false values. Each variable must be declared with one of these types before it is used, typically at the top of a program unit, in a form like integer :: count or real :: temperature. This static typing lets the compiler catch type mismatches at compile time rather than causing errors at runtime.

🏏

Cricket analogy: Like a scorecard having distinct columns for runs, overs, wickets, and extras, each tracking a specific kind of number, Fortran has distinct types, integer, real, character, logical, complex, each tracking a specific kind of data.

Declaring and Initializing Variables

Variables are declared using the syntax type, attributes :: name, optionally followed by an initial value, for example integer :: score = 0 or character(len=20) :: username. The parameter attribute creates a named constant whose value cannot change after compilation, such as real, parameter :: pi = 3.14159265. Character variables require a specified length using len=, since Fortran strings are fixed-length by default unless declared as allocatable or deferred-length with the character(:) syntax.

🏏

Cricket analogy: Like fixing a player's squad number before the tournament starts and never changing it, real, parameter :: pi = 3.14159265 fixes a value at compile time that can never change during the program.

Numeric Precision: Kind Specifiers

Plain real variables typically use single precision (about 6-7 significant decimal digits), which is often insufficient for scientific work requiring more accuracy. Modern Fortran addresses this with kind specifiers, for example real(kind=8) :: precise_value or, more portably, using the selected_real_kind function or the iso_fortran_env module's real64 constant, which guarantees double precision (about 15-16 significant digits) regardless of the compiler or platform.

🏏

Cricket analogy: Like the difference between a basic scoreboard showing only runs and a Hawk-Eye system tracking ball trajectory to the millimeter, real(kind=8) gives far more precision than plain single-precision real.

Never compare real (floating-point) values for exact equality with ==. Due to rounding, 0.1 + 0.2 may not exactly equal 0.3. Instead check that the absolute difference is smaller than a small tolerance, e.g. abs(a - b) < 1.0e-6.

fortran
program data_types_demo
    use iso_fortran_env, only: real64
    implicit none

    integer :: age = 30
    real(kind=real64) :: pi = 3.14159265358979_real64
    character(len=10) :: name = "Ada"
    logical :: is_valid = .true.
    complex :: z = (2.0, 3.0)

    print *, "Name:", name, "Age:", age
    print *, "Pi:", pi, "Valid:", is_valid
end program data_types_demo
  • Fortran has five intrinsic types: integer, real, complex, character, and logical.
  • Variables must be declared with a type before use, in the form type :: name.
  • The parameter attribute creates a compile-time constant that cannot be reassigned.
  • Character variables need an explicit length via len=, since strings are fixed-length by default.
  • Plain real is single precision (~6-7 digits); real(kind=8) or real64 gives double precision (~15-16 digits).
  • The iso_fortran_env module provides portable kind constants like real64 across compilers.
  • Never compare floating-point values with == due to rounding error; use a tolerance-based comparison instead.

Practice what you learned

Was this page helpful?

Topics covered

#Programming#FortranStudyNotes#FortranDataTypesAndVariables#Fortran#Data#Types#Variables#StudyNotes#SkillVeris#ExamPrep