Variables and Types in Fortran
Learn about variables, data types, and type conversions in Fortran with practical Docker-ready examples
Fortran’s type system reflects its origins as a language built for numerical computation. Every variable must be explicitly declared with a type before use (when following the implicit none best practice), and the compiler enforces type safety at compile time. This static, strong typing catches entire categories of bugs before your program ever runs.
Fortran provides a rich set of numeric types — including built-in complex number support, something most languages delegate to libraries. The language also offers fine-grained control over numeric precision through its kind system, which lets you specify exactly how many bytes a number should occupy in memory.
In this tutorial, you’ll learn how to declare variables with all of Fortran’s intrinsic types, use constants with the parameter attribute, control numeric precision with kind specifiers, and convert between types.
Basic Variable Declarations
Fortran declares variables between implicit none and the executable statements. Each declaration specifies the type followed by :: and the variable name.
Create a file named variables.f90:
| |
Declaration Rules
- All declarations must appear before any executable statement — Fortran does not allow declaring variables mid-block.
- Character variables require a
len=specifier. If the assigned string is shorter thanlen, it gets padded with spaces;trim()removes trailing spaces. - Logical literals use
.true.and.false.(with the dots). - Complex numbers are written as
(real_part, imaginary_part).
Constants, Kind Specifiers, and Type Conversions
Fortran’s parameter attribute creates named constants that the compiler can optimize. The kind system lets you control numeric precision — essential for scientific computing where single precision may not be accurate enough.
Create a file named variables_advanced.f90:
| |
Key Points
parametermakes a variable constant — any attempt to reassign it causes a compile error.character(len=*), parameterlets the compiler determine the string length from the assigned value.selected_real_kind(p, r)requests a real type with at leastpsignificant digits and an exponent range of at leastr. This is more portable than hard-codingkind=8.selected_int_kind(r)requests an integer type that can represent values up to 10^r.- Type conversion functions:
int()truncates,nint()rounds,real()converts to real,dble()converts to double precision. - Internal I/O:
write(string, fmt)converts numbers to strings andread(string, *)parses strings to numbers — Fortran’s approach to what other languages calltoStringandparseInt.
Running with Docker
| |
Expected Output
Output from variables.f90:
count = 42
year = 1957
temperature = 98.59999
pi = 3.14159012
name = Fortran
greeting = Hello from Fortran!
is_valid = T
complex z = (3.00000000,4.00000000)
Output from variables_advanced.f90:
PI = 3.14159274
MAX_SIZE = 1000
LANG = Fortran
--- Precision comparison ---
rough_pi = 3.14159012
precise_pi = 3.1415926535897931
big_number = 9000000000000000000
--- Type conversions ---
int_val = 7
real(7) = 7.00000000
int(9.81) = 9
nint(9.81) = 10
num to str = 3.14
str to num = 256
Note: Fortran’s default list-directed output (print *, ...) includes a leading space and aligns numbers to field widths. Single-precision real values show approximately 7 significant digits, while double-precision values show approximately 15. The exact formatting may vary slightly between compilers.
Key Concepts
- All variables must be declared before use when
implicit noneis specified — this is the modern Fortran best practice that prevents silent bugs from typos. - Five intrinsic types cover most needs:
integer,real,character,logical, andcomplex(built-in complex number support is rare among programming languages). - The kind system provides portable control over numeric precision using
selected_real_kind()andselected_int_kind()rather than assuming specific byte sizes. - Constants use
parameter— the compiler enforces immutability and can optimize constants more aggressively than variables. - Declarations come first — all variable declarations must appear before executable statements, unlike C99+ or Java which allow mid-block declarations.
- Type conversions are explicit — Fortran provides intrinsic functions like
int(),real(),nint(), anddble()for conversions between numeric types. - Internal I/O (
read/writewith character variables) handles string-to-number and number-to-string conversion without external libraries.
Comments
Loading comments...
Leave a Comment