Fortran
Online Fortran Compiler
main.f90 is compiled with gfortran from GNU Fortran 16 (-O2) as free-form Fortran, and run as a native program. Output isn’t buffered here, so a prompt written with advance='no' shows before read(*,*) waits for your input.
Try it aboveHow Fortran runs here
- Versions
- GNU Fortran 16
- Build
gfortran -O2 -o a.out main.f90- Run
./a.out- Limits per run
- 256 MB memory, 15 s of CPU time on half a core, 2 MB of output
- Time
- Up to 10 minutes a run (30 on a paid plan); waits 2 minutes for input (5 on a paid plan)
- Sandbox
- Each run gets its own Linux sandbox with no network access. Text files your program writes next to your code open as new tabs.
A Fortran program that reads input
This main.f90 reads values into an allocatable array and works on the whole array with intrinsics:
program stats implicit none integer :: n, i real, allocatable :: x(:) write (*, '(a)', advance='no') 'How many values? ' read (*, *) n allocate (x(n)) do i = 1, n write (*, '(a, i0, a)', advance='no') 'Value ', i, ': ' read (*, *) x(i) end do print '(a, f0.2)', 'Mean: ', sum(x) / n print '(a, f0.2, a, f0.2)', 'Min: ', minval(x), ', max: ', maxval(x) print '(a, i0)', 'Above the mean: ', count(x > sum(x) / n) end program stats
$ gfortran -O2 -o a.out main.f90 && ./a.out How many values? 4 Value 1: 12 Value 2: 7 Value 3: 30 Value 4: 5 Mean: 13.50 Min: 5.00, max: 30.00 Above the mean: 1
Reading input in Fortran
- read (*, *) is list-directed: numbers can be separated by spaces, commas or new lines, so read (*, *) a, b takes 3 4 on one line.
- Reading text with read (*, *) stops at the first space or comma. Use read (*, '(a)') line to get the whole line.
- print * output puts a leading space and wide columns in front of numbers. Give a format, such as '(a, i0)' or '(f0.2)', for exact output.
What isn’t available here
- Libraries: nothing beyond the Fortran runtime is linked, so no LAPACK, BLAS, OpenMP or MPI. Intrinsics such as matmul, dot_product and transpose are there.
- Several files: only main.f90 is compiled. Put your modules in it, above the program.
- Fixed-form FORTRAN 77 layout (C in column 1, code from column 7). The file is read as free form.
- Bounds checking: -fcheck isn’t on, so an index outside an array isn’t reported.
Errors you’ll see
- “Error: Symbol 'total' at (1) has no IMPLICIT type” With implicit none, every variable needs a declaration.
- “Fortran runtime error: Bad integer for item 1 in list input” read (*, *) into an integer got text, or a number with a decimal point.
- Wrong results from integer division: 7 / 2 is 3. Write 7.0 / 2 or real(a) / b.
Questions
Which Fortran compiler does this use?
GNU Fortran 16 (gfortran), compiling main.f90 with -O2.
Does FORTRAN 77 code compile?
Code in free form does. Fixed-form layout with C comments in column 1 doesn’t, because main.f90 is read as free form.
How do I read input in Fortran?
read (*, *) n for numbers, read (*, '(a)') line for a whole line of text. Type the input in the terminal while the program runs.