Your First Fortran Program
Writing your first Fortran program means creating a plain text file with a .f90 extension, since modern free-form Fortran source files conventionally use this suffix, and typing a small, complete program unit into it. A minimal but real program declares implicit none, defines any variables it needs, prints something with the print statement, and closes with end program. Unlike some languages, Fortran does not require a semicolon at the end of each line, since a newline itself terminates a statement.
Cricket analogy: Like a young batsman's very first net session focusing only on getting bat on ball, your first Fortran program focuses only on the basics: declare, print, end.
Compiling and Running the Program
Once hello.f90 is saved, compiling it is a single terminal command: gfortran hello.f90 -o hello, which produces an executable file named hello (or hello.exe on Windows). Running it is then done from the same terminal with ./hello on macOS/Linux or hello.exe on Windows. If the compiler reports errors, it will point to the specific line and column number, and the most frequent beginner mistakes are missing implicit none, forgetting to declare a variable, or mismatched parentheses in a print or format statement.
Cricket analogy: Like a bowler's action being checked by a biomechanist for legality before a match, gfortran hello.f90 -o hello checks your code for compile errors before producing something you can actually run.
Reading Compiler Error Messages
When gfortran finds a problem, it prints the file name, line number, and a caret pointing at the exact column, followed by an error description such as Error: Symbol 'x' at (1) has no IMPLICIT type. Learning to read this output methodically, starting from the very first error reported, since later errors are often just consequences of the first, is one of the fastest ways to become productive in Fortran, or in fact any compiled language.
Cricket analogy: Like a third umpire's replay showing the exact frame where the ball clips the bail, gfortran's caret points to the exact column where the error occurs.
Always fix the first error reported by the compiler first, then recompile. Later errors in the same run are frequently just cascading consequences of the first mistake, such as a missing declaration confusing everything that follows it.
program first_program
implicit none
integer :: apples
integer :: oranges
integer :: total
apples = 3
oranges = 5
total = apples + oranges
print *, "Total fruit:", total
end program first_program- Modern free-form Fortran source files conventionally use the .f90 extension.
- A minimal program has implicit none, variable declarations, executable statements, and end program.
- Compile with gfortran file.f90 -o output_name, then run with ./output_name (or output_name.exe on Windows).
- Fortran statements are terminated by a newline, not a semicolon.
- Compiler errors report the exact file, line, and column, followed by a description of the problem.
- Always fix the first reported error first, since later errors are often caused by it.
- Common beginner mistakes include missing implicit none, undeclared variables, and mismatched parentheses.
Practice what you learned
1. What file extension is conventionally used for modern free-form Fortran source files?
2. Which command compiles hello.f90 into an executable named hello?
3. How does a Fortran statement typically get terminated?
4. When a Fortran program fails to compile with multiple errors, which error should you fix first?
5. On macOS or Linux, how do you run a compiled executable named 'hello' in the current directory?
Was this page helpful?
You May Also Like
Fortran Program Structure
How a Fortran program is organized, from the program statement and implicit none through declarations, executable statements, and program units.
Fortran Data Types and Variables
An overview of Fortran's intrinsic data types, integer, real, character, logical, and complex, and how to declare and initialize variables.
Installing a Fortran Compiler
A practical guide to choosing, installing, and testing a Fortran compiler such as gfortran on Windows, macOS, and Linux.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics