HP 3000 Manuals

HP C/iX [ HP FORTRAN 77/iX Programmer's Guide ] MPE/iX 5.0 Documentation


HP FORTRAN 77/iX Programmer's Guide

HP C/iX 

HP C/iX, when invoked in ANSI mode, is a conforming implementation of
ANSI C, as specified by American National Standard X3.159-1989.  It runs
on the HP 3000 Series 900 computer.

An HP C/iX procedure or function can be called from an HP FORTRAN 77/iX
program and an HP FORTRAN 77/iX program can call an HP C/iX procedure or
function if the data types of the parameters match (see the table below).
The ALIAS compiler directive should be used for correctly passing
parameters.

          Table 8-4.  HP FORTRAN 77/iX and HP C/iX Types 

---------------------------------------------------------------------------------------------
|                                             |                                             |
|            HP FORTRAN 77/iX Type            |                HP C/iX Type                 |
|                                             |                                             |
---------------------------------------------------------------------------------------------
|                                             |                                             |
| --                                          | char                                        |
|                                             |                                             |
---------------------------------------------------------------------------------------------
|                                             |                                             |
| CHARACTER*1                                 | unsigned char                               |
|                                             |                                             |
---------------------------------------------------------------------------------------------
|                                             |                                             |
| INTEGER*2                                   | short                                       |
|                                             |                                             |
---------------------------------------------------------------------------------------------
|                                             |                                             |
| --                                          | unsigned short                              |
|                                             |                                             |
---------------------------------------------------------------------------------------------
|                                             |                                             |
| INTEGER*4                                   | int                                         |
| or INTEGER                                  |                                             |
|                                             |                                             |
---------------------------------------------------------------------------------------------
|                                             |                                             |
| --                                          | unsigned int                                |
|                                             |                                             |
---------------------------------------------------------------------------------------------
|                                             |                                             |
| INTEGER*4                                   | long                                        |
|                                             |                                             |
---------------------------------------------------------------------------------------------
|                                             |                                             |
| --                                          | unsigned long                               |
|                                             |                                             |
---------------------------------------------------------------------------------------------
|                                             |                                             |
| REAL or REAL*4                              | float                                       |
|                                             |                                             |
---------------------------------------------------------------------------------------------
|                                             |                                             |
| REAL*8 or DOUBLE                            | long float                                  |
| PRECISION                                   |                                             |
|                                             |                                             |
---------------------------------------------------------------------------------------------
|                                             |                                             |
| REAL*8 or DOUBLE                            | double                                      |
| PRECISION                                   |                                             |
|                                             |                                             |
---------------------------------------------------------------------------------------------
|                                             |                                             |
| COMPLEX or                                  | (See Note 1)                                |
| COMPLEX*8                                   |                                             |
|                                             |                                             |
---------------------------------------------------------------------------------------------
|                                             |                                             |
| DOUBLE COMPLEX                              | (See Note 2)                                |
| or COMPLEX*16                               |                                             |
|                                             |                                             |
---------------------------------------------------------------------------------------------

          HP FORTRAN 77/iX and HP C/iX Types (Continued) 

---------------------------------------------------------------------------------------------
|                                             |                                             |
|            HP FORTRAN 77/iX Type            |                HP C/iX Type                 |
|                                             |                                             |
---------------------------------------------------------------------------------------------
|                                             |                                             |
| INTEGER*4                                   | enum                                        |
|                                             |                                             |
---------------------------------------------------------------------------------------------
|                                             |                                             |
| Not available.                              | pointer to type                             |
|                                             |                                             |
|                                             | long pointer to                             |
|                                             |                                             |
|                                             | type                                        |
|                                             |                                             |
---------------------------------------------------------------------------------------------
|                                             |                                             |
| CHARACTER*n                                 | string                                      |
|                                             | (char *)                                    |
| (See Note 3)                                |                                             |
|                                             |                                             |
---------------------------------------------------------------------------------------------
|                                             |                                             |
| CHARACTER*1                                 | char                                        |
| array                                       | array                                       |
|                                             |                                             |
| (See Notes 4 & 5)                           |                                             |
|                                             |                                             |
---------------------------------------------------------------------------------------------
|                                             |                                             |
| Hollerith array                             | See Note 5)                                 |
|                                             |                                             |
---------------------------------------------------------------------------------------------
|                                             |                                             |
| (See Note 4)                                | arrays                                      |
|                                             |                                             |
---------------------------------------------------------------------------------------------
|                                             |                                             |
| LOGICAL*2                                   | short (Used for                             |
|                                             | logical test)                               |
| (See Note 6)                                |                                             |
|                                             |                                             |
---------------------------------------------------------------------------------------------
|                                             |                                             |
| LOGICAL*4                                   | int (Used for                               |
|                                             | logical test)                               |
| (See Note 6)                                |                                             |
|                                             |                                             |
---------------------------------------------------------------------------------------------
|                                             |                                             |
| Used when calling a                         | void                                        |
| SUBROUTINE                                  |                                             |
|                                             |                                             |
---------------------------------------------------------------------------------------------
|                                             |                                             |
| Used when calling a                         | function                                    |
| FUNCTION                                    |                                             |
|                                             |                                             |
---------------------------------------------------------------------------------------------

