Sage-Code Laboratory

Fortran Data Types

Fortran is a statically typed language. That means you need to declare data type for variables. Once establish, data type can't be changed during program runtime. To change data type you need to modify the source code and recompile the project.

Data Literals

Constant data can be represented as data literals. This is a special notation that represent different kind of data. For every data literal there is a default type that is assigned automatically by the compiler. Data literals can't be changed during runtime. They are embedded in source code.

Basic Types

Fortran, supports five intrinsic data types. The most frequently used data types are integer and floating-point. Other data types are complex numbers, characters and logical data.

Type Description
integer A whole number (not a fraction) that can be positive, negative, or zero.
real Variable that can be set to a real number.
complex Variable that can be set to a complex number.
character Variable that is a character or sequence of characters.
logical Variable that can only be set to .true. or .false.


An integer is a whole number (not a fraction) that can be positive, negative, or zero. Examples include the numbers 10, 0, -30 Integers are the numbers people are most familiar with, and they serve a crucial role in mathematics and computers. Division of integers produce integer numbers. So 1 divided by two (1/2) is 0 since the result must be a whole number.


A real number includes the fractional part, even if the fractional part is 0. Real numbers, also referred to as floating-point numbers, include both rational numbers and irrational numbers.

Examples of irrational numbers or numbers with repeating decimals include π and e. Additional examples include literals: 1.5, 5.0, and 3.14159. Fortran 95/2003/2008 will accept 5. as 5.0 but the examples in this text will include the .0 to ensure clarity.


A complex number, in mathematics, is a number comprising a real number and an imaginary number. It can be written in the form of a + bi, where a and b are real numbers, and the i is the standard imaginary unit with the property that i^2 = −1.0. The complex numbers contain the ordinary real numbers, but extend them by adding in extra numbers like an extra dimension. This data type is not used extensively, but can be useful when needed.


A character is a symbol like a letter, numerical digit, or punctuation. A string is a sequence or set of characters. Characters and strings are typically enclosed in quotes. For example, the upper case letter "Z" is a character and "Hello World" is a string. The characters are represented in a standardized format referred to as ASCII.


A logical is only allowed to have two values, true or false. A logical can also be referred to as a Boolean. In Fortran, the true and false values are formally expressed as .true. or .false. which are also called logical constants. The leading and trailing . (period) are required for the true and false constants.


Fortran has implicit and explicit data declarations. You can decide to disable implicit data types. If you do so, data checking is more tight so you end-up with less logical errors, but you have to declare data type for all variables, parameters and results.

Declaring variables formally defines the data type of each variable and sets aside a memory location. This is performed by a type declaration statement in the form of: :: The type must be one of the predefined data types (integer, real, complex, character, logical) as outlined in the previous section. Declarations are placed in the beginning of the program (after the program statement).
!fortran example
program variables
  implicit none

  integer :: amount
  real :: pi
  complex :: frequency
  character :: initial
  logical :: isOkay
  character(len=20):: str

  amount = 10
  pi = 3.1415927
  frequency = (1.0, -0.5)
  initial = 'A'
  isOkay = .false.

  print *, amount
  print *, pi
  print *, initial
  print *, isOkay, .true.

  str = "Fortran is cool"
  print *, str
end program variables
 F T
 Fortran is cool 


Expressions are sequence of identifiers, operators, separators and data literals. Each expression can produce a result of a specific data type that is determined by the compiler. You can use the expression result in a statement.

program expressions
  integer:: a = 10, b = 3
  real:: r = .0
  print *, a + 1
  print *, "a/b =", a/b
  r = (a + 2)/b
  print *, "r = ", r
  e = a ** b
  print *, "e = ", e
end program
 a/b =           3
 r =    4.00000000    
 e =    1000.00000  

Notes: Expressions can be combined together to create larger expressions. In a larger expression, the order of operations is important. It is dictated by the operator priority. You can modify the order of execution using round parenthesis.

Arithmetic Expressions

In Fortran we use infix expressions. That means operator is between two operands like (x + y), therefore most operators are called binary operators. Sometimes there is only one operand, in this case the operator is "unary" operators.

Math operators

Obvious arithmetic operators are used in numeric expressions. We have a problem with the computers the number of operators is limited to the number of legacy ASCII special symbols that is very limited. We can't use Unicode symbols as operators in Fortran.

Symbol Description Given Expression Result
- Sign change a=10 -a -10
- Subtraction a=10, b=3 a - b 7
+ Addition a=10 a + 2 12
* Multiplication a=10, b=0.5 a * b 5.0
/ Division a=10, b=3 a/b 3
** Exponential a=10, b=3 a ** b 1000

Logic expressions

Before creating conditionals you need to understand logical expressions. These are evaluated to values {.true. or .false.} in old Fortran {.T. or .F.}. You can logical and relation operators with variables, constants and function calls to create logical expressions.

Logic operators

Logic operators are based on keywords. These special keywords are enclosed between two dots. Is a little bit annoing but you can get use to it eventually.

