|
Fortran and C/C++ Mixed Programming |
|
| Table of Contents |
Programming Note No. 17 Ravi Kochhar kochhar@physiology.wisc.edu Dept. of Physiology Univ. of Wisconsin - Madison Feb. 9, 1999 Rev. Level 1.008 (11/18/2008) ![]() |
Introduction
This is an attempt to list some common programming features and how
they are implemented in Fortran and in C and C++.
The main goal is to help anyone who has to write programs in both languages
and needs a quick way to compare/translate some function or the other.
This note also covers topics related to writing mixed language programs.
This is not an attempt to list the relative merits of either language.
This is also not very complete, just the things that I found interesting
or convenient to document.
It is important to bear in mind that C and C++ are quite different from each
other, though they share some common syntax.
I have used them interchangeably for this note, but I have tried to add a
comment if something will work in one and not the other.
The Fortran features discussed below are for Fortran-77, and newer Fortrans
such as Fortran-90 may do some things differently.
Some of the examples/syntax described below may be specific to Digital
Fortran.
I have shown Fortran examples in uppercase characters, but they
work just as well in lowercase.
The mixed-language examples are for the Microsoft Visual C++ compiler and
Digital Visual Fortran.
They should work in other environments with minimal changes.
If you notice any error or omission
please send me an email.
General Observations
(1) Most things in C/C++ are case-sensitive, in Fortran they are not.
For example, the variables named SUM and sum are different
in C, but the same in Fortran.
(2) Fortran-77 (and earlier) will ignore anything after the 72'nd column of
any record.
In addition, the first 5 columns are reserved for either a statement label
or comment identifier.
Column-6 has a special meaning, and is used to indicate a continuation of
the previous line.
By contrast, C and C++ (and Fortran-90) are much more free-format.
Arithmetic operations/functions
Relational and Logical Operators
Bitwise Operators
Mathematical Functions
Note: In C/C++, must include the header file "math.h" to use these functions.
Angles must be specified in radians for these functions. "n/a" means "not available".
Calling Fortran routines from C++
Example-1: Calling routines and functions
The following sample shows how Fortran routines and functions can be
called from a C++ program.
Example-2: Passing C char string to a Fortran routine
The following sample shows how a C char string may be passed from
a C++ program to a Fortran routine.
Example-3: Passing arrays to a Fortran routine
The following sample shows how arrays may be passed from
a C++ program to a Fortran routine.
Calling C++ routines from Fortran
The following examples work with Microsoft Visual C++ and Compaq Visual
Fortran.
Your mileage may vary on other systems.
Example-1: Calling routines and functions
The following sample shows how C++ routines and functions can be
called from a Fortran program.
Further Reading
These are some other sources of information.
If you have questions about this document
please send them by e-mail to
kochhar@physiology.wisc.edu
Return to Documentation Page
.
Fortran
Example
C/C++
Example
Comment
Add
+
A=B+C
+
a=b+c;
.
Subtract
-
A=B-C
-
a=b-c;
.
Multiply
*
A=B*C
*
a=b*c;
.
Divide
/
A=B/C
/
a=b/c;
.
Modulus
MOD
A=MOD(B,C)
%
a=b%c;
ints only in C, reals possible in Fortran
Power
**
A=B**C
pow()
a=pow(b,c);
C/C++ function not intrinsic, provided via math.h
.
Fortran
Example
C/C++
Example
Comment
Equal to
.EQ.
IF(A.EQ.B)...
==
if(a==b)...
.
Not Equal to
.NE.
IF(A.NE.B)...
!=
if(a!=b)...
.
Less Than
.LT.
IF(A.LT.B)...
<
if(a<b)...
.
Greater Than
.GT.
IF(A.GT.B)...
>
if(a>b)...
.
Less Than or Equal to
.LE.
IF(A.LE.B)...
<=
if(a<=b)...
.
Greater Than or Equal to
.GE.
IF(A.GE.B)...
>=
if(a>=b)...
.
Logical Not
.NOT.
IF(.NOT.A)...
!
if(!a)...
.
Logical AND
.AND.
IF(A.AND.B)...
&&
if(a&&b)...
.
Logical OR
.OR.
IF(A.OR.B)...
||
if(a||b)...
.
.
Fortran
Example
C/C++
Example
Comment
Bitwise AND
IAND
IAND(N,M)
&
n&m
.
Bitwise OR
IOR
IOR(N,M)
|
n|m
.
Bitwise Exclusive OR
IEOR
IEOR(N,M)
^
n^m
.
Bitwise 1's Complement
INOT
INOT(N)
~
~n
.
Bitwise Left Shift
ISHFT
ISHFT(N,M) (M > 0)
<<
n<<m
n shifted left by m bits
Bitwise Right Shift
ISHFT
ISHFT(N,M) (M < 0)
>>
n>>m
n shifted right by m bits
.
Fortran
Example
C/C++
Example
Comment
Sine
SIN
SIN(R)
n/a
n/a
Single Precision
Sine
DSIN
DSIN(R)
sin
sin(r)
Double Precision
Cosine
COS
COS(R)
n/a
n/a
Single Precision
Cosine
DCOS
DCOS(R)
cos
cos(r)
Double Precision
Tangent
TAN
TAN(R)
n/a
n/a
Single Precision
Tangent
DTAN
DTAN(R)
tan
tan(r)
Double Precision
Arc Sine
ASIN
ASIN(R)
n/a
n/a
Single Precision
Arc Sine
DASIN
DASIN(R)
asin
asin(r)
Double Precision
Arc Cosine
ACOS
ACOS(R)
n/a
n/a
Single Precision
Arc Cosine
DACOS
DACOS(R)
acos
acos(r)
Double Precision
Arc Tangent
ATAN
ATAN(R)
n/a
n/a
Single Precision
Arc Tangent
DATAN
DATAN(R)
atan
atan(r)
Double Precision
Hyperbolic Sine
SINH
SINH(R)
n/a
n/a
Single Precision
Hyperbolic Sine
DSINH
DSINH(R)
sinh
sinh(r)
Double Precision
Hyperbolic Cosine
COSH
COSH(R)
n/a
n/a
Single Precision
Hyperbolic Cosine
DCOSH
DCOSH(R)
cosh
cosh(r)
Double Precision
Hyperbolic Tangent
TANH
TANH(R)
n/a
n/a
Single Precision
Hyperbolic Tangent
DTANH
DTANH(R)
tanh
tanh(r)
Double Precision
(1) The C++ file:
// This illustrates how a Fortran routine and function may be
// called from a main program in C++
#include <iostream.h>
extern "C"
{
void __stdcall FR1(int*,int *);
int __stdcall FF1(int *);
}
int main()
{
int n=10,nSq,nCube;
FR1(&n,&nSq);
cout << "The square is:" << nSq << endl;
nCube=FF1(&n);
cout << "The Cube is:" << nCube << endl;
return 0;
}
(2) The Fortran File:
SUBROUTINE FR1(N,M)
C COMPUTES THE SQUARE OF N, RETURNS IN M
M=N*N
RETURN
END
C
INTEGER FUNCTION FF1(N)
C COMPUTES THE CUBE OF N
FF1=N*N*N
RETURN
END
Back to Top
(1) The C++ file:
// This illustrates how a Fortran routine may be
// called from a main program in C++, and a char[] string passed to it
#include <iostream.h>
#include <string.h>
extern "C"
{
void __stdcall FR1(int *,int *,char *);
}
int main()
{
int n=10,nSq;
char szCtest[20];
strcpy(szCtest,"teststring");
FR1(&n,&nSq,szCtest);
cout << "The square is:" << nSq << endl;
return 0;
}
(2) The Fortran File:
SUBROUTINE FR1(N,M,CSTR)
INTEGER*4 CSTR(1)
C HERE WE RECEIVE THE C CHAR STRING IN AN INTEGER ARRAY
C COULD ALSO HAVE USED A BYTE ARRAY
M=N*N
WRITE(6,20) (CSTR(L),L=1,3)
20 FORMAT(' CSTR=',3A4)
WRITE(6,*) 'DONE'
RETURN
END
Back to Top
(1) The C++ file:
// Illustrate passing integer and floating point arrays
// from C++ to Fortran
#include <iostream.h>
extern "C"
{
int __stdcall SUMIT(int *,int*);
float __stdcall MEAN(float*,int*);
}
int main()
{
int iA[]={3,5,6,7,2,3,4,5,11,7},iN=10,iSum;
float fpA[]={1.2f,3.f,44.f,2.5f,-1.3f,33.44f,5.f,0.3f,-3.6f,24.1f},fpMean;
iSum=SUMIT(iA,&iN);
cout << "The Sum of iA is:" << iSum << endl;
fpMean=MEAN(fpA,&iN);
cout << "The Mean of fpA is:" << fpMean << endl;
return 0;
}
(2) The Fortran File:
INTEGER FUNCTION SUMIT(IA,N)
INTEGER IA(1)
ISUM=0
DO 50 J=1,N
50 ISUM=ISUM+IA(J)
SUMIT=ISUM
RETURN
END
C
REAL FUNCTION MEAN(RA,N)
REAL RA(1)
SUM=0.
DO 50 J=1,N
50 SUM=SUM+RA(J)
IF(N.GT.0) MEAN=SUM/FLOAT(N)
RETURN
END
Back to Top
(1) The Fortran file:
INTEGER CR2
N=10
CALL CR1(N,M)
WRITE(6,20) N,M
20 FORMAT(' The square of',I3,' is',I4)
K=CR2(N)
WRITE(6,30) N,K
30 FORMAT(' The cube of',I3,' is',I15)
CALL EXIT
END
(2) The C++ files:
extern "C"
{
void __stdcall CR1(int *,int *);
int __stdcall CR2(int *);
}
void __stdcall CR1(int *n, int *m)
{
// Compute the square of n, return in m
int k;
k=*n;
*m=k*k;
return;
}
int __stdcall CR2(int *n)
// Compute the cube of n
{
int m,k;
k=*n;
m=k*k*k;
return m;
}
Back to Top
Back to The Basement
This page last modified on : Nov. 18, 2008