HPlogo HP C/HP-UX Programmer's Guide: Workstations and Servers > Chapter 3 Calling Other Languages

Comparing HP C and HP FORTRAN 77

» 

Technical documentation

Complete book in PDF

 » Table of Contents

 » Index

Table 3-2 “HP C versus HP FORTRAN 77 Storage ” shows the differences in storage allocation between HP C and HP FORTRAN 77. The notes the table refers to are located after the table in the section called "Notes on HP C and HP FORTRAN 77."

Table 3-2 HP C versus HP FORTRAN 77 Storage

HP C Type

HP C Description

HP FORTRAN 77 Type

HP FORTRAN 77 Description

char, signed char, char enum

1 byte, byte aligned

1 byte, 1-byte aligned

unsigned char

1 byte, byte aligned

CHARACTER*1

1 byte, 1-byte aligned

short, short enum

2 bytes, 2-byte aligned

INTEGER*2

2 bytes, 2-byte aligned

unsigned short

2 bytes, 2-byte aligned

int, int enum

4 bytes, 4-byte aligned

INTEGER*4 or INTEGER

4 bytes, 4-byte aligned

unsigned int

4 bytes, 4-byte aligned

long, long enum

4 bytes, 4-byte aligned

INTEGER*4 or INTEGER

4 bytes, 4-byte aligned

unsigned long

4 bytes, 4-byte aligned

float

4 bytes, 4-byte aligned

REAL or REAL*4

4 bytes, 4-byte aligned

double

8 bytes, 8-byte aligned

REAL*8 or DOUBLE PRECISION

8 bytes, 8-byte aligned

long double

16 bytes, 16-byte aligned

REAL*16

16 bytes, 16-byte aligned

(See Note 1)

8 bytes, 4-byte aligned

COMPLEX or COMPLEX*8

8 bytes, 4-byte aligned

(See Note 2)

16 bytes, 8-byte aligned

DOUBLE COMPLEX or COMPLEX*16

16 bytes, 8-byte aligned

enum

4 bytes, 4-byte aligned

INTEGER*4 or INTEGER

4 bytes, 4-byte aligned

pointer to type long pointer to type

Not available

string (char *)

CHARACTER*n (See Note 3)

char array

CHARACTER*1 array (See Notes 4 &5)

(See Note 5)

Hollerith array

arrays

Size is number of elements times element size. Aligned according to element type.

(See Note 4)

Size is number of elements times element size. Aligned according to element type.

struct

(See Note 6)

STRUCTURE

Used to declare FORTRAN 77 record structures.

union

(See Note 6)

UNION

Used to declare FORTRAN 77 union types.

short (Used for logical test)

2 bytes, 2-byte aligned

LOGICAL*2 (See Note 7)

2 bytes, 2-byte aligned

int (Used for logical test)

4 bytes, 4-byte aligned

LOGICAL*4 (See Note 7)

4 bytes, 4-byte aligned

void

-

Used when calling a SUBROUTINE

function

Used when calling a FUNCTION

 

Notes on HP C and HP FORTRAN 77

  1. The following HP C structure is equivalent to the HP FORTRAN 77 type listed in the table:

         struct complex {
    float real_part;
    float imag_part;
    };
  2. The following HP C structure is equivalent to the HP FORTRAN 77 type listed in the table:

         struct double_complex {
    double real_part;
    double imag_part;
    };
  3. HP FORTRAN 77 passes strings as parameters using string descriptors corresponding to the following HP C declarations:

            char *char_string;  /* points to string */
    int len; /* length of string */
  4. HP C stores arrays in row-major order, whereas HP FORTRAN 77 stores arrays in column-major order. The lower bound for HP C is always zero; for HP FORTRAN 77, the default lower bound is 1.

  5. HP C terminates character strings with a null byte, while HP FORTRAN 77 does not.

  6. The size is equal to the size of all members plus any padding needed for the alignment. (See Chapter 2 for details on alignment.) The alignment is that of the member with the strictest alignment requirement.

  7. HP C and HP FORTRAN 77 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 interprets nonzero value as true and interprets zero as false.

Mixing C and FORTRAN File 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. C accesses the file using I/O subroutines and intrinsics. This method of file access can also be used from FORTRAN instead of FORTRAN I/O.

Be aware that HP FORTRAN 77 on HP 9000 workstations and servers using HP-UX uses the unbuffered I/O system calls read and write (described in the HP-UX Reference manual) for all terminal I/O, magnetic tape I/O, and direct access I/O. It uses the system calls fread and fwrite for all other I/O. This can cause problems in programs that mix C and FORTRAN I/O. In particular, C programs that use stdio(3S) output procedures such as printf and fwrite and FORTRAN output statements must flush stdio buffers (by calling the libc function fflush) if they are in use before returning to FORTRAN output or the I/O may be asynchronous (if the library is using write).

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 disc read. You can avoid these problems by using only the read and write calls in the C program that the FORTRAN I/O library uses.

Passing Parameters Between HP C and HP FORTRAN 77

All parameters in HP FORTRAN 77 are passed by reference. This means that all arguments in an HP C call to an HP FORTRAN 77 routine must be pointers. In addition, all parameters in an HP C routine called from HP FORTRAN 77 must be pointers, unless the HP FORTRAN 77 code uses the $ALIAS directive to define the parameters as value parameters. Refer to the example called "HP FORTRAN 77 Nested Structure" later in this chapter.

Passing string variables of any length must be done by: building and passing a two-parameter descriptor (defined in Note 3 above), initializing the string appropriately, and by passing two arguments. The two arguments are the pointer to the characters and the value of the length word. This is shown below:

     char *chars = "Big Mitt";
int len;
.
.
.
len = strlen(chars);

pass_c_string (chars, len);
.
.
.

Linking HP FORTRAN 77 Routines on HP-UX

When calling HP FORTRAN 77 routines on an HP-UX system, you have to include the HP FORTRAN 77 run-time libraries by adding the option:

-lcl

to the cc command line.

For details on linking external libraries, see the -l option of the cc(1) and ld(1) commands in the HP-UX Reference manual.

© Hewlett-Packard Development Company, L.P.