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
.



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. ...
    (comp.lang.fortran)
  • 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. ...
    (comp.lang.fortran)
  • 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 ...
    (comp.lang.fortran)
  • 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 ...
    (comp.lang.fortran)
  • Re: code speed moving from fortran 77 compiler to f2003 compiler
    ... familiar with the language, whether certain language features (or their ... sufficiently usable compilers, of free compilers, of already bought ... I think one can safely use Fortran 95 and even many of the Fortran 2003 ... and readability due to the conversion.) ...
    (comp.lang.fortran)