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 Fortran77, and newer Fortrans
such as Fortran90 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 mixedlanguage 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 casesensitive, in Fortran they are not.
For example, the variables named SUM and sum are different
in C, but the same in Fortran.
(2) Fortran77 (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.
Column6 has a special meaning, and is used to indicate a continuation of
the previous line.
By contrast, C and C++ (and Fortran90) are much more freeformat.
Back to Top
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

Back to Top
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)...
 .

Back to Top
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

Back to Top
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

Back to Top
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
END
Back 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
#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
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
END
Back 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++ file:
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.
 Digital (now Compaq/HP) Visual Fortran Programmer's Guide, esp. the chapter titled
"Programming with Mixed Languages". This online book is included with
all recent versions of the compiler. The book is also available
online by clicking here.

MixedLanguage Issues
(from Microsoft)
 Also see
Microsoft's C Calls to Fortran page.

Mixed Language Programming using C++ and Fortran 77
by Carsten A. Arnholm has many examples.

Mixed Language Programming  Fortran and C
by Allan, Chipperfield and WarrenSmith is another good source.

FTN95 Mixed Language Programming
from the University of Salford.

A Comparison of C++, Fortran 90 and Oberon2 for Scientific Programming
by Bernd Mösli.

Interfacing Fortran and C
by Janne Saarela.

Mixed Language Programming
from Pittsburgh Supercomputing Center.
 Some examples from DEC:

Will C++ be faster than Fortran?
by T.L.Veldhuizen and M.E.Jernigan.

Interfacing Absoft Fortran with C.

Mixing ANSIC with Fortran 77 or Fortran 90
by B. Einarsson.

Comparison of C++ and Fortran 90 for Object Oriented Scientific
Programming
by J.Cary, S.Shasharina, J.Cummings, J.Reynders and P.Hinker.

Fortran and C Programming from Iowa State University.

Win32 Fortran Compiler Comparisons by John Appleyard.

Calling Fortran Routines from C/C++ by J. Thornburg.

f2c  Fortran to C Converter
Acknowledgements
This work was done at the University of Wisconsin  Madison under the direction of Dr. W.S. Rhode.
Supported in part by a grant from NIH.
Back to Top
Feedback
Please send feedback/suggestions/questions/complaints to the author via email, at rkochhar@wisc.edu
Back to Top
(This page last modified on May 21, 2015)
