Jump to content United States-English
HP.com Home Products and Services Support and Drivers Solutions How to Buy
» Contact HP
More options
HP.com home
Fortran 90 Compiler for HP-UX: Fortran 90 Programmer's Guide > Chapter 8 Calling C routines from HP Fortran 90

Data types

» 

Technical documentation

Complete book in PDF
» Feedback
Content starts here

 » Table of Contents

 » Glossary

Table 8-1 “Data type correspondence for HP Fortran 90 and C ” lists the corresponding data types for HP Fortran 90 and C when compiled as 32-bit applications .

Table 8-1 Data type correspondence for HP Fortran 90 and C

HP Fortran 90

C

CHARACTER

char (array of)

Hollerith (synonymous with CHARACTER)

char (array of)

BYTE, LOGICAL(KIND=1), INTEGER(KIND=1)

char

LOGICAL(KIND=2)

short

INTEGER(KIND=2)

short

LOGICAL, LOGICAL(KIND=4)

long or int

INTEGER, INTEGER(KIND=4)

long or int

INTEGER(KIND=8)

long long

REAL, REAL(KIND=4)

float

DOUBLE PRECISION, REAL(KIND=8)

double

REAL(KIND=16)

long double

COMPLEX, COMPLEX(KIND=4)

struct

DOUBLE COMPLEX, COMPLEX(KIND=8)

struct

derived type

struct

 

Using the +DA2.0W option to compile HP Fortran 90 programs in 64-bit mode has no effect on Fortran data types; see “Compiling in 64-bit mode”. However, it does change the sizes of some C data types. If your program calls functions written in C and is compiled in 64-bit mode, you should be aware of the size discrepancies and either promote individual data items or recompile with the +autodbl option to promote all default integer, real, and logical items to 64-bits.

Table 8-2 “Size differences between HP Fortran 90 and C data types” shows the differences between the corresponding data types in HP Fortran 90 and C when compiling in 32-bit mode and in 64-bit mode. Table 8-3 “Size differences after compiling with +autodbl shows the differences when the Fortran program is compiled with the +autodbl option. Notice that Fortran data items that are explicitly sized (for example, INTEGER*4) stay the same size regardless of whether they are compiled in 32-bit mode, in 64-bit mode, or with the +autodbl option.

Table 8-2 Size differences between HP Fortran 90 and C data types

HP Fortran 90
data types
C data typesSizes (in bits)
32-bit mode64-bit mode
INTEGERint or longint32
INTEGER*4int or longint32
INTEGER*8long longlong or
long long
64
REALfloatfloat32
DOUBLE PRECISIONdoubledouble64
REAL*16long doublelong double128

 

Table 8-3 Size differences after compiling with +autodbl

HP Fortran 90
data types
C data typesSizes (in bits)
32-bit mode64-bit mode
INTEGERlong longlong64
INTEGER*4int or longint32
INTEGER*8long longlong64
REALfloatfloat64
DOUBLE PRECISIONlong doublelong double128
REAL*16long doublelong double128

 

The following sections provide more detailed information about language differences for the following data types:

  • Unsigned integers

  • Logicals

  • Complex numbers

  • Derived types

Unsigned integers

Unlike Fortran, C allows integer data types (char, int, short, and long) to be declared as either signed or unsigned. If a Fortran program passes a signed integer to a C function that expects an unsigned integer argument, C will interpret the bit pattern as an unsigned value.

An unsigned integer in C can represent twice the number of positive values as the same-sized integer in HP Fortran 90. If an HP Fortran 90 program calls a C function that returns an unsigned integer and the return value is greater than can be represented in a signed integer, HP Fortran 90 will interpret the bit pattern as a negative number.

Logicals

C uses integers for logical types. In HP Fortran 90, a 2-byte LOGICAL is equivalent to a C short, and a 4-byte LOGICAL is equivalent to a long or int. In C and HP Fortran 90, zero is false and any nonzero value is true. HP Fortran 90 sets the value 1 for true.

Complex numbers

C has no complex numbers, but they are easy to simulate. To illustrate this, create a struct type containing two floating-point members of the correct size — two floats for the complex type, and two doubles for the double complex type. The following creates the typedef COMPLEX:

typedef struct
{
float real;
float imag;
} COMPLEX;

Consider a program that consists of two source files:

  • The Fortran 90 source file, which defines the main program unit

  • The C source file, which defines a function sqr_complex, having the following prototype declaration:

    COMPLEX sqr_complex(COMPLEX cmx_val);

The main subprogram calls sqr_complex, passing in a complex number. The C function squares the number and returns the result. There is no complex data type in C, but this example uses C's typedef feature to create one.

The Fortran source file for such a scenario is shown below in the example pass_complex.f90.

Example 8-1 pass_complex.f90

PROGRAM main
! This program passes a complex number to a C function
! that squares it and returns the result. The C
! function has the following declaration prototype:
!
! complex sqr_complex(complex cmx_val);
!
! "complex" is not an intrinsic type for C but it
! creates a typedef for one, using a struct.

COMPLEX :: result, cmx_num = (2.5, 3.5)

! We have to declare the C function because we're calling it
! as a function rather than a subroutine. If we didn't
! declare it, Fortran would use the implicit typing rules
! by default and assume from the name, sqr_complex, that it
! returns a real.
COMPLEX sqr_complex

PRINT *, "C will square this complex number: ", cmx_num

! Use the %VAL built-in function to indicate that cmx_num
! is being passed by value, as C expects it to be, and
! and not by reference, as Fortran does by default
result = sqr_complex(%VAL(cmx_num))

PRINT *, "The squared result is: ", result

END PROGRAM main

Following is the C source file:

Example 8-2 sqr_complex.c

#include <stdio.h>

/* simulate Fortran's complex number */
typedef struct
{ float real;
float imag;
}COMPLEX;

/* returns the square of the complex argument */
COMPLEX sqr_complex(COMPLEX cmx_val)
{
COMPLEX result;
float a, b;

/* copy both parts of the complex number into locals */
a = cmx_val.real;
b = cmx_val.imag;

/* square the complex number and store the results into
* the return variable
*/
result.imag = 2 * (a * b);
a = a * a;
b = b * b;
result.real = a - b;

return result;
}

Here are the command lines to compile, link, and execute the program, followed by the output from a sample run:

$ cc -Aa -c sqr_complex.c
$ f90 pass_complex.f90 sqr_complex.o
$ a.out
C will square this complex number: (2.5,3.5)
The squared result is: (-6.0,17.5)

Derived types

Although the syntax of Fortran"s derived types differs from that of C"s structures, both languages have similar default packing and alignment rules. HP Fortran 90 uses the same packing rules and alignments when laying out derived-type objects in memory that HP C uses for structures.

Pointers

Although the Fortran 90 pointer differs in some respects from the C pointer, a pointer passed by Fortran 90 to a C function looks and acts the same as it does in C. The only precaution is that, when the pointer is to an array (which will almost always be the case), the two languages store and access arrays differently; see “Arrays”.

Allocatable arrays may be passed from Fortran 90 to C like any other array, with the precaution about array differences between the two languages. Strings (an array of characters in C) are a different matter; see “C strings” for information about passing strings from Fortran 90 to C.

Printable version
Privacy statement Using this site means you accept its terms Feedback to webmaster
© Hewlett-Packard Development Company, L.P.