The following code is valid and compiles successfully giving the appropriate output (i.e. prints 2).
#define y 2
program main
implicit none
integer :: x
x = y
print *, x
end program main
However, if moving the macro right before the line were the variable x is assigned it value
program main
implicit none
integer :: x
#define y 2
x = y
print *, x
end program main
it does not compile because the pre-processor does not perform the expansion, as confirmed by compiling with gfortran using the flag -E.
However, if I were to write an equivalent program in C
#include <stdio.h>
int main() {
int y;
#define x 2
y = x;
printf("%d",y);
return 0;
}
the macro would be expanded as expected.
I know that the pre-processor is evoked with the traditional mode by default in GCC when compiling Fortran, but quickly skimming through cpp traditional mode documentation doesn't mention anything about macro expansion in the middle of the body of the program.
What could be the reason that cause the pre-processor not to expand a macro in the middle of the program? Is it just for define statements or is it for everything else?