The five intrinsic types

Fortran has five intrinsic types and no others. Everything else in the language is built from them: a derived type is a record of these five, an array is a shape around one of them, and a module is a place to keep them. This lesson goes through the five, the literal that writes a value of each, and the two numbers the compiler keeps about a type: its kind and, for characters, its length.

The five types

One program declares all five, prints a value of each, and asks the compiler what kind each value has. The kind intrinsic returns the kind value, and len returns the length of a character value. Both numbers are the compiler's answer on this machine and not a constant of the language.

The file types.f90:

program types implicit none integer :: i real :: x complex :: z logical :: ok character(len=10) :: word i = -42 x = 0.1 z = (1.0, -2.0) ok = .true. word = "Fortran" print *, "integer :", i print *, "real :", x print *, "complex :", z print *, "logical :", ok print *, "character:", word print *, "kinds :", kind(i), kind(x), kind(z), kind(ok), len(word) end program types

gfortran -Wall -Wextra -std=f2018 types.f90 -o types
./types

integer : -42
real : 0.100000001
complex : (1.00000000,-2.00000000)
logical : T
character:Fortran
kinds : 4 4 4 4 10

Four of the five kinds are 4 and the character length is 10. The real value is not exactly 0.1: a 4-byte real holds about seven decimal digits, and the nearest binary value to one tenth prints as 0.100000001. That is not a bug in the print statement, and the lesson on real arithmetic is about it.

INTEGER

A whole number. The default kind is 4 bytes on every platform this course runs on, which gives about nine decimal digits and a range from -2147483648 to 2147483647; the huge and range intrinsics report the exact limits. A literal is written as digits, with an optional sign and an underscore suffix for the kind: 42, 42_int64.

Integer arithmetic is exact until it overflows, and overflow wraps silently. That makes the choice of kind a design decision for anything that counts bytes, not a detail; the lesson on integer arithmetic shows the wrap.

REAL

An approximation of a real number, stored as a sign, an exponent and a mantissa. The default kind is 4 bytes: about seven significant digits, with a relative error of one part in ten million. The literal needs a decimal point or an exponent: 0.1, 1.0e3 and 1.0d3 are reals, while 1 is an integer.

Two reals of the same value need not compare equal after arithmetic, and that is a property of binary floating point, not of Fortran.

COMPLEX

A pair of reals, real part and imaginary part, of the same kind. The literal is a parenthesised pair: (1.0, -2.0). Arithmetic is the arithmetic of complex numbers, written with the ordinary operators: +, -, *, / and **. The intrinsics real, aimag, conjg and abs reach into the parts.

List-directed output prints the pair in parentheses with a comma, as the line above shows. A complex array of a million elements is 8 megabytes at the default kind, which is why a transform is usually the reason a Fortran program exists.

LOGICAL

True or false, written .true. and .false. with dots on both sides so the compiler can tell them from names. The operators are .and., .or., .not., .eqv. and .neqv., and the relational operators produce logical values.

List-directed output prints T and F, one letter, without the dots. A logical is not an integer in Fortran: there is no conversion between the two, and if (1) is a compile error. That strictness is deliberate and it is the opposite of C.

CHARACTER

A string of fixed length. The length is part of the type, written character(len=10), and a value shorter than the length is padded with blanks on the right. There is no terminator and no length stored with the value: the compiler knows the length from the declaration. Comparison ignores the trailing blanks, which is why "abc" == "abc " is true.

The literal is quoted with single or double quotes, and a quote inside a string is written twice: "it""s" is the four characters it"s. A substring is written with a colon, word(1:3), and it is a value, not a pointer into the string.

Literals of all five

One more program, this time with the literal as the argument of the print. It is the shortest way to see how the compiler reads each form.

The file literals.f90:

program literals implicit none print *, "integer :", 42 print *, "real :", 0.5 print *, "exponent:", 1.0e3 print *, "complex :", (1.0, -2.0) print *, "logical :", .false. print *, "quoted :", "it""s" end program literals

gfortran -Wall -Wextra -std=f2018 literals.f90 -o literals
./literals

integer : 42
real : 0.500000000
exponent: 1000.00000
complex : (1.00000000,-2.00000000)
logical : F
quoted :it"s

Three of those lines are worth the second look. 1.0e3 is a real and prints as 1000.00000, not as an integer. The complex pair keeps its parentheses and its comma. And the doubled quote became one quote in the value, which is how a string holds a quote character.

Exercises

  1. Print huge(0) and tiny(1.0) in the types program. Which type does each answer?
  2. Declare a complex variable and print real(z) and aimag(z). What separates them?
  3. Change character(len=10) to character(len=3) and assign "Fortran". What is stored, and does it build?
  4. Print .true. .and. .false. and .not. .true.. Which one is which?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. How many intrinsic types does Fortran have?

    Answer

    Five: integer, real, complex, logical and character

    Every other type in a program is built from those five.

  2. What is the default kind of a real on this compiler?

    Answer

    4, which means about seven significant digits

    The default real is single precision and holds about seven decimal digits.

  3. How is a complex literal written?

    Answer

    (1.0, -2.0)

    A parenthesised pair of reals is the complex literal.

  4. How does print * show a logical value?

    Answer

    T or F, one letter

    List-directed output prints one letter without the dots.

  5. What happens to a string shorter than the declared length?

    Answer

    It is padded with blanks on the right

    Every character value has its declared length, so a short value is blank-padded.

  6. How is a quote character written inside a string literal?

    Answer

    Doubled, as "it""s"

    Fortran doubles the quote; there is no backslash escape in a literal.

  7. What does this program print? program p implicit none print *, 1.0e3 end program p

    Answer

    1000.00000

    The literal has a decimal point and an exponent, so it is a real and prints as one.

  8. What does kind(1.0) return?

    Answer

    The kind value of the default real

    kind returns the kind number, which is 4 for the default real here.

  9. Which comparison is true in Fortran?

    Answer

    "abc" == "abc "

    Character comparison ignores trailing blanks and is case sensitive.

  10. Why is if (1) an error in Fortran?

    Answer

    A logical is not an integer, and there is no conversion between them

    Fortran keeps the two types apart, so a condition must be a logical expression.

The programs on this page were built with GNU Fortran (Alpine 15.2.0) 15.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev

Banner image