Sage-Code Laboratory
index<--

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.

Integer

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.

Real

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.

Complex

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.

Character

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.

Logical

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.

Declarations

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).
example
!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
console
>./vars
          10
   3.14159274    
 A
 F T
 Fortran is cool 
>       

Expressions

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.

example
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
console
>./exp
          11
 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.

example
!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

Notes:

  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
console
>./continue 
continue 
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.


Read next: Code Structure