PQC Verified

Naming, Pretty Print and Integers

by gg582 · 2026-05-27 05:09:39 · 83 views · 3 min read

Table of contents

Regarding Fortran 2018

First, Fortran 2018 is essentially a different language from the classic Fortran 90. Modern Fortran can be described as a delicate balancing act between providing modern syntactic sugar, similar to Rust or Python 3, and maintaining low-level control.


Naming in Fortran

Fortran has a strong tendency to label various syntaxes and rules to make sections clear and human-friendly. As described later, there are expressions such as named conditional statements. Therefore, programs do not use regular, machine-centric patterns like the C language's main:

int main() {
    return 0;
}

Unit Division of 'Fortran Programs'

Fortran programs have a strong concept of units, clearly defining the boundaries of "this is where the program exists." In other words, the start and end of the program are visually distinct.

The expression is as follows:

program hello_program
end program hello_program

It progresses in a manner close to natural language.

In line with Fortran's current standards, coding is performed while ensuring environment settings strictly adhere to ISO Fortran 2018. The error_unit is a unit for managing standard error; writing to this buffer will output logs to the standard error stream.

program hello_program
    use, intrinsic :: iso_fortran_env, only: error_unit
    implicit none

This is the standard approach.

For output, "Pretty Print" is supported. There is the write statement, which can target various buffers, and the print statement, which outputs to standard output.

print is used as follows:

print *, "Hello World!"

In the case of write, you can specify the buffer. This example outputs a log line to the standard error output:

write(error_unit, *) "This is an error message layout."


Comments

In Fortran, single-line comments are marked using an exclamation mark (!). They are written as follows:

! This is a comment.

Code

This is an example Fortran program incorporating all the points mentioned above:

program hello_world
    ! Load the standard Fortran environment.
    ! Use the ISO standard Fortran environment and import the error unit for error stream utilization.
    ! Define 'implicit none' to eliminate implicit typing.
    use, intrinsic :: iso_fortran_env, only: error_unit
    implicit none

    ! Output the statement to standard output.
    ! The '*' automatically formats and outputs the statement nicely.

    print *, "Hello World!"

    ! Output to the error unit, formatted nicely via '*'.

    write(error_unit, *) "This is an error message layout."
end program hello_world

This program can be built using GCC. A simple GNU Makefile can be written as follows:

CC=gfortran

all:
    $(CC) -std=f2018 hello_world.f90 -o hello_world

Afterward, simply run the make command.

Integers

When you specify an integer's size, all you need is iso_fotran_env.

This program specifies its size.

program integers

use, intrinsic :: iso_fortran_env

! we have int8, int16, int32, int64, int128
integer(int8) :: a
end program

However, just specifying integer's decimal size will do the same feature while making it easy to understand.

program integers_dec
    use, intrinsic :: iso_fortran_env
    ! at least it should be capable of 99999.
    ! as a result it becomes int32.
    integer, parameter :: k5 = selected_int_kind(9)
end program

Integer Range Table

Type Size Range (Min to Max) selected_int_kind Digits
int8 8 bits (1 byte) -128 to 127 2
int16 16 bits (2 bytes) -32,768 to 32,767 4
int32 32 bits (4 bytes) -2,147,483,648 to 2,147,483,647 9
int64 64 bits (8 bytes) -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 18
int128 128 bits (16 bytes) \approx -1.7014 \times 10^{38} to 1.7014 \times 10^{38}<br>

<br>(-2^{127} to 2^{127}-1) | 38 |

Some compilers doesn't implement int128. But GFortran and Clang implements this type.

Related posts

Back
Report

Comments

No comments yet.