inthaveThe(unsignedchar*p,unsignedcharnum)2],&quot 转义;wb &quot 转义;);

C H A P T E R
11 - C-Fortran Interface
C H A P T E R
C-Fortran Interface
This chapter treats issues regarding Fortran and C interoperability and applies only to the specifics of the Sun Studio Fortran 95, and C compilers.
discusses briefly the C binding features proposed in Section 15 of the Fortran 2000 standard. (The standard is available at the international Fortran standards web site, ). The Fortran 95 compiler implements these features, as described in the standard.
Most C-Fortran interfaces must agree in all of these aspects:
Function and subroutine definitions and calls
Data type compatibility
Argument passing, either by reference or by value
Order of arguments
Procedure name, either uppercase, lowercase, or with a trailing underscore (_)
Passing the right library references to the linker
Some C-Fortran interfaces must also agree on:
Array indexing and order
File descriptors and stdio
File permissions
The word function has different meanings in C and Fortran. Depending on the situation, the choice is important:
In C, all subpr however, void functions do not return a value.
In Fortran, a function passes a return value, but a subroutine generally 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 Fortran INTEGER*4) or void otherwise. A value is returned if the Fortran subroutine uses alternate returns, in which case it is the value of the expression on the RETURN statement. If no expression appears on the RETURN statement, and alternate returns are declared on the SUBROUTINE statement, a zero is returned.
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:
C data types int, long int, and long are equivalent (4 bytes) in a 32-bit environment. However, in a 64-bit SPARC environment and when compiling with any of the -xarch=v9 options, long and pointers are 8 bytes. This is referred to as the LP64 data model.
REAL*16 and COMPLEX*32,
in a 64-bit SPARC environment and when compiling with any -xarch=v9 option, are aligned on 16-byte boundaries.
Alignments marked 4/8 indicate that alignment is 8-bytes by default, but on 4-byte boundaries in COMMON blocks. The maximum default alignment in COMMON is 4-bytes. 4/8/16 indicates alignments on 16-byte boundaries when compiling with any -xarch=v9 option.
REAL(KIND=16),
REAL*16, COMPLEX(KIND=16), COMPLEX*32, are only available on SPARC platforms.
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 a Fortran 95 routine to a C routine by using %VAL(arg) at the call site. You can pass arguments by value from C to Fortran 95 provided the Fortran routine has an explicit interface block that declares the dummy argument with the VALUE attribute.
Components of numeric sequence types are aligned the same way as common blocks, and are also affected by the -aligncommon option.A numeric sequence type is a sequence type where all the components are of type default integer, default real, double-precision real, default complex, or default logical, and are not pointers.
Components of data types that are not numeric sequence types are aligned on natural alignments in most cases, except QUAD variables. For quad-precision variables, the alignment is different between SPARC V8 and V9 platforms.
Components of VAX structures and data types defined with the BIND(C) attribute always have the same alignment as C structures on all platforms.
TABLE 11-1
Data Sizes and Alignment--(in Bytes) Pass by Reference (
CHARACTER x
CHARACTER (LEN=n) x
unsigned char x[n] ;
struct {float r,i;}
COMPLEX (KIND=4) x
COMPLEX (KIND=8) x
COMPLEX (KIND=16) x
struct {float r,i;}
struct {double dr,}
struct {long double, dr,}
DOUBLE COMPLEX x
struct {double dr,}
DOUBLE PRECISION x
REAL (KIND=4) x
REAL (KIND=8) x
REAL (KIND=16) x
INTEGER (KIND=1) x
INTEGER (KIND=2) x
INTEGER (KIND=4) x
INTEGER (KIND=8) x
LOGICAL (KIND=1) x
LOGICAL (KIND=2) x
LOGICAL (KIND=4) x
LOGICAL (KIND=8) x
C and Fortran take opposite perspectives on case sensitivity:
C is case sensitive--case matters.
Fortran ignores case by default.
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:
In the C subprogram, make the name of the C function all lowercase.
Compile the Fortran program with the -U option, which tells the compiler to preserve existing uppercase/lowercase distinctions on function/subprogram names.
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.
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:
In the C function, change the name of the function by appending an underscore to that name.
Use the BIND(C) attribute declaration to indicate that an external function is a C language function.
Use the f95 -ext_names option to compile references to external names without underscores.
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.
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 .
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.
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:
Address for each argument (datum or function)
A long int for each character argument (the whole list of string lengths comes after the whole list of other arguments)
CHARACTER*7 S
INTEGER B(3)
CALL SAM( S, B(2) )
char s[7];
sam_( s, &b[1], 7L ) ;
Array indexing and order differ between Fortran and C.
C arrays always start at zero, but by default Fortran arrays start at 1. There are two usual ways of approaching indexing.
You can use the Fortran default, as in the preceding example. Then the Fortran element B(2) is equivalent to the C element b[1].
You can specify that the Fortran array B starts at B(0) as follows:
INTEGER B(0:2)
This way, the Fortran element B(1) is equivalent to the C element b[1].
Fortran arrays are stored in column-major order: 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.
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
Opened for reading and writing
Opened for reading, or for writing, or
See open(2)
Opened for reading, or for writing, or opened for both
Formatted or unformatted
Always unformatted, but can be read or written with format-interpreting routines
Always unformatted
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
Byte stream
Byte stream
Arbitrary nonnegative integers from 0-
Pointers to structures in the user's address space
Integers from 0-1023
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
Main programs compiled by f95 call dummy initialization routine f90_init in the library at program start up. The routines in the library are dummies that do nothing. The calls the compilers generate pass pointers to the program's arguments and environment. These calls provide software hooks you can use to supply your own routines, in C, to initialize a program in any customized manner before the program starts up.
One possible use of these initialization routines to call setlocale for an internationalized Fortran program. Because setlocale does not work if libc is statically linked, only Fortran programs that are dynamically linked with libc should be internationalized.
The source code for the init routines in the library is
void f90_init(int *argc_ptr, char ***argv_ptr, Char ***envp_ptr) {}
f90_init is called by f95 main programs. The arguments are set to the address of argc, the address of argv, and the address of envp.
The standard method for passing data between Fortran routines and C procedures is by reference. To a C procedure, a Fortran subroutine or function call looks like a procedure call with all arguments represented by pointers. The only peculiarity is the way Fortran handles character strings and functions as arguments and as the return value from a CHARACTER*n function.
For simple data types (not COMPLEX or CHARACTER strings), define or pass each associated argument in the C routine as a pointer:
TABLE 11-3
Passing Simple Data Types
external CSim
call CSim(i,r)
----------------------------
void csim_(int *i, float *r)
int i=100;
extern void fsim_(int *i, float *r);
fsim_(&i, &r);
------------------------------
subroutine FSim(i,r)
Pass a Fortran COMPLEX data item as a pointer to a C struct of two float or two double data types:
TABLE 11-4
Passing COMPLEX Data Types
double complex z
external CCmplx
call CCmplx(w,z)
------------------------------
struct cpx {float r,};
struct dpx {double r,i;};
void ccmplx_(
struct cpx *w,
struct dpx *z)
w -& r = 32.;
w -& i = .007;
z -& r = 66.67;
z -& i = 94.1;
struct cpx {float r,};
struct cpx d1;
struct cpx *w = &d1;
struct dpx {double r,};
struct dpx d2;
struct dpx *z = &d2;
fcmplx_( w, z );
---------------
subroutine FCmplx( w, z )
double complex z
w = (32., .007)
z = (66.67, 94.1)
In 64-bit environments and compiling with -xarch=v9, COMPLEX values are returned in registers.
Passing strings between C and Fortran routines is not recommended because there is no standard interface. However, note the following:
All C strings are passed by reference.
Fortran calls pass an additional argument for every argument with character type in the argument list. The extra argument gives the length of the string and is equivalent to a C long int passed by value. (This is implementation dependent.) The extra string-length arguments appear after the explicit arguments in the call.
A Fortran call with a character string argument is shown in the next example with its C equivalent:
TABLE 11-5
Passing a CHARACTER String
CHARACTER*7 S
INTEGER B(3)
CALL CSTRNG( S, B(2) )
char s[7];
cstrng_( s, &b[1], 7L );
If the length of the string is not needed in the called routine, the extra arguments may be ignored. However, note that Fortran does not automatically terminate strings with the explicit null character that C expects. This must be added by the calling program.
The call for a character array looks identical to the call for a single character variable. The starting address of the array is passed, and the length that it uses is the length of a single element in the array.
Array subscripts in C start with 0.
TABLE 11-6
Passing a One-Dimensional Array
integer i, Sum
integer a(9)
external FixVec
call FixVec ( a, Sum )
------------------------------
void fixvec_ (
int v[9], int *sum )
for ( i = 0; i &= 8; i++ )
*sum = *sum + v[i];
extern void vecref_
( int[], int * );
int v[9] = ...
vecref_( v, &sum );
------------------------------
subroutine VecRef( v, total)
integer i, total, v(9)
do i = 1,9
total = total + v(i)
Rows and columns between C and Fortran are switched.
TABLE 11-7
Passing a Two-Dimensional Array
REAL Q(10,20)
Q(3,5) = 1.0
CALL FIXQ(Q)
------------------------------
void fixq_( float a[20][10] )
a[5][3] = a[5][3] + 1.;
extern void
qref_( int[][10], int *);
int m[20][10] = ...
qref_( m, &sum );
------------------------------
SUBROUTINE QREF(A,TOTAL)
INTEGER A(10,20), TOTAL
DO I = 1,10
DO J = 1,20
TOTAL = TOTAL + A(I,J)
C and Fortran 95 derived types can be passed to each other's routines as long as the corresponding elements are compatible.( f95 accepts legacy STRUCTURE statements.)
TABLE 11-8
Passing Legacy FORTRAN 77 STRUCTURE Records
STRUCTURE /POINT/
REAL X, Y, Z
END STRUCTURE
RECORD /POINT/ BASE
EXTERNAL FLIP
CALL FLIP( BASE )
------------------------------
struct point {
float x,y,z;
void flip_( struct point *v )
v -& x = v -&
v -& z = -2.*(v -& z);
struct point {
float x,y,z;
void fflip_ ( struct point *) ;
struct point *ptx = &d;
fflip_ (ptx);
------------------------------
SUBROUTINE FFLIP(P)
STRUCTURE /POINT/
REAL X,Y,Z
END STRUCTURE
RECORD /POINT/ P
P.Z = -2.*P.Z
Note that Fortran 77 (VAX) structures always have the same alignment as C structures on all platforms. However, the alignment changes between platforms.
TABLE 11-9
Passing Fortran 95 Derived Types
TYPE point
REAL :: x, y, z
END TYPE point
TYPE (point) base
EXTERNAL flip
CALL flip( base)
------------------------------
struct point {
float x,y,z;
void flip_( struct point *v )
v -& x = v -&
v -& z = -2.*(v -& z);
struct point {
float x,y,z;
extern void fflip_ (
struct point *) ;
struct point *ptx = &d;
fflip_ (ptx);
------------------------------
SUBROUTINE FFLIP( P )
TYPE POINT
REAL :: X, Y, Z
END TYPE POINT
TYPE (POINT) P
P%Z = -2.*P%Z
Note that the Fortran 95 standard requires the SEQUENCE statement in the definition of the derived type to insure that storage sequence order be preserved by the compiler.
The components of a numeric sequence type are aligned on word (4-byte) boundaries on all platforms by default. This matches the alignment of C structures on x86 platforms, but differs from the alignment of C structures on SPARC V8 and V9 platforms. Use the -aligncommon option to change the alignment of numeric sequence types to match C structures. Use -aligncommon=8 to match SPARC V8 C structures, -aligncommon=16 to match SPARC V9.
Derived types not explicitly declared with SEQUENCE have the same alignment as C structures on SPARC V8 and V9 platforms, but differ on x86 platforms. This alignment cannot be changed with a compiler option.
A FORTRAN 77 (Cray) pointer can be passed to a C routine as a pointer to a pointer because the Fortran routine passes arguments by reference.
TABLE 11-10
Passing a FORTRAN 77 (Cray) POINTER
POINTER (P2X, X)
EXTERNAL PASS
P2X = MALLOC(4)
CALL PASS(P2X)
------------------------------
void pass_(p)
float **p;
**p = 100.1;
extern void fpass_( float** );
float *p2x;
fpass_(&p2x) ;
------------------------------
SUBROUTINE FPASS (P2X)
POINTER (P2X, X)
C pointers are compatible with Fortran 95 scalar pointers, but not array pointers.
Fortran 95 routine:
SUBROUTINE PASS(P)
REAL, POINTER :: P
END SUBROUTINE
END INTERFACE
REAL, POINTER :: P2X
ALLOCATE (P2X)
CALL PASS(P2X)
PRINT*, P2X
C routine:
void pass_(p);
float **p;
**p = 100.1;
The major difference between Cray and Fortran 95 pointers is that the target of a Cray pointer is always named. In many contexts, declaring a Fortran 95 pointer automatically identifies its target. Also, an explicit INTERFACE block is required for the called C routine.
To pass a Fortran 95 pointer to an array or array section requires a specific INTERFACE block, as in this example:
Fortran 95 routine:
SUBROUTINE S(P)
integer P(*)
END SUBROUTINE S
END INTERFACE
integer, target:: A(0:9)
integer, pointer :: P(:)
P =& A(0:9:2) !! pointer selects every other element of A
C routine:
void s_(int p[])
/* change middle element */
p[2] = 444;
Note that since the C routine S is not a Fortran 95 routine, you cannot define it to be assumed shape (integer P(:)) in the interface block. If the C routine needs to know the actual size of the array it must be passed as an argument to the C routine.
Again, keep in mind that subscripting between C and Fortran differs in that C arrays start at subscript 0.
Fortran 95 programs should use the VALUE attribute in dummy arguments when being called from C, and supply an INTERFACE block for C routines that are called from Fortran 95.
TABLE 11-11
Passing Simple Data Elements Between C and Fortran 95
PROGRAM callc
INTEGER FUNCTION crtn(I)
BIND(C) crtn
INTEGER, VALUE, INTENT(IN) :: I
END FUNCTION crtn
END INTERFACE
MM = crtn(M)
WRITE (*,*) M, MM
END PROGRAM
---------------------------------
int crtn(int x)
printf(&%d input \n&, x);
y = x + 1;
printf(&%d returning \n&,y);
return(y);
---------------------------------
21 returning
#include &stdlib.h&
int main(int ac, char *av[])
to_fortran_(12);
--------------------------------
SUBROUTINE to_fortran(i)
INTEGER, VALUE :: i
PRINT *, i
--------------------------------
Note that if the C routine will be called with different data types as an actual argument, you should include a !$PRAGMA IGNORE_TKR I in the interface block to inhibit the compiler from requiring a match in type, kind, and rank between the actual and dummy argument.
With legacy Fortran 77, call by value was available only for simple data, and only by Fortran 77 routines calling C routines. There was no way for a C routine to call a Fortran 77 routine and pass arguments by value. Arrays, character strings, or structures are best passed by reference.
To pass a value to a C routine from a Fortran 77 routine, use the nonstandard Fortran function %VAL(arg) as an argument in the call.
In the following example, the Fortran 77 routine passes x by value and y by reference. The C routine incremented both x and y, but only y is changed.
Fortran routine:
PRINT *, x,y
CALL value( %VAL(x), y)
PRINT *, x,y
C routine:
void value_( float x, float *y)
printf(&%f, %f\n&,x,*y);
x = x + 1.;
*y = *y + 1.;
printf(&%f, %f\n&,x,*y);
Compiling and running produces output:
x and y from Fortran
x and y from C
new x and y from C
new x and y from Fortran
A Fortran function that returns a value of type BYTE , INTEGER, REAL, LOGICAL, DOUBLE PRECISION, or REAL*16 is equivalent to a C function that returns a compatible type (see ). There are two extra arguments for the return values of character functions, and one extra argument for the return values of complex functions.
The following example returns a REAL or float value. BYTE, INTEGER, LOGICAL, DOUBLE PRECISION, and REAL*16 are treated in a similar way:
TABLE 11-12
Functions Returning a REAL or Float Value
real ADD1, R, S
external ADD1
S = ADD1( R )
------------------------------
float add1_( pf )
return ( f );
extern float fadd1_() ;
s = fadd1_( &r );
------------------------------
real function fadd1 (p)
fadd1 = p + 1.0
The situation for interoperability of COMPLEX data differs between SPARC V8 and V9 implementations.
A Fortran function returning COMPLEX or DOUBLE COMPLEX on SPARC V8 platforms is equivalent to a C function with an additional first argument that points to the return value in memory. The general pattern for the Fortran function and its corresponding C function is:
COMPLEX FUNCTION CF(a1, a2, ..., an)
cf_ (return, a1, a2, ..., an)
struct { float r, } *return
TABLE 11-13
Function Returning COMPLEX Data (SPARC V8)
COMPLEX U, V, RETCPX
EXTERNAL RETCPX
U = ( 7.0, -8.0)
V = RETCPX(U)
------------------------------
struct complex { float r, };
void retcpx_( temp, w )
struct complex *temp, *w;
temp-&r = w-&r + 1.0;
temp-&i = w-&i + 1.0;
struct complex { float r, };
struct complex c1, c2;
struct complex *u=&c1, *v=&c2;
extern retfpx_();
u -& r = 7.0;
u -& i = -8.0;
retfpx_( v, u );
------------------------------
COMPLEX FUNCTION RETFPX(Z)
RETFPX = Z + (1.0, 1.0)
In 64-bit environments and compiling with -xarch=v9, COMPLEX values are returned in floating-point registers: COMPLEX and DOUBLE COMPLEX in %f0 and %f1, and COMPLEX*32 in %f0, %f1, %f2, and %f3. For v9, a C function returning a structure whose fields are all floating-point types will return the structure in the floating-point registers if at most 4 such registers are needed to do so.The general pattern for the Fortran function and its corresponding C function on V9 platforms is:
COMPLEX FUNCTION CF(a1, a2, ..., an)
struct {float r,i;} cf_ (a1, a2, ..., an)
TABLE 11-14
Function Returning COMPLEX Data (SPARC V9)
COMPLEX U, V, RETCPX
EXTERNAL RETCPX
U = ( 7.0, -8.0)
V = RETCPX(U)
---------------------------------------------------------
struct complex {float r, };
struct complex retcpx_(struct complex *w )
temp.r = w-&r + 1.0;
temp.ii = w-&i + 1.0;
return (temp);
struct complex { float r, };
struct complex c1, c2;
struct complex *u=&c1;
extern struct complex retfpx_(struct complex *);
u -& r = 7.0;
u -& i = -8.0;
retfpx_( u );
---------------------------------------------------------
COMPLEX FUNCTION RETFPX(Z)
RETFPX = Z + (1.0, 1.0)
Passing strings between C and Fortran routines is not encouraged. However, a Fortran character-string-valued function is equivalent to a C function with two additional first arguments--data address and string length. The general pattern for the Fortran function and its corresponding C function is:
CHARACTER*n FUNCTION C(a1, ..., an)
void c_ (result, length, a1, ..., an)
char result[ ];
Here is an example:
TABLE 11-15
A Function Returning a CHARACTER String
CHARACTER STRING*16, CSTR*9
STRING = ' '
STRING = '123' // CSTR('*',9)
------------------------------
void cstr_( char *p2rslt,
long rslt_len,
char *p2arg,
long arg_len )
{ /* return n copies of arg */
int count,
count = *p2n;
for (i=0; i& i++) {
*cp++ = *p2
void fstr_( char *, long,
char *, int *, long );
char sbf[9] = &&;
char *p2rslt =
int rslt_len = sizeof(sbf);
char ch = '*';
int n = 4;
int ch_len = sizeof(ch);
/* make n copies of ch in sbf
fstr_( p2rslt, rslt_len,
&ch, &n, ch_len );
------------------------------
FUNCTION FSTR( C, N)
CHARACTER FSTR*(*), C
DO I = 1,N
FSTR(I:I) = C
FSTR(N+1:N+1) = CHAR(0)
In this example, the C function and calling C routine must accommodate two initial extra arguments (a pointer to the result string and the length of the string) and one additional argument at the end of the list (length of character argument). Note that in the Fortran routine called from C, it is necessary to explicitly add a final null character. Fortran strings are not null-terminated by default.
Fortran labeled COMMON can be emulated in C by using a global struct.
TABLE 11-16
Emulating Labeled COMMON
COMMON /BLOCK/ ALPHA,NUM
extern struct block {
extern struct block block_ ;
block_.alpha = 32.;
block_.num += 1;
Note that the external name established by the C routine must end in an underscore to link with the block created by the Fortran program. Note also that the C directive #pragma pack may be needed to get the same padding as with Fortran.
f95 aligns data in common blocks to at most 4-byte boundaries by default. To obtain the natural alignment for all data elements inside a common block and match the default structure alignment, use -aligncommon=16 when compiling the Fortran routines.
Mixing Fortran I/O with C I/O (issuing I/O calls from both C and Fortran routines) is not recommended. It is better to do all Fortran I/O or all C I/O, not both.
The Fortran I/O library is implemented largely on top of the C standard I/O library. Every open unit in a Fortran program has an associated standard I/O file structure. For the stdin, stdout, and stderr streams, the file structure need not be explicitly referenced, so it is possible to share them.
If a Fortran main program calls C to do I/O, the Fortran I/O library must be initialized at program startup to connect units 0, 5, and 6 to stderr, stdin, and stdout, respectively. The C function must take the Fortran I/O environment into consideration to perform I/O on open file descriptors.
Remember: even though the main program is in C, you should link with f95.
Fortran 77's alternate returns mechanism is obsolete and should not be used if portability is an issue. There is no equivalent in C to alternate returns, so the only concern would be for a C routine calling a Fortran routine with alternate returns. Fortran 95 will accept Fortran 77 alternate returns, but its use should be discouraged.
The implementation returns the int value of the expression on the RETURN statement. This is implementation dependent and its use should be avoided.
TABLE 11-17
Alternate Returns
int altret_ ( int * );
m = altret_( &k ) ;
printf( &%d %d\n&, k, m);
------------------------------
SUBROUTINE ALTRET( I, *, *)
IF(I .EQ. 0) RETURN 1
IF(I .GT. 0) RETURN 2
demo% cc -c tst.c
demo% f95 -o alt alt.f tst.o
The C routine receives the return value 2 from
the Fortran routine because it executed the
RETURN 2 statement.
The Fortran 2000 draft standard (available from http://www.j3-fortran.org) provides a means of referencing procedures and global variables defined by the C programming language from within a Fortran 95 program. And, conversely, provides a means for defining Fortran subprograms or global variables so that they can be referenced from C procedures.
By design, use of these features to accomplish interoperability between Fortran 95 and C programs insures portability across standards-conforming platforms.
Fortran 2000 provides the BIND attribute for derived types, and the ISO_C_BINDING intrinsic module. The module makes accessible to the Fortran program certain named constants, derived types, and procedures that support specification of interoperable objects. The details can be found in the Fortran 2000 draft standard, Section 15.}

我要回帖

更多关于 singlequot 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信