From: Dan Nagle on
Hello,

The usual answers are to use cpp or its half-tamed relation fpp;
or to use m4 or other preprocessor; or to use f90ppr; or to use coco.
Note the cautions Tim mentioned below.

http://www.ifremer.fr//ditigo/molagnon/fortran90/contenu.html for f90ppr

http://users.erols.com/dnagle/coco.html#download for coco

Google for your favorite other preprocessor. IIRC, Sun used to have
one, I don't know if they still do. Lahey used to have an fpp,
again, I don't know if they still do. Try Walt's www.fortran.com
for more leads.

HTH

Tim Prince wrote:

<snip requoted>

> gfortran and g77 use tradcpp (K&R C pre-processor). It is fairly common
> practice to set up a Makefile so as to use tradcpp separately from
> Fortran (invoked e.g. as 'gcc -x c -traditional -E -P somefile.F') so as
> to avoid the minor differences in pre-processing support included with
> most Fortran compilers. The #1 reason for K&R style rather than C89
> pre-processing is to avoid the syntax conflict of the // operator.
> Read the documentation of your Fortran compiler to see which
> pre-processing options are supported by it.
> There is also a standard, but less often used, Fortran pre-processor
> facility, called coco, which is not generally included with Fortran
> compilers: http://www.fortran.com/stds_docs.html


--
Cheers!

Dan Nagle
Purple Sage Computing Solutions, Inc.
From: Richard Maine on
Timothy Patrick Gallagher <gtg085x(a)acmex.gatech.edu> 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)?

Well, yes there is something roughly comparable. It is a parameter. I'm
not sure I understand why you don't "like" it, but perhaps that's
because you don't understand what it is; your choice of terminology
suggests such misunderstanding. A parameter is not a variable; nor is it
in memory; and the concept of static does not apply, since it isn't in
memory. A parameter is a compile-time constant, which is exactly what
you asked for. The compiler might end up putting the value in memory,
depending on its usage, but that's also true of the #define in C.

No, there aren't two different ways to spell the same thing... well
other than trivial things like having a separate parameter statement
versus putting the parameter attribute in a type declaration statement.

There are differences between C macros (that's what the #define things
are), and Fortran parameters, but the difference is that C macros are
not constants - they are text substitutions. Also, they can have
arguments. So if you wanted macros that had arguments or were used for
things other than constants, I could understand that parameters don't do
that. But you did say you wanted it for constants. A Fortran parameter
is exactly that, while that's actually a bit of a misapplication of C
macros (though a common one).

If you do want to use the C proprocessor or something simillar but safer
like fpp (beware of using cpp literally with Fortran code; it can work,
but there are gotchas, it can be done with any compiler by separately
running cpp/fpp, possibly assisted by a Makefile or script. Some
compilers have features to automatically invoke cpp/fpp for you, but you
can always do it separately even if the compiler provides nothing
special.

--
Richard Maine | Good judgement comes from experience;
email: last name at domain . net | experience comes from bad judgement.
domain: summertriangle | -- Mark Twain
From: Ron Shepard on
In article <en8prj$10t$1(a)news-int2.gatech.edu>,
Timothy Patrick Gallagher <gtg085x(a)acmex.gatech.edu> 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
From: Timothy Patrick Gallagher on
I think it is just my misunderstanding of the parameter atribute.
I was under the impression that it created a memory space that stored
the value but the compiler didn't let anything change that memory space.

For the purpose of constants, using parameter makes sense if the actual
implementation is the same as a #define. I didn't use #define for too
many other things in C, except to prevent duplicate header includes
(#ifdef _HEADER_H_ ... etc) or to define Win32 API messages. I don't
really need to do either of those in Fortran at this point though.

--
Tim Gallagher
Email: gtg085x(a)mail.gatech.edu
From: Dan Nagle on
Hello,

Timothy Patrick Gallagher wrote:
> I think it is just my misunderstanding of the parameter atribute.
> I was under the impression that it created a memory space that stored
> the value but the compiler didn't let anything change that memory space.

That's certainly one implementation, but not the only one.

> For the purpose of constants, using parameter makes sense if the actual
> implementation is the same as a #define. I didn't use #define for too
> many other things in C, except to prevent duplicate header includes
> (#ifdef _HEADER_H_ ... etc) or to define Win32 API messages. I don't
> really need to do either of those in Fortran at this point though.

Well, #ifndef _HEADER_H_ perhaps?

The Fortran standard tells you what you can do with a named constant,
but not how they're implemented.

An integer one, might appear only in the instruction stream,
as an increment instruction where a general add might be expected.

A constant array might actually be stored somewhere, or not,
depending upon how it's used (as a whole matrix argument to matmul(),
or as individual elements only).

A standard-conforming program cannot tell the difference.
Your debugger might.

--
Cheers!

Dan Nagle
Purple Sage Computing Solutions, Inc.