This FORTRAN code should not compile. Is there any reason why this is happening?

The following code compiles, but I don't think it should. As you can see, the result is garbage.

This is a minimal bad example of something that makes it a little harder to work on a large project I'm working on.

My question is, why doesn't the compiler complain? Is this a compiler limitation, or is this some kind of "expected behavior" and am I missing something?

I am using gfortran 4.6.3.

module dataModule
    integer :: datum1 = int(1)
    integer :: datum2 = int(2)    
end module dataModule

program moduleTest
    use dataModule, only: datum1

    write(*,*) "datum 1 is", datum1
    write(*,*) "datum 2 is", datum2

end program moduleTest

      

Output example:

datum 1 is           1
datum 2 is  4.58322689E-41

      

+3


source to share


1 answer


Your code is corrupted, not the compiler. If datum2

were related to usage despite the suggestion only

, and if explicit initialization datum2

was ignored, then yes, it would be a naughty compiler.

The answer is much more mundane.



datum2

not used: if absent, implicit none

it is an implicitly typed variable in the main program. Garbage is due to the fact that it is not defined, by initialization or assignment, before its value is specified, and that it is implicitly (by default) valid. The compiler is not required to detect this error at compile time (or startup).

+3


source







All Articles