Sun Studio 12: Fortran Programming Guide

11.1 Compatibility Issues

Most C-Fortran interfaces must agree in all of these aspects:

Some C-Fortran interfaces must also agree on:

11.1.1 Function or Subroutine?

The word function has different meanings in C and Fortran. Depending on the situation, the choice is important:

When a Fortran routine calls a C function:

When a C function calls a Fortran subprogram:

11.1.2 Data Type Compatibility

Table 11–2 summarizes the data sizes and default alignments for Fortran 95 data types compared with C. It assumes no compilation options affecting alignment or promoting default data sizes are applied. Note the following:

Table 11–1 Data Sizes and Alignment—(in Bytes) Pass by Reference ( f95 and cc)

Fortran 95 Data Type 

C Data Type 

Size 

Alignment 

BYTE x

char x

CHARACTER x

unsigned char x ;

 

CHARACTER (LEN=n) x

unsigned char x[n] ;

n

COMPLEX x

struct {float r,i;} x;

 

COMPLEX (KIND=4) x

COMPLEX (KIND=8) x

COMPLEX (KIND=16) x (SPARC)

struct {float r,i;} x;

struct {double dr,di;} x;

struct {long double, dr,di;} x;

16 

32 

4/8 

4/8/16 

DOUBLE COMPLEX x

struct {double dr, di;} x;

16 

4/8 

DOUBLE PRECISION x

double x ;

REAL x

float x ;

 

REAL (KIND=4) x

REAL (KIND=8) x

REAL (KIND=16) x (SPARC)

float x ;

double x ;

long double x ;

16 

4/8 

4/8/16 

INTEGER x

int x ;

 

INTEGER (KIND=1) x

INTEGER (KIND=2) x

INTEGER (KIND=4) x

INTEGER (KIND=8) x

signed char x ;

short x ;

int x ;

long long int x;

LOGICAL x

int x ;

 

LOGICAL (KIND=1) x

LOGICAL (KIND=2) x

LOGICAL (KIND=4) x

LOGICAL (KIND=8) x

signed char x ;

short x ;

int x ;

long long int x;

11.1.3 Case Sensitivity

C and Fortran take opposite perspectives on case sensitivity:

The f95 default is to ignore case by converting subprogram names to lowercase. It converts all uppercase letters to lowercase letters, except within character-string constants.

There are two usual solutions to the uppercase/lowercase problem:

Use one of these two solutions, but not both.

Most examples in this chapter use all lowercase letters for the name in the C function, and do not use the f95 -U compiler option.

11.1.4 Underscores in Routine Names

The Fortran compiler normally appends an underscore (_) to the names of subprograms appearing both at entry point definition and in calls. This convention differs from C procedures or external variables with the same user-assigned name. Almost all Fortran library procedure names have double leading underscores to reduce clashes with user-assigned subroutine names.

There are three usual solutions to the underscore problem:

Use only one of these solutions.

The examples in this chapter could use the BIND(C) attribute declaration to avoid underscores. BIND(C) declares the C external functions that can be called from Fortran, and the Fortran routines that can be called from C as arguments. The Fortran compiler does not append an underscore as it ordinarily does with external names. The BIND(C) must appear in each subprogram that contains such a reference. The conventional usage is:


       FUNCTION ABC
        EXTERNAL XYZ
        BIND(C) ABC, XYZ

Here the user has specified not only that XYZ is an external C function, but that the Fortran caller, ABC, should be callable from a C function. If you use BIND(C), the C function does not need an underscore appended to the function name.

11.1.5 Argument-Passing by Reference or Value

In general, Fortran routines pass arguments by reference. In a call, if you enclose an argument with the nonstandard function %VAL(), the calling routine passes it by value.

The standard Fortran 95 way to pass arguments by value is the VALUE attribute and through INTERFACE blocks. See 11.4 Passing Data Arguments by Value.

In general, C passes arguments by value. If you precede an argument by the ampersand operator (&), C passes the argument by reference using a pointer. C always passes arrays and character strings by reference.

11.1.6 Argument Order

Except for arguments that are character strings, Fortran and C pass arguments in the same order. However, for every argument of character type, the Fortran routine passes an additional argument giving the length of the string. These are long int quantities in C, passed by value.

The order of arguments is:

Example:

This Fortran code fragment:  

Is equivalent to this in C:  


CHARACTER*7 S
INTEGER B(3)
...
 CALL SAM( S, B(2) )

char s[7];
int b[3];
...
sam_( s, &b[1], 7L ) ; 

11.1.7 Array Indexing and Order

Array indexing and order differ between Fortran and C.

11.1.7.1 Array Indexing

C arrays always start at zero, but by default Fortran arrays start at 1. There are two usual ways of approaching indexing.


      INTEGER B(0:2)

This way, the Fortran element B(1) is equivalent to the C element b[1].

11.1.7.2 Array Order

Fortran arrays are stored in column-major order: A(3,2)


A(1,1)  A(2,1)  A(3,1)  A(1,2)  A(2,2)  A(3,2)

C arrays are stored in row-major order: A[3][2]


A[0][0] A[0][1] A[1][0] A[1][1] A[2][0] A[2][1]

This does not present a problem for one-dimensional arrays. However, with multi-dimensional arrays, be aware of how subscripts appear and are used in all references and declarations—some adjustments might be necessary.

For example, it may be confusing to do part of a matrix manipulation in C and the rest in Fortran. It might be preferable to pass an entire array to a routine in the other language and perform all the matrix manipulation in that routine to avoid doing part in C and part in Fortran.

11.1.8 File Descriptors and stdio

Fortran I/O channels are in terms of unit numbers. The underlying SunOS operating system does not deal with unit numbers but with file descriptors. The Fortran runtime system translates from one to the other, so most Fortran programs do not have to recognize file descriptors.

Many C programs use a set of subroutines, called standard I/O (or stdio). Many functions of Fortran I/O use standard I/O, which in turn uses operating system I/O calls. Some of the characteristics of these I/O systems are listed in the following table.

Table 11–2 Comparing I/O Between Fortran and C

 

Fortran Units  

Standard I/O File Pointers  

File Descriptors  

Files Open  

Opened for reading and writing 

Opened for reading, or for writing, or for both; or opened for appending; See open(2)

Opened for reading, or for writing, or opened for both 

Attributes  

Formatted or unformatted 

Always unformatted, but can be read or written with format-interpreting routines 

Always unformatted 

Access  

Direct or sequential 

Direct access if the physical file representation is direct access, but can always be read sequentially 

Direct access if the physical file representation is direct access, but can always be read sequentially 

Structure  

Record 

Byte stream 

Byte stream 

Form  

Arbitrary nonnegative integers from 0-2147483647 

Pointers to structures in the user’s address space 

Integers from 0-1023 

11.1.9 Libraries and Linking With the f95 Command

To link the proper Fortran and C libraries, use the f95 command to invoke the linker.

Example 1: Use the compiler to do the linking:


demo% cc -c someCroutine.c
demo% f95 theF95routine.f someCroutine.o  <- The linking step
demo% a.out
 4.0 4.5
 8.0 9.0
demo%