ZetCode

Fortran literals and constants

last modified November 14, 2026

A literal is a value written directly in the source text, such as 42 or 'text'. Every literal has a type and a kind, and Fortran lets the program choose the kind with a suffix. This article goes through the literals of all five intrinsic types, a trap with real literals, BOZ constants, and named constants.

Integer literals

An integer literal is a string of digits with an optional sign. Without a suffix it has the default kind. A suffix of the form _kind selects another kind, where kind is a named constant or a number, so 100_int8 is an 8-bit integer. The kind constants come from iso_fortran_env, as described in the data types article.

integers.f90
program integers
    use, intrinsic :: iso_fortran_env, only: int8, int64
    implicit none
    integer :: a
    integer(int8) :: b
    integer(int64) :: c

    a = 42
    b = 100_int8
    c = 10000000000_int64

    print *, a, b, c
    print *, 'kinds:', kind(42), kind(42_int8), kind(42_int64)
    print *, -17, +17
end program integers

The program stores a literal in each of three integer kinds and prints them, and then prints the kinds of the literals themselves. A literal without a suffix has kind 4, 42_int8 has kind 1, and 42_int64 has kind 8. A leading minus or plus sign is not part of the literal but a unary operator applied to it.

$ gfortran integers.f90 -o integers
$ ./integers
          42  100          10000000000
 kinds:           4           1           8
         -17          17

Integer literals that are too large

The kind of a literal comes from the literal itself and not from the variable it is assigned to. A number that is too large for the default kind is an error, even when the variable is wide enough.

bigint.f90
program bigint
    use, intrinsic :: iso_fortran_env, only: int64
    implicit none
    integer(int64) :: c

    c = 10000000000
    print *, c
end program bigint

The variable c has 64 bits, but the literal 10000000000 is a default integer, which is 32 bits wide. The compiler detects that the value does not fit and refuses to compile the statement.

$ gfortran bigint.f90 -o bigint
bigint.f90:6:19:

    6 |     c = 10000000000
      |                   1
Error: Integer too big for its kind at (1). This check can be disabled with the option ‘-fno-range-check’

Giving a literal the right kind

The cure is a suffix, so that the literal itself has the wide kind. The same program with the suffix compiles and runs.

bigint_fixed.f90
program bigint_fixed
    use, intrinsic :: iso_fortran_env, only: int64
    implicit none
    integer(int64) :: c

    c = 10000000000_int64
    print *, c
end program bigint_fixed

With _int64 after the digits the literal has kind 8 and the assignment compiles. The same applies to the arguments of procedures and to constants in expressions: the suffix belongs to the literal.

$ gfortran bigint_fixed.f90 -o bigint_fixed
$ ./bigint_fixed
          10000000000

Real literals

A real literal has a decimal point, an exponent, or both. The digits before or after the point may be left out, so 1. and .5 are valid. The exponent is introduced by e or E. In FORTRAN 77 the letter d started the exponent of a double-precision literal, and it still does: 1.5d3 is a literal of kind 8. A suffix also works for reals, as in 1.5_real64.

reals.f90
program reals
    use, intrinsic :: iso_fortran_env, only: real32, real64
    implicit none

    print *, 3.14, 1., .5, 1.5e3, -2.5E-2
    print *, 'kind of 1.5:         ', kind(1.5)
    print *, 'kind of 1.5e0:       ', kind(1.5e0)
    print *, 'kind of 1.5d0:       ', kind(1.5d0)
    print *, 'kind of 1.5_real32:  ', kind(1.5_real32)
    print *, 'kind of 1.5_real64:  ', kind(1.5_real64)
    print *, 'kind of 1.5e3_real64:', kind(1.5e3_real64)
    print *, 1.5d3, 1.5e3_real64
end program reals

The first line prints the five literals in several forms. The following lines show the kind of each form: 1.5 and 1.5e0 have the default kind 4, 1.5d0 has kind 8, and the suffix chooses the kind explicitly. The last line prints a literal with a d exponent and one with the _real64 suffix; both are the same double-precision number, 1500.0, shown with all its digits.

$ gfortran reals.f90 -o reals
$ ./reals
   3.14000010       1.00000000      0.500000000       1500.00000      -2.50000004E-02
 kind of 1.5:                    4
 kind of 1.5e0:                  4
 kind of 1.5d0:                  8
 kind of 1.5_real32:             4
 kind of 1.5_real64:             8
 kind of 1.5e3_real64:           8
   1500.0000000000000        1500.0000000000000

The widening trap

A real literal without a suffix is a single-precision number, even when it is assigned to a double-precision variable. The compiler converts the literal at compile time, from the value that single precision could hold. The extra digits are not there to recover: they were lost before the assignment.

