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.
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
1. Which of these is NOT one of Fortran's five intrinsic data types?
2. What does the parameter attribute do when declaring a variable?
3. Why must character variables typically specify a length with len=?
4. What module provides the portable real64 kind constant for double precision?
5. Why should you avoid comparing two real (floating-point) values with == in Fortran?
Was this page helpful?
You May Also Like
Fortran Program Structure
How a Fortran program is organized, from the program statement and implicit none through declarations, executable statements, and program units.
Your First Fortran Program
A hands-on walkthrough of writing, compiling, and running a simple Fortran program, from source file to executable.
What Is Fortran?
An introduction to Fortran, the pioneering language built for numerical and scientific computing that still powers weather models and supercomputers today.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics