Kind parameters

What kind means

A kind parameter selects one variant of a type: how many digits a real holds or how large an integer can grow.

Write it in parentheses after the type name, as in real(8) or integer(4), but prefer named constants over raw numbers.

program kinddemo implicit none print *, kind(1.0) print *, kind(1.0d0) end program kinddemo

The kind function reports the kind number of any value, so these two lines show the default real kind and the double precision kind.

selected_real_kind

The selected_real_kind function asks for a real kind by required decimal digits, which keeps code portable across compilers.

Store the answer in a named integer constant and use that constant as the kind for all working variables.

program seldemo implicit none integer, parameter :: sp = selected_real_kind(6) real(sp) :: x x = 1.5_sp print *, x end program seldemo

iso_fortran_env kinds

The iso_fortran_env module provides ready made kind names such as int32, int64, real32, and real64.

Load only the names needed with the only clause, then use them exactly like kinds from selected_real_kind.

program envdemo use iso_fortran_env, only : int32, real64 implicit none integer(int32) :: n real(real64) :: y n = 7_int32 y = 2.5_real64 print *, n, y end program envdemo

The working-precision idiom

Many projects define one double precision constant named dp and use real(dp) for every float in the program.

Changing one line then changes the precision of the whole model, which is ideal for experiments and teaching.

program dpdemo implicit none integer, parameter :: dp = kind(1.0d0) real(dp) :: pi pi = 3.141592653589793_dp print *, pi end program dpdemo

The code on this page was run with gfortran 15.2.0 on Alpine Linux 3.24 (x86_64) in a Docker container.

Article author: Arthur Isaev