widening.f90
program widening
    use, intrinsic :: iso_fortran_env, only: real64
    implicit none
    real(real64) :: wrong, right

    wrong = 0.1
    right = 0.1_real64

    write (*, '(a, es25.17e3)') 'wrong:      ', wrong
    write (*, '(a, es25.17e3)') 'right:      ', right
    write (*, '(a, es25.17e3)') 'difference: ', wrong - right
end program widening

Both variables are 64-bit reals, and both are assigned the literal 0.1. The variable wrong receives the single-precision approximation of 0.1, which is 0.10000000149..., so it differs from the correct value by about 1.5e-9. The variable right receives 0.1_real64 and is exact to double precision. This is the most frequent precision bug in Fortran programs. The rule is to put the kind suffix on every real literal in a program that uses a kind other than the default.

$ gfortran widening.f90 -o widening
$ ./widening
wrong:       1.00000001490116119E-001
right:       1.00000000000000006E-001
difference:  1.49011611383365050E-009

BOZ constants

Binary, octal and hexadecimal constants are written with the letters b, o and z followed by digits in quotes: b'1010', o'17' and z'FF'. They have no type of their own, and the standard allows them in data statements and as arguments of a few intrinsic functions, such as int, real, dble and cmplx. The function int converts the bit pattern to an integer of the requested kind.

boz.f90
program boz
    use, intrinsic :: iso_fortran_env, only: int64
    implicit none
    integer :: a, b, c
    integer(int64) :: d

    a = int(b'1010')
    b = int(o'17')
    c = int(z'FF')
    d = int(z'FFFFFFFF', int64)

    print *, a, b, c, d
end program boz

The program converts a binary, an octal and a hexadecimal constant to default integers. The last conversion gives int a kind, so the hexadecimal value FFFFFFFF becomes the 64-bit integer 4294967295, which would not fit in 32 bits as a positive number.

$ gfortran boz.f90 -o boz
$ ./boz
          10          15         255           4294967295

Assigning a BOZ constant

Assigning a BOZ constant directly to a variable, as many older programs do, is rejected by gfortran in the default mode, because the standard does not allow it.

boz2.f90
program boz_assign
    implicit none
    integer :: x

    x = z'FF'
    print *, x
end program boz_assign

The program assigns z'FF' to an integer without a conversion function. The compiler explains that the constant must be a data statement value or an argument of one of the conversion functions, and it also names the option -fallow-invalid-boz, which turns the error into a warning for legacy code. The error is printed twice; the listing shows the first message only.

$ gfortran boz2.f90 -o boz2
boz2.f90:5:8:

    5 |     x = z'FF'
      |        1
Error: BOZ literal constant at (1) is neither a DATA statement value nor an actual argument of INT/REAL/DBLE/CMPLX intrinsic subprogram [see ‘-fno-allow-invalid-boz’]

Logical literals

There are two logical literals, .true. and .false.. The dots are part of the name. A logical value also arises from a relational expression such as 5 > 3. Relational operators have two forms: the symbols ==, /=, <, <=, >, >= and the older words .eq., .ne., .lt., .le., .gt., .ge..

logicals.f90
program logicals
    implicit none
    logical :: yes, no

    yes = .true.
    no = .false.
    print *, yes, no
    print *, 5 > 3, 5 < 3
    print *, kind(.true.)
end program logicals

Logical values print as T and F. The relational expressions 5 > 3 and 5 < 3 yield the same two values. The last line prints the kind of .true., which is the default logical kind, 4 in gfortran.

$ gfortran logicals.f90 -o logicals
$ ./logicals
 T F
 T F
           4

Character literals

A character literal is text in single or double quotes. The two quote styles are equivalent, which helps when the text itself contains one of them: either use the other kind of quote or double the quote character inside the text. Fortran has no escape sequences, so a backslash is an ordinary character.

charlit.f90
program charlit
    implicit none
    character(len=*), parameter :: single = 'single quotes'
    character(len=*), parameter :: double = "double quotes"
    character(len=*), parameter :: apostrophe = 'It''s a quote'
    character(len=*), parameter :: inner = "He said ""hi"""
    character(len=*), parameter :: other = "It's easy to mix"
    character(len=*), parameter :: backslash = 'C:\temp\new'

    print *, single
    print *, double
    print *, apostrophe
    print *, inner
    print *, other
    print *, backslash
    print *, len(apostrophe), len(inner), len('')
    print *, 'first' // ' ' // 'second'
end program charlit

The program shows each way of writing a quote. 'It''s a quote' doubles the apostrophe, "He said ""hi""" doubles the double quote, and "It's easy to mix" avoids both by choosing the other quote style. The path 'C:\temp\new' is printed unchanged, because the backslash does not start an escape. The empty literal '' has length zero, and the operator // concatenates literals.

$ gfortran charlit.f90 -o charlit
$ ./charlit
 single quotes
 double quotes
 It's a quote
 He said "hi"
 It's easy to mix
 C:\temp\new
          12          12           0
 first second

