TechTorch

Location:HOME > Technology > content

Technology

A Beginners Guide to Writing Hello, World in Fortran

January 10, 2025Technology4048
A Beginners Guide to Writing Hello, World in FortranFortran, short for

A Beginner's Guide to Writing 'Hello, World' in Fortran

Fortran, short for Formula Translation, is one of the oldest high-level programming languages and remains a powerful tool for scientific computing and numerical analysis. A common exercise to get started with any programming language is to write the classic "Hello, World!" program. Let's explore how to do this in Fortran.

Overview of Fortran

Fortran was first developed in the 1950s and has undergone several revisions over the years. The language continues to evolve, providing modern features while maintaining compatibility with older code. In this tutorial, we will use a modern version of Fortran, such as Fortran 95 or higher, which supports the syntax you'll see in the example below.

Writing "Hello, World" in Fortran

The "Hello, World" program in Fortran is remarkably concise and straightforward. Here's a simple example:

program p  print *, "Hello, World!"end program p

This program consists of a single program unit, `program p`, which contains a print statement followed by the `end program p` directive to close the program.

The `print` statement is used to output text to the console. The `*` tells the compiler that the output should be sent to the default output device (usually the console). You can also use other specifiers to control the format of the output, such as `A` for character strings, `I` for integers, `F` for floating-point numbers, and so on.

Understanding the Code

program p: This line marks the start of the program. The name `p` is the name of the program, and it can be any valid Fortran identifier.

print *, "Hello, World!": This line prints the string "Hello, World!" to the console. The `*` means "the standard output unit".

end program p: This line marks the end of the program. It is a directive to the compiler to end the program unit.

Running the Code

To run this program, you need a Fortran compiler such as GNU Fortran, Intel Fortran, or Oracle Fortran. You can compile the program using the command line or an integrated development environment (IDE).

Here are the steps to compile and run the program:

Save the code to a file with a suitable extension, such as `hello_world.f95`.Open a terminal or command to the directory where you saved the the program using a Fortran compiler. For example, if you are using GNU Fortran:
gfortran -o hello_world hello_world.f95
To run the program, simply type the compiled program's name followed by the `.` and the extension:
./hello_world

Advanced Fortran "Hello, World" Examples

Now that you have written and run a simple "Hello, World" program, you might be curious about more advanced examples. Here are a few variations:

Modified Output: You can add formatting to the output, for example, using new lines or tabs:
program p  print *, "Hello, World!"  print *, "This is a new line"end program p
Printing to a File: To print to a file instead of the console, you can redirect the output to a file:
program p  open(unit10, file"output.txt", status"unknown")  write(10, *) "Hello, World!"  close(10)end program p
Interactive Input: You can also take input from the user:
program p  read *, name  print *, "Hello, " // nameend program p

Conclusion

Fortran is still an important language in the scientific community, and as you can see, writing "Hello, World!" in Fortran is not only easy but also a good way to familiarize yourself with the basic syntax and concepts of the language. This tutorial provides a brief introduction, but there is much more to explore in Fortran.

Whether you are a programmer interested in scientific computing or a student learning about programming languages, Fortran offers a rich and powerful environment to work in. So why not try your hand at writing more complex programs?