Question about block

program test
  integer :: n
  real :: res
  real, allocatable :: x(:)
  n = 1000
  allocate(x(n), source = 1.0)
  res = 0.0
  block 
    integer :: i
    do i = 1, n
      res = res + x(i)
    end do
  end block
  print *, i
  print *, res
end program

Why is this code not producing any error/warning message in GFortran? I thought variables defined within a block are treated as undefined outside of the block.

The “i” that is outside the block is a separate variable, implicitly declared, and with an undefined value in that print statement. If you add “implicit none”, then gfortran correctly reports the following error:

block.f90:14:12:
   14 |   print *, i
      |            1
Error: Symbol 'i' at (1) has no IMPLICIT type

Also, you probably misdeclared res to be of type integer. You probably want it to be real.

4 Likes