Special characters

Since a literal cannot hold a newline or a tab, they are built from character functions: new_line('a') returns the newline character, achar(n) the character with the ASCII code n.

newline.f90
program newline_demo
    implicit none

    print '(a)', 'one' // new_line('a') // 'two'
    print '(a)', 'tab:' // achar(9) // 'here'
    print '(a)', 'quote char: ' // achar(34) // ' and ' // achar(39)
end program newline_demo

The first statement joins two words with a newline, so the output has two lines. The tab has the code 9 and is shown as a gap. The codes 34 and 39 give the double and the single quote. The format '(a)' prints the text without the leading blank of list-directed output.

$ gfortran newline.f90 -o newline
$ ./newline
one
two
tab:	here
quote char: " and '

Complex literals

A complex literal is a pair of real literals in parentheses: the real part and the imaginary part. The kind of the literal is the larger kind of the two parts. The parts must be constants; to build a complex value from variables, use the function cmplx(x, y).

complexlit.f90
program complexlit
    use, intrinsic :: iso_fortran_env, only: real64
    implicit none
    complex :: z
    complex(real64) :: w

    z = (1.0, -2.0)
    w = (1.0_real64, -2.0_real64)
    print *, z
    print *, w
    print *, kind(z), kind(w)
end program complexlit

Both variables hold the value 1 - 2i. The first literal has default real parts, so it is a complex number of kind 4. The second has parts with the suffix _real64, and so it has kind 8 and prints 16 digits.

$ gfortran complexlit.f90 -o complexlit
$ ./complexlit
            (1.00000000,-2.00000000)
              (1.0000000000000000,-2.0000000000000000)
           4           8

Named constants

A named constant is declared with the parameter attribute and must have an initial value. The value may be any constant expression: literals, other named constants, and calls of intrinsic functions such as atan. The classic way to get an accurate pi is 4 * atan(1) in the kind that the program needs. A constant can also be an array, written as a list in square brackets, and a character constant can take its length from the value with len=*.

named.f90
program named
    use, intrinsic :: iso_fortran_env, only: real64
    implicit none
    real(real64), parameter :: pi = 4.0_real64 * atan(1.0_real64)
    real(real64), parameter :: two_pi = 2.0_real64 * pi
    integer, parameter :: n = 5
    integer, parameter :: squares(n) = [1, 4, 9, 16, 25]
    character(len=*), parameter :: greeting = 'Hello'

    print *, 'pi =    ', pi
    print *, '2 pi =  ', two_pi
    print *, 'n =     ', n, ' sum =', sum(squares)
    print *, greeting, len(greeting)
end program named

The program computes pi in double precision from the arctangent of 1 and builds two_pi from it. squares is an integer array constant with five elements whose size comes from the constant n. The character constant greeting has the length 5 that the compiler counts from the literal. The sum function adds up the array elements: 1 + 4 + 9 + 16 + 25 = 55.

$ gfortran named.f90 -o named
$ ./named
 pi =       3.1415926535897931
 2 pi =     6.2831853071795862
 n =                5  sum =          55
 Hello           5

Constants in declarations

Named constants can be used wherever a constant expression is required, and the most important place is the declaration of an array: its bounds must be known before the program runs, unless the array is allocatable. A constant defined from other constants makes the sizes consistent.

constexpr.f90
program constexpr
    implicit none
    integer, parameter :: rows = 3, cols = 4
    integer, parameter :: cells = rows * cols
    integer :: grid(rows, cols)
    character(len=*), parameter :: label = 'grid'

    grid = 0
    print *, label, ' has', cells, 'cells, size =', size(grid)
    print *, 'bits in default integer:', bit_size(cells)
end program constexpr

The constants rows and cols set the shape of grid, and cells is computed from them at compile time. The program prints the number of cells, which equals the size of the array, and the number of bits of a default integer. Changing one of the two constants changes everything that depends on it.

$ gfortran constexpr.f90 -o constexpr
$ ./constexpr
 grid has          12 cells, size =          12
 bits in default integer:          32

Next steps

The data types and kinds article describes what the kinds in this article mean, and Fortran variables and declarations covers declarations and the parameter attribute in more detail. The next article, Fortran operators and expressions, combines the values described here with the operators of the language.

Source

GNU Fortran: BOZ literal constants

GNU Fortran: the ISO_FORTRAN_ENV module

Fortran 2018 standard (J3/18-007r1)

In this article we have written integer, real, logical, character, and complex literals, chosen their kinds with suffixes, avoided the widening trap of real literals, used BOZ constants, and declared named constants. The previous article, Fortran data types and kinds, describes the types themselves. The series continues with Fortran operators and expressions, which combines these values with the operators of the language.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Fortran tutorials.