From: Timothy Patrick Gallagher on
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)?

--
Tim Gallagher
Email: gtg085x(a)mail.gatech.edu
From: Beliavsky on

Timothy Patrick Gallagher 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).

Why? For about 30 years, using PARAMETERs has been the idiomatic way to
define constants in Fortran. I think only a small percentage of
competent Fortran programmers routinely use a preprocessor to define
constants. In C++, I think declaring variables "const" is preferred to
using #define.

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

I do not use the C preprocessor, but I believe that if you give your
source files a .F (fixed source form) or .F90 (free source form)
suffix, the C preprocessor will be invoked automatically.

This was discussed in a gfortran mailing list message
http://gcc.gnu.org/ml/fortran/2006-06/msg00333.html . The gfortran
mailing list is the fastest way to reach gfortran developers.


>
> --
> Tim Gallagher
> Email: gtg085x(a)mail.gatech.edu

From: Tim Prince on
Timothy Patrick Gallagher 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)?
>
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
From: Steve Lionel on
Timothy Patrick Gallagher 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)?

There is a preprocessor defined in standard Fortran, but it is optional
and not widely implemented. It is also not compatible with the cpp
preprocessor syntax that most everyone uses.

Many Fortran compilers DO support cpp-style preprocessing, though the
mechanism to invoke it varies. Typically there is a switch you must
add or you name the file with a .F or .F90 file type (capital F) on
Linux. Read your compiler's documentation for details.

However, your newness to Fortran seems to have confused you on one
item. PARAMETER constants are not "memory variables". They are "named
constants" and are exactly what you want to use for this purpose. I
don't recommend using #define and the like unless you are writing
conditional code that you want the preprocessor to look at (with
#ifdef, etc.)

Steve

From: Tim Prince on
Timothy Patrick Gallagher 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).
>

I don't know whether you refer to C or to Fortran there. The analogy
between Fortran and C is not very close; I don't know of anyone who
prefers pre-processor macros to Fortran PARAMETER constants, even among
those who use tradcpp heavily.