gfortran

Implementation of programming language Fortran

gfortran is GNU Fortran compiler, which is included in GNU Compiler Collection. It replaced g77 since GCC 4.0.

gfortran currently supports Fortran 95 and Fortran 2003 dialects.

GCC logo
GCC logo

Examples:

Factorial:

Example for versions Intel Visual Fortran 11.1, gfortran 4.5.0

This example uses iterative factorial definition. Format specifiers I and A are used for printing integers and strings, respectively. Trying to calculate 13! produces an arithmetic overflow error, so last lines of program output look like this:

13! = 1932053504
14! = 1278945280
15! = 2004310016
16! = 2004189184

    program Factorial

    integer :: f,n

    f = 1
    n = 0
    
    do
        print '(I2, A, I10)', n, "! = ", f
        n = n + 1
        f = f * n
        if (n==17) then
            exit
        end if
    end do

    end program Factorial

Fibonacci numbers:

Example for versions Intel Visual Fortran 11.1, gfortran 4.5.0

This example uses iterative definition of Fibonacci numbers. The tricky part is printing the calculated values in one line, without leading or trailing spaces. Format specifier (I3, A, $) means that first an integer is printed using exactly 3 positions, followed by a string. The final $ suppresses trailing carriage return, used by default, so that everything is printed in one line.

    program Fibonacci

    integer :: f1,f2,f3,i

    i = 1
    f1 = 0
    f2 = 1
    
    do
        f3 = f2 + f1
        f1 = f2
        f2 = f3
        i = i + 1
        if (f1<10) then
            print '(I1, A, $)', f1, ', '
        elseif (f1<100) then
            print '(I2, A, $)', f1, ', '
        else
            print '(I3, A, $)', f1, ', '
        end if
        if (i==17) then
            exit
        end if
    end do
    
    print *, '...'

    end program Fibonacci

Hello, World!:

Example for versions Intel Visual Fortran 11.1, gfortran 4.5.0
program HelloWorld

print *, 'Hello, World!'

end program HelloWorld