Symbol Description A B Expression Result
.not. Logic NOT .false. .not. A .true.
.true. .not. B .false.
.and. Logic AND .true. .false. A .and. B .false.
.true. .true. A .and. B .true.
.or. Logic OR .true. .false. A .or. B .true.
.false. .false. A .or. B .false.
.eqv. Equivalent .true. .false. A .eqv. B .false.
.false. .false. A .eqv. B .true.

Relation operators

Fortran operators have changed from old syntax 77 to new syntax 90 and beyond. We use these operators to create logic expressions that return a Boolean value. You can compare numbers or characters.

New Fortran Old Fortran Description Example
< .LT. Less than a < b
> .GT. Greater than a > b
>= .GE. Greater than or equal a >= b
<= .LE. Less than or equal a <= b
== .EQ. Equal a == b
/= .NE. Not equal a /= b

String expressions

In Fotran, strings are ASCII encoded. You can have string literals enclosed in single or double quoted delimiters. A delimiter must be duplicated inside string to represent the symbol itself. Usually text strings are double quoted because English has ' used in words.

Concatenation: "//"

Strings can be joined together using special operator double slash, that is usually a comment in other languages. This is why Fortran is a little bit strange. Remember, comments are starting with bang symbol "!" and this is also unusual.

!this is fortran 
program string
  implicit none

  character(len=4) :: first_name
  character(len=5) :: last_name
  character(10) :: full_name

  first_name = 'John'
  last_name = 'Smith'

  ! String concatenation
  full_name = first_name//' '//last_name

  print *, full_name

  print *, "log strings &
  can span multiple lines
  if you use operator && 

end program string


  1. Numbers and strings can't be concatenated together like in JavScript or Python. You need to convert the numbers into strings before concatenation, but this is not straight forward.
  2. In Fortran standard there is no escape sequence like in C. Therefore you must use function achar() to produce special characters and the you can concatenate a special character in the string.

Continuation: "&"

In Fortrand longer strings can be represented on multiple lines if each line is ending with single ampersand symbol. This is the continuation operator and is not part of the string.

example: continue.f95
program main

! escape character
character :: lf = achar(10) 

! this statement does nothing
if (.true.) continue

! long string on multiline
print "(a)", &   
  "continue " //lf//&
  "can be used "     //lf//&
  "wen a statement " //lf//&
  "is required "     //lf//&
  "but there is "    //lf//&
  "nothing to do there!"
end program
can be used 
wen a statement 
is required 
but there is 
nothing to do there!

Notes: Symbol "&" must be used together with "//" in the example above, otherwise the strings are not concatenated and this is correct but it could have been done better.

Data Input/Output

Input/Output operations are tightly related to data types. Usually input/output can be from console or from file data fields. There are 3 statement for performing I/O.

read, write, print 

These operations require format specification, and can output a list of variables, constants or expressions. The arguments are separated by comma.

Format specification

Data format can be simple as "*" that is handled automatically by the system and it supports many variables. Better format is a string containing a set of rule that you can create for different data types. Check the example:

!fortran example
program main
  real:: r = 225.502
  integer:: i = 25
  character(len=*):: t= "Test"
  fmt='(a," ", i5.4," ", f10.5)'
  print fmt, trim(t), i, r
end program
Test  0025  225.50200

Notes: In the example above you can see 3 variables of different kind can be combined using a single format string that has inside 3 specifications separated by comma. You will get many errors until you get the format right. The format is very powerful feature in Fortran. If you learn it right you can create a report quickly from your data.

Format Rules

Rule format is a string with the following convention: It must be enclosed in round brackets like: "( ... )" or '( ... )'. The specification can hold rules and arbitrary text. The text must be enclosed in alternative quotes, "" or '' depending on outer quotes.

Format Description
"..." Inserts arbitrary characters.
Iw[.m] Read/write w characters as integer number, with optional number of leading zeros m.
Bw[.m] Read/write w characters as binary values, with optional number of leading zeros m.
Ow[.m] Read/write w characters as octal values, with optional number of leading zeros m.
Zw[.m] Read/write w characters as hexadecimal values, with optional number of leading zeros m.
Fw.d Read/write floating-point number in decimal notation, with w digits, of which d is the number of decimal places. Sign and floating point must be regarded in w. F0.d allows a variable length.
Ew.d Read/write floating-point number in exponential notation, with w characters and a mantissa of d digits..
EXw.d[Ee]Read/write real value in hexadecimal, with field width w, the number of hexadecimal digits in d, and the optional exponent e (Fortran 2018).
Dw.d Read/write Floating-point number, double precision.
A, Aw. Read/write arbitrary length string, or fixed string with w characters
Lw Read/write w characters as logical.
nX Read: Ignore the next n characters.
nX Write: Print n spaces."
Tc Puts next character at position c in line.
/ Force a line feed that makes rule o 2 rows.

Rule Multiplier

The rule can be multiplied like this: nR where n is an integer number. It will create n column having same rule R. The rule R use to start with a capital letter but new format works with lowercase letters as well.

Read next: Code Structure