| United States-English |
|
|
|
![]() |
Fortran 90 Compiler for HP-UX: Fortran 90 Programmer's Guide > Chapter 8 Calling C routines from HP Fortran 90Data types |
|
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
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
Table 8-3 Size differences after compiling with +autodbl
The following sections provide more detailed information about language differences for the following data types:
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. 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. 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:
Consider a program that consists of two source files:
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
Following is the C source file: Example 8-2 sqr_complex.c
Here are the command lines to compile, link, and execute the program, followed by the output from a sample run:
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. 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. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||