Chapter 11
C-Fortran Interface
This chapter treats issues regarding Fortran and C interoperability.
The discussion is inherently limited to the specifics of the Sun FORTRAN 77, Fortran 95, and C compilers.
Note – Material common to both FORTRAN 77 and Fortran 95 is presented in examples that use FORTRAN 77.
Compatibility Issues
Most C-Fortran interfaces must agree in all of these aspects:
-
- Function/subroutine: definition and call
- Data types: compatibility of types
- Arguments: passing by reference or value
- Arguments: order
- Procedure name: uppercase and lowercase and trailing underscore (_)
- Libraries: telling the linker to use Fortran libraries
Some C-Fortran interfaces must also agree on:
-
- Arrays: indexing and order
- File descriptors and
stdio
- File permissions
Function or Subroutine?
The word function has different meanings in C and Fortran. Depending on the situation, the choice is important:
-
- In C, all subprograms are functions; however, some may return a null (void) value.
- In Fortran, a function passes a return value, but a subroutine does not.
When a Fortran routine calls a C function:
-
- If the called C function returns a value, call it from Fortran as a function.
- If the called C function does not return a value, call it as a subroutine.
When a C function calls a Fortran subprogram:
-
- If the called Fortran subprogram is a function, call it from C as a function that returns a compatible data type.
- If the called Fortran subprogram is a subroutine, call it from C as a function that returns a value of
int
(compatible to FortranINTEGER*4
) orvoid
. A value is returned if the Fortran subroutine uses alternate returns, in which case it is the value of the expression on theRETURN
statement. If no expression appears on theRETURN
statement, and alternate returns are declared onSUBROUTINE
statement, a zero is returned.
Data Type Compatibility
The tables below summarize the data sizes and default alignments for FORTRAN 77 and Fortran 95 data types. In both tables, note the following:
-
- C data types
int
,long
int
, andlong
are equivalent (4 bytes). In a 64-bit environment and compiling with-xarch=v9
orv9a
,long
and pointers are 8 bytes. This is referred to as "LP64".
- C data types
REAL*16
andCOMPLEX*32
, (REAL(KIND=16)
andCOMPLEX(KIND=16)
), are available only on SPARC platforms. In a 64-bit environment and compiling with-xarch=v9
orv9a
, alignment is on 16-byte boundaries.- Alignments marked 4/8 for SPARC indicate that alignment is 8-bytes by default, but on 4-byte boundaries in COMMON blocks. The maximum alignment in COMMON is 4-bytes.
- The elements and fields of arrays and structures must be compatible.
- You cannot pass arrays, character strings, or structures by value.
- You can pass arguments by value from
f77
to C, but not from C tof77
, since%VAL()
is not allowed in a Fortran dummy argument list.
FORTRAN 77 and C Data Types
TABLE 11-1 shows the sizes and allowable alignments for FORTRAN 77 data types. It assumes no compilation options affecting alignment or promoting default data sizes are applied. (See also the FORTRAN 77 Language Reference Manual).
FORTRAN 77 Data Type | C Data Type | Size | Default Alignment SPARC x86 |
|
---|---|---|---|---|
BYTE X n X |
char x |
1 1 n |
1 1 1 |
1 1 1 |
COMPLEX X |
struct {float r,i;} x; |
8 8 16 16 32 |
4 4 4/8 4/8 4/8/16 |
4 4 4 4 -- |
DOUBLE PRECISION X |
double x |
8 4 4 8 16 |
4/8 4 4 4/8 4/8/16 |
4 4 4 4 -- |
INTEGER X |
int x |
4 2 4 8 |
4 2 4 4 |
4 2 4 4 |
LOGICAL X |
int x |
4 1 2 4 8 |
4 1 2 4 4 |
4 1 2 4 4 |
SPARC: Fortran 95 and C Data Types
The following table similarly compares the Fortran 95 data types with C.