1. Hello World
by
gg582 · 2026-05-27 05:07:49 · 21 views
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.