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) 
If you notice any error or omission please send me an email.
Arithmetic operations/functions
.
Fortran
Example
C/C++
Example
Comment
Add
+
A=B+C
+
a=b+c;
.
Subtract

A=BC

a=bc;
.
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
Relational and Logical Operators
.
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(ab)...
.
Bitwise Operators
.
Fortran
Example
C/C++
Example
Comment
Bitwise AND
IAND
IAND(N,M)
&
n&m
.
Bitwise OR
IOR
IOR(N,M)

nm
.
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
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".
.
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
Calling Fortran routines from C++
Example1: Calling routines and functions
The following sample shows how Fortran routines and functions can be called from a C++ program.
(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 ENDBack to Top
Example2: 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.
(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 ENDBack to Top
Example3: Passing arrays to a Fortran routine
The following sample shows how arrays may be passed from a C++ program to a Fortran routine.
(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 ENDBack to Top
Calling C++ routines from Fortran
The following examples work with Microsoft Visual C++ and Compaq Visual Fortran. Your mileage may vary on other systems.
Example1: Calling routines and functions
The following sample shows how C++ routines and functions can be called from a Fortran program.
(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
Further Reading
These are some other sources of information.
If you have questions about this document please send them by email to kochhar@physiology.wisc.edu
Return to Documentation Page
Back to The Basement
This page last modified on : Nov. 18, 2008