Notes on HP FORTRAN 77/iX and HP C/iX Types 

   1.  The FORTRAN 77 type of COMPLEX or COMPLEX*8 is equivalent to the
       following HP C/iX structure:

            struct complex {
               float real_part;
               float imag_part;
            };

   2.  The FORTRAN 77 type of DOUBLE COMPLEX or COMPLEX*16 is equivalent
       to the following HP C/iX structure:

            struct complex {
               double real_part;
               double imag_part;
            };

   3.  HP FORTRAN 77 passes character strings as parameters using string
       descriptors corresponding to the following HP C/iX declarations:

            char *char_string;  /* points to string */
            int len;            /* length of string */

   4.  HP FORTRAN 77/iX stores arrays in column-major order whereas HP
       C/iX stores arrays in row-major order.  The default lower bound
       for HP FORTRAN 77 is one; for HP C, the lower bound is always
       zero.

   5.  HP FORTRAN 77 does not terminate character or Hollerith strings
       with a null byte, but HP C does.

   6.  HP FORTRAN 77 and HP C do not share a common definition of true or
       false.  In HP FORTRAN 77, logical values are determined by the
       low-order bit of the high-order byte.  If this bit is 1, the
       logical value is .TRUE., and if the bit is zero, the logical value
       is .FALSE..  HP C uses any nonzero value to represent true and
       uses zero for false.

Files and I/O 

A FORTRAN unit cannot be passed to a C routine to perform I/O on the
associated file.  Nor can a C file pointer be used by a FORTRAN routine.
However, a file created by a program written in either language can be
used by a program of the other language if the file is declared and
opened within the latter program.

Mixing FORTRAN direct, terminal, or tape READ statements with stdio fread
input results in the FORTRAN READ commencing from the beginning of the
next block after the contents of the buffer, not from the current
position of the input cursor in the fread buffer.  The same situation in
reverse may occur by mixing read with a FORTRAN sequential disk read.

Parameter Passing between HP FORTRAN 77 and HP C 

The major difference is that FORTRAN and C pass parameters
differently--FORTRAN by reference and C by value.  This means that all
actual parameters in an HP C call to an HP FORTRAN 77 routine must be
pointers or variables prefixed with the unary address-of operator &.  In
addition, all formal parameters in an HP C routine called from HP FORTRAN
77 must be pointers, unless you use the $ALIAS directive in the HP
FORTRAN 77 code to change FORTRAN's parameter passing mechanism so the
parameters are defined as value parameters.  Refer to the HP FORTRAN 
77/iX Reference, chapter 7 for more information about the $ALIAS
directive.

To pass string variables of any length from an HP C call to an HP FORTRAN
77 subroutine you must build and pass a two-parameter descriptor (defined
in Note 3 above), initialize the string appropriately, and pass two
arguments.  The two arguments are the pointer to the characters and the
value of the length word.  This is shown below:

     /* C program */
     extern print_str();
     main()
     {
       char *str ="ABCDEFG";
       int len;

       len = strlen (str);
       (void) print_str (str, len);
     }

     C    FORTRAN program
     C
          SUBROUTINE print_str (str, len)
     C    ASSUME MAX LENGTH OF 300
          character*300 str
          integer len

          if (len .GE. 5) then
            print *, str(1:5)
          else
            print *, str(1:len)
          endif
          END

This example shows passing a character string from a FORTRAN program to a
C function.  The function returns the number of characters in the string
before a space.  Otherwise it returns the maximum string length.

     /* C program */
     #define MSLEN 300

     sizer(x) char *x;
     {
          register int i;

          for (i=0; i <MSLEN; i++)
               if (x[i] == ' ') return(i);
          return(MSLEN);
     }

     C FORTRAN program
     $alias sizer='sizer'(%ref)
           program test
           character*300 x
           integer sizer
           external sizer
           integer i
           data x/"abcdefghi klmnop"/

           i = sizer(x)
           print *,i
           end



MPE/iX 5.0 Documentation