(Dr.R.K.) Mix Fortran77 & C - Collected Slides

Go to Top Go to Bottom blank blank Go up one level R.K.'s Home Page Keyword Index Site Map Help Linux


Slide 1
1 Title Page Mix Fortran77 & C - Title Page

Mix Fortran77 & C

R.K. Owen,Ph.D.

KooZ Software

email:rk@owen.sj.ca.us

email:rkowen@nersc.gov



Slide 2
2 Fortran & C Mix Fortran77 & C - Fortran & C


Slide 3
2.1 IBM SP2 Mix Fortran77 & C - IBM SP2 F77 & C
Fortran C size(bits)
INTEGER*1 signed char 8
INTEGER*2 short 16
INTEGER int 32
REAL float 32
DOUBLE PRECISION double 64
COMPLEX 2 float struct 64
DOUBLE COMPLEX 2 double struct 128
SUBROUTINE SUBR( ) void subr( )  
FUNCTION FN( ) float fn( )  
COMMON /CB/data(10) ?  


Slide 4
2.2 CRI C90 Mix Fortran77 & C - Cray F77 & C
Fortran C size(bits)
INTEGER int 46/64
cft77 -i64 cc -nofastmd  
INTEGER long 64
REAL double or float 64
DOUBLE PRECISION long double 128
COMPLEX float complex
or double complex
128
SUBROUTINE SUBR( ) void SUBR( )  
FUNCTION FN( ) double FN( )  
COMMON /CB/data(10) struct common {
double data[10];
} CB;
 


Slide 5
2.2 Mix Fortran77 & C - F77 & C - more
Note Differences

Slide 6
2.3 Fortran to C Mix Fortran77 & C - F77 & C example

Fortran calling C function

Fortran Main

 
      PROGRAM F2C
C
      CHARACTER*32 NAME
      INTEGER AGE
      REAL TEMP
C
      NAME = "Knut"
C add null character at end for portable & safe handling by C
      NAME(LEN(NAME):LEN(NAME)) = CHAR(0)
C note that LEN(NAME) = 32 in this case
      AGE = 4
      TEMP = 98.6
      CALL NAMEAGE(NAME, AGE, TEMP)
      END
 

C Function

 
#include <string.h>
#ifdef _CRAY
#  include <fortran.h>
#  define nameage       NAMEAGE
#else
#  ifndef _AIX
#    define nameage	nameage_
#  endif
#  define _fcd          char *
#  define _fcdtocp(a)   (a)
#  define _fcdlen(a)    strlen(a)
#endif

void nameage(_fcd name, int *age, float *temp) {
        char *cp;
        size_t len;

        cp = _fcdtocp(name);    /* convert to C char* */
        len = _fcdlen(name);

        /* strip trailing blanks */
        while (cp[len-1] == ' ' || cp[len-1] == '\0') --len;
        printf("Hello %.*s, who is %d years old, "
                "has a temperature of %4.1f\n", len, cp, *age, *temp);
}
 

Compilation Steps



Slide 7
2.4 C to Fortran Mix Fortran77 & C - F77 & C example

C calling Fortran subroutine

C Main

 
#include <string.h>
#ifdef _CRAY
#  include <fortran.h>
#  define nameage      NAMEAGE
#else
#  ifndef _AIX
#    define nameage    nameage_
#  endif
#  define _fcd          char *
#  define _cptofcd(a,b) (a)
#  define _fcdlen(a)    strlen(a)
#endif

void nameage(_fcd name, int *nlen, int *age, float *temp);

int main() {
        char *name = "Knut";
        _fcd fp;
        int nlen,age = 4;
        float temp = 98.6;

        nlen = strlen(name);
        fp = _cptofcd(name, nlen);      /* convert to Fortran string */

        nameage(fp, &nlen, &age, &temp);
        return 0;
}
 

Fortran Subroutine

 
      SUBROUTINE NAMEAGE(NAME, NLEN, AGE, TEMP)
      CHARACTER*(*) NAME
      INTEGER NLEN,AGE
      REAL TEMP
C
      WRITE(6,1000) NAME(1:NLEN),AGE,TEMP
 1000 FORMAT(1X,'Hello ',A,', who is ',I2,
     .      ' years old, has a temperature of ', f4.1)
      RETURN
      END
 

Compilation Steps

To discover which libraries are necessary for Fortran modules, compile a short Fortran test program and add the -V option (or equivalent) to get a verbose execution listing.

Last Modified:
Brought to you by: R.K. Owen, Ph.D.
This page is http://rkowen.owentrek.com/howto/slides/FandC/ALL.html