Some type of automatic initialization of variables?

No other way to say this but relying on the compiler/OS to initialize anything by default (unless you are using a compiler option to do it) is shoddy programming.The results can be compiler dependent.

Try this program on your systems.

Program realz
  
  USE ISO_FORTRAN_ENV

  Implicit NONE

  Real(REAL32) :: z32
  Real(REAL64) :: z64

  Print *,''
  Print *,' uninitialized'

  Print *,' z32 = ? - ', z32
  Print *,' z64 = ? - ', z64

  z32 = -0.000000
  z64 = -0.000000

  Print *,''
  Print *,' z32 = -0.000000 - ', z32
  Print *,' z64 = -0.000000 - ', z64

  z32 = -0.000000_REAL32
  z64 = -0.000000_REAL64

  Print *,''
  Print *,' z32 = -0.000000_REAL32 - ', z32
  Print *,' z64 = -0.000000_REAL64 - ', z64

  z32 = REAL(-0,REAL32)
  z64 = REAL(-0,REAL64)

  Print *,''
  Print *,' z32 = REAL(-0,REAL32) - ', z32
  Print *,' z64 = REAL(-0,REAL64) - ', z64
  Print *,''
  Print *,''

  STOP

End Program realz

I get the following results for ifort, gfortran-11, and nvfortran on a Linux Mint (Ubuntu 18.04 LTS) 64 bit system. Only ifort appears to initialize to 0.0 by default but that just might be an optimization thing so don’t assume ifort does this for all unitialized values.

ifort OneAPI 2021.6.0

  uninitialized
  z32 = ? -   0.0000000E+00
  z64 = ? -   0.000000000000000E+000
 
  z32 = -0.000000 -   0.0000000E+00
  z64 = -0.000000 -   0.000000000000000E+000
 
  z32 = -0.000000_REAL32 -   0.0000000E+00
  z64 = -0.000000_REAL64 -   0.000000000000000E+000
 
  z32 = REAL(-0,REAL32) -   0.0000000E+00
  z64 = REAL(-0,REAL64) -   0.000000000000000E+000

gfortran-11

  uninitialized
  z32 = ? -    6.41672678E+27
  z64 = ? -    0.0000000000000000     
 
  z32 = -0.000000 -   -0.00000000    
  z64 = -0.000000 -   -0.0000000000000000     
 
  z32 = -0.000000_REAL32 -   -0.00000000    
  z64 = -0.000000_REAL64 -   -0.0000000000000000     
 
  z32 = REAL(-0,REAL32) -    0.00000000    
  z64 = REAL(-0,REAL64) -    0.000000000000000

nvfortran 22-2-0

  uninitialized
  z32 = ? -    4.5916347E-41
  z64 = ? -    1.1520304496612931E-310
 
  z32 = -0.000000 -    -0.000000    
  z64 = -0.000000 -    -0.000000000000000     
 
  z32 = -0.000000_REAL32 -    -0.000000    
  z64 = -0.000000_REAL64 -    -0.000000000000000     
 
  z32 = REAL(-0,REAL32) -     0.000000    
  z64 = REAL(-0,REAL64) -     0.000000000000000

1 Like