2
votes

I have some legacy Fortran code which I was asked to analyze and translate to a modern language. I don't know which compiler was used in the past to compile the code, so for now, I'm trying to compile it with gfortran. The code contains a statement like this was causes gfortran to complain:

  program test
  implicit none
  integer*4 :: var
  var=.true.

  if(var) then
        write(*,*) "Hi"
  endif

  end program test

Compiling this with gfortran gives the following error:

test.f:6:9:

       if(var) then
         1
Error: IF clause at (1) requires a scalar LOGICAL expression

(In addition, it gives a warning about the conversion done in var=.true.).

I'm not sure which which compiler the code was compiled, but apparently the code should compile as it is. Is there a way to tell gfortran to accept this conversion?

According to the docs, no implicit conversion is done within if-statements though: https://gcc.gnu.org/onlinedocs/gfortran/Implicitly-convert-LOGICAL-and-INTEGER-values.html

1
I can't answer the question (as I don't know how to get gfortran to compile this broken code), but can you just make var logical? - francescalus
Yes that would work, but I would prefer not to change the code.. - Raphael Roth
A perfectly reasonable desire. A warning, though: if there are these non-standard parts in the code then there's no telling what else is going on (which may surprise you) that your compiler isn't complaining about. It may be worth looking to see which compiler was originally used. - francescalus
If you don't want to change your code to Fortran, how can you insist on a Fortran compiler to compile it? - Vladimir F
Intel will compile this. Gfortran won't you have found the manual entry about it yourself so you see yourself it is not possible in gfortran. Should we copy the manual to an answer for you? Be aware there are other issues too. .true. in gfortran is +1, but in Intel (DEC,Compaq), it is -1. - Vladimir F

1 Answers

1
votes

This is not possible in GFortran. The manual states:

However, there is no implicit conversion of INTEGER values in if-statements, nor of LOGICAL or INTEGER values in I/O operations.

You are only able to perform implicit conversions in assignments like your

  integer :: var
  var = .true.

but even there you must be very careful. It is not standard conforming and the value var will differ between compilers. Intel used to use -1 (all bits set to 1), unless -standard-semantics was chosen, for .true., but gfortran uses +1 as in the C language. New versions of Intel Fortran changes the default. The other direction is even trickier, there might be values which are neither .true. nor .false..