Search code examples
fortran

Is Fortran's parameter statement similar to C/C++'s #define statement?


I am new to Fortran, but it looks to me that Fortran's parameter is similar to using #define in C/C++. Are they similar? Are they used in the same ways? I know #define is a preprocessor statement and I wasn't sure what similarities or differences this may cause #define to have with parameter.


Solution

  • #define is a pre-processor statement to define a macro. Macros are replaced by their value during a pre-processing step. Please note that pre-processor statements are not part of the Fortran standard. Not every compiler supports them.

    A parameter, on the other hand, denotes a named constant (similar to the C construct const). Any variable type may be used to declare a constant. This constant is defined at compile-time:

    integer, parameter :: i = 1
    real, parameter    :: a = 1.
    complex,parameter  :: z = (1., 1.)