fortrangfortran

What are TRUE and FALSE constants (without the surrounding periods ('.')) in Fortran?


Consider the below program

program
  print*,.true.,.false.
  print*,true,false
end program

This program prints different values in pgfortan and gfortran.

pgfortran output

T F
0.00000000       0.00000000

gfortran output

T F
4.59135442E-41   5.87982594E-39

Question - The logical constants .true. and .false. are displayed properly as T and F. But what are these constants true and false, where there are no . around the constants?


Solution

  • As suggested by albert, TRUE and FALSE have no intrinsic meaning in Fortran - they are just ordinary identifiers that must be declared and assigned a value. Sometimes an application USEs a module with a bunch of vendor-supplied declarations and these might include declarations of TRUE and FALSE as named constants, especially on the Windows platform.

    In your example, TRUE and FALSE are implicitly declared, uninitialized variables. Since they are uninitialized, the value is undefined. Some implementations might give uninitialized variables a zero value, but most do not. It's better to not default values to zero, so that you are aware of programming errors earlier.

    And while we're on the topic of LOGICAL values, I'll point you to an old post of mine on the subject.