[comp.lang.fortran] f2c/F77

herbst@uncw.UUCP (R.T. Herbst) (01/02/91)

Having read much news about f2c, I acquired a copy with libraries 
from research.att.acom via ftp anon. To my delight, I discovered that
f2c is an extension of the Feldman Weinberger F77 compiler that was
distributed with early UNIX. This is an excellent compiler. Vanila 
F77. I recommend it. The price is certainly right. 
Works like a charm on my wgs6386/25. 
Since f2c generates K&R C Ansi C and C++, one might find an
examination of the "C" code useful in the FORTRAN vs. C debates.
Certainly the discussion of arrays could be brought into focus
by looking at the output of f2c.
I have used the Feldman Weinberger compiler in teaching F77 at UNCW.
It really fits neatly in to the UNIX environment. No suprise here....
F2c also meshes nicely with UNIX. Not to well known by the FW compiler
suports recursion. I have checked f2c and find that it also supports
recursion. Here is an example.
C Here is a C program that calculates n!.
c #include <stdio.h>
c #include "fnfac.h"
c main()
c {
c 	int n ,fnfac();
c 	printf("enter integer n \n");
c 	scanf("%d",&n);
c 	printf("n! = %d\n",fnfac(n));
c }
c 
c 
c /*fnfac(n)
c int n;
c {
c 	int i = 0;
c 	printf(" I am recursive %d\n", i++);
c 	if(n <= 1)
c 		return(1);
c 	return(n*fnfac(n-1));
c }*/
cHere is a f77 version
c Document F77 program to calculate n! by recursion.
C Declare:
	integer n, fnfac
C Execute
	print*, 'Enter an integer -> '
	read*, n
	print *, 'n! =',fnfac(n)
	end
C Recursive function fnfac to calculate n!.
	integer function fnfac(n)
	integer n
	if( n .le. 1) then
		fnfac = 1
		return
	else
		fnfac = n*fnfac(n-1)
		return
	endif
	end
C The fortran progran give the correct answer for n!
c for n .le. 12.
The output of f2c is

/* tfnfac.f -- translated by f2c (version of 19 December 1990  16:50:21).
   You must link the resulting object file with the libraries:
	-lF77 -lI77 -lm -lc   (in that order)
*/

#include "f2c.h"

/* Table of constant values */

static integer c__9 = 9;
static integer c__1 = 1;
static integer c__3 = 3;

/* Here is a c program that claculates n!. */
/* #include <stdio.h> */
/* #include "fnfac.h" */
/* main() */
/* { */
/* 	int n ,fnfac(); */
/* 	printf("enter integer n \n"); */
/* 	scanf("%d",&n); */
/* 	printf("n! = %d\n",fnfac(n)); */
/* } */


/* /*fnfac(n) */
/* int n; */
/* { */
/* 	int i = 0; */
/* 	printf(" I am recursive %d\n", i++); */
/* 	if(n <= 1) */
/* 		return(1); */
/* 	return(n*fnfac(n-1)); */
/* }+/ */
/* Here is a f77 version */
/* Document F77 program to calculate n! by recursion. */
/* Declare: */
/* Main program */ MAIN__()
{
    /* System generated locals */
    integer i__1;

    /* Builtin functions */
    integer s_wsle(), do_lio(), e_wsle(), s_rsle(), e_rsle();

    /* Local variables */
    extern integer fnfac_();
    static integer n;

    /* Fortran I/O blocks */
    static cilist io___1 = { 0, 6, 0, 0, 0 };
    static cilist io___2 = { 0, 5, 0, 0, 0 };
    static cilist io___4 = { 0, 6, 0, 0, 0 };


/* Execute */
    s_wsle(&io___1);
    do_lio(&c__9, &c__1, "Enter an integer -> ", 20L);
    e_wsle();
    s_rsle(&io___2);
    do_lio(&c__3, &c__1, (char *)&n, (ftnlen)sizeof(integer));
    e_rsle();
    s_wsle(&io___4);
    do_lio(&c__9, &c__1, "n! =", 4L);
    i__1 = fnfac_(&n);
    do_lio(&c__3, &c__1, (char *)&i__1, (ftnlen)sizeof(integer));
    e_wsle();
} /* MAIN__ */

/* Recursive function fnfac to calculate n!. */
integer fnfac_(n)
integer *n;
{
    /* System generated locals */
    integer ret_val, i__1;

    if (*n <= 1) {
	ret_val = 1;
	return ret_val;
    } else {
	i__1 = *n - 1;
	ret_val = *n * fnfac_(&i__1);
	return ret_val;
    }
    return ret_val;
} /* fnfac_ */