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

#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

Relevant Pages

  • Re: The "F" language.
    ... "implicit none" almost as soon as I began using Fortran, ... when C compilers were memory limited and so didn't diagnose as ... write a preprocessor that will convert the existing code. ...
  • Re: Posix fortran and the gnu toolchain
    ... So I tried to go in and verify what Richard says, and I'm a little disappointed that I couldn't get the text + x numbers to show. ... know anything about Fortran. ... worry about the possibility of such conflicts; ... Be aware that the details can and do vary among compilers. ...
  • Re: Preprocessed fortran
    ... The early C compilers did it with a separate program, ... Only discovered a few months ago that Fortran used the C preprocessor ...
  • Re: Is it time to legitimise REAL*8 etc?
    ... Some vendors phased out their F77 compilers over 10 ... No need to support a seperate F77 compiler as, ... experience with F77) that the F95 programming is safer than the F77 ... Authors are not chosing more recent Fortran ...
  • Re: what is the most suitable Linux platform for Programmers and software developers
    ... C, C++, Perl, Python, Lisp, and a bunch of other languages are supported ... There are FORTRAN compilers, but FORTRAN ... Look at the Linux ...