1

Here is the source (lets call it test4.F):

     do 10 i=1
        write(*,*) i
10   continue
     end

With gfortran:

$gfortran test4.F 
$./a.out 
  -259911288

with ifort:

$ifort test4.F 
$./a.out 
       0

I know the syntax is incorrect. So both should throw compile-time errors.

1
  • yes, the indention was an error. fixed Commented Sep 9, 2016 at 17:53

2 Answers 2

5

Your test program is indeed not valid Fortran, but there isn't a syntax problem.

When the file has a .F suffix both gfortran and ifort assume that the program uses fixed form source. In fixed form source, blanks in statements aren't significant (outside character contexts). Your program is equivalent to:

      do10i=1
      write(*,*) i
 10   continue
      end

With comments:

      do10i=1         ! Set the real variable do10i to value 1
      write(*,*) i    ! Write the undefined integer variable i
 10   continue        ! Do nothing
      end             ! End execution

Because of implicit typing you have a defaul real variable do10i and so there isn't a syntax error for a do control, and there isn't a do control that sets i to 1.

Instead your program is invalid because you are referencing the value of i although it hasn't first been defined (because it isn't a loop variable). But this isn't an error the compiler has to complain about at compile time (or even run time). A compiler is allowed to print any value it likes. gfortran chose one, ifort another.

Liberal use of implicit none and avoiding fixed-form source are good ways to avoid many programming errors. In this case, use of an end do instead of continue would also alert the compiler to the fact that you intended there to be some looping.

Sign up to request clarification or add additional context in comments.

1 Comment

I think your answer works out. Should have thought of this. I will wait for some time before accepting.
1

Which version of gfortran are you using? With gfortran-6, the following

      PROGRAM MAIN

      USE ISO_FORTRAN_ENV, ONLY:
     1 COMPILER_VERSION,
     2 COMPILER_OPTIONS

C EXPLICIT TYPING ONLY
      IMPLICIT NONE

C VARIABLE DECLARATIONS
      INTEGER I

      DO 10 I = 1
      WRITE (*,*) I
 10   CONTINUE

      PRINT '(/4A/)',
     1 'THIS FILE WAS COMPILED USING ', COMPILER_VERSION(),
     2 ' USING THE OPTIONS ', COMPILER_OPTIONS()

      END PROGRAM MAIN

indeed throws the error

main.F:13:13:

       DO 10 I = 1
         1
Error: Symbol ‘do10i’ at (1) has no IMPLICIT type

I suspect that test4.F does not include the implicit none statement. Consequently, DO 10 I = 1 is interpreted as an implicitly defined real variable DO10I = 1. Here is a sample of a fixed form statement showing what are now (post Fortran 90) considered significant blanks followed by an equivalent statement without the blanks

DO I=1 , M AX ITER S

DO I = 1 , MAXITERS

Upshot, always use explicit variable declarations in all your programs. Better yet, only write Fortran in free source form main.f90. Free source form is more compatible with modern interactive input devices than fixed form. The maximum line length is 132 characters, compared to the older limit of 72 characters. This reduces the possibility of text exceeding the limit, which could lead the compiler to misinterpret names. Here's the same code in free source form

program main

  use  ISO_Fortran_env, only: &
       stdout => OUTPUT_UNIT, &
       compiler_version, &
       compiler_options

  ! Explicit typing only
  implicit none

  ! Variable declarations
  integer :: i

  do i = 1, 3
     write (stdout, *) i
  end do

  print '(/4a/)', &
       ' This file was compiled using ', compiler_version(), &
       ' using the options ', compiler_options()

end program main

and

gfortran-6 -std=f2008ts -o main.exe  main.f90
./main.exe

yields 1 2 3

 This file was compiled using GCC version 6.1.1 20160802 using the options -mtune=generic -march=x86-64 -std=f2008ts

5 Comments

Indeed, the reason all languages newer than Fortran have reserved keywords and no implicit variable declarations is that the software to launch a satellite had a line, DO 31 = 3.1. This was a typo for DO 31 = 3,1, but the compiler “correctly” parsed it as declaring an implicit variable named DO31. The rocket blew up, and the postmortem determined that the design of Fortran was partly to blame.
@Lorehead Arguments leveled against Fortran's implicit typing are generally unjust; few admonish JavaScript or Python for this. Computing was in its infancy when FORTRAN 66 was standardized. Initially, its purpose was to perform calculations, and not to implement complex data structures. Fancy calculators have no need to declare anything ahead of time, because variables didn't require types. That is, values have types, and variables are just nametags attached to values, therefore there's nothing to declare about the type of a variable in the absence of a value.
@jlokimlin The issue here, though, is that it’s very easy for a simple mistake to create a bug, and very hard to catch that bug, because what was meant as a control statement turns into an accidentally-valid implicit variable assignment.
@Lorehead I completely agree with you. Every well written Fortran project must include universal instances of the implicit none statement.
There were well written projects before implicit none.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.