Re: Fortran Preprocessor?



In article <en8prj$10t$1@xxxxxxxxxxxxxxxxxxxx>,
Timothy Patrick Gallagher <gtg085x@xxxxxxxxxxxxxxxx> wrote:

I'm relatively new to Fortran but I've used C/C++ quite a bit...
The thing I miss the most is the ability to use #define to set
constants (I don't like using static/parameter memory variables).

Is there any analogue to #define in standard Fortran? Does gfortran
use the gcc preprocessor (allowing #define statements)?

Most fortran compilers do use the C preprocessor. It might be
invoked with a compiler-specific command line parameter, or it might
be invoked automatically through the file extension (e.g. uppercase
..F or .F90). It is not required by any standard, and not all
compilers do so, but in my experience most of them do.

However, there are several good reasons for using fortran parameters
rather than preprocessor macros. One of them is that fortran
parameters have a defined data type and kind, whereas inline
constants (through macro substitution) do not. The difference
arises in mixed mode and mixed precision expressions and when used
as actual arguments to subprograms. For example,

#define PI=3.14159265358979323846264338328d0
real(wp), parameter :: pix=PI
real(wp), parameter :: piy=3.14159265358979323846264338328_wp

call sub(PI)
call sub(pix)
call sub(piy)

The values of PI, pix, and piy may all be different, and the three
calls may result in very different results, or some may be illegal
when the others aren't, depending on how the kind "wp" has been
defined, or you may get compiler errors with some combinations of
compiler options but not others. These kinds of bugs in programs
can be difficult to track down. Of course, you might do something
like

#define PI=3.14159265358979323846264338328_wp

to take advantage of the fortran semantics, but by then you might as
well just use the full fortran parameter syntax.

$.02 -Ron Shepard
.