The ISO_C_BINDING Module

The ISO_C_BINDING module provides access to named constants that represent kind type parameters of data representations compatible with C types, the derived type C_PTR corresponding to any C data pointer type, the derived type C_FUNPTR corresponding to any C function pointer type, and four procedures.

Constants for use as Kind Type Parameters

Table 27 shows the interoperability between Fortran intrinsic types and C types. A Fortran intrinsic type with particular kind type parameter values is interoperable with a C type if the type and kind type parameter value are listed in the same row as that C type; if the type is character, interoperability also requires that the length type parameter be omitted or be specified by an initialization expression whose value is one. A combination of Fortran type and type parameters that is interoperable with a C type listed in the table is also interoperable with any unqualified C type that is compatible with the listed C type.

Table 27. Interoperable Fortran and C Types

Fortran Type Named Constant
(kind type parameter)
Value C Type




INTEGER C_SIGNED_CHAR 1 signed char

C_SHORT 2 short

C_INT 4 int

C_LONG 4 (with -q32)
8 (with -q64)
long

C_LONG_LONG 8 long long

C_SIZE_T 4 (with -q32)
8 (with -q64)
size_t

C_INTPTR_T 4 (with -q32)
8 (with -q64)
intptr_t

C_INTMAX_T 8 intmax_t

C_INT8_T 1 int8_t

C_INT16_T 2 int16_t

C_INT32_T 4 int32_t

C_INT64_T 8 int64_t

C_INT_LEAST8_T 1 int_least8_t

C_INT_LEAST16_T 2 int_least16_t

C_INT_LEAST32_T 4 int_least32_t

C_INT_LEAST64_T 8 int_least64_t

C_INT_FAST8_T 1 int_fast8_t

C_INT_FAST16_T 4 int_fast16_t

C_INT_FAST32_T 4 int_fast32_t

C_INT_FAST64_T 8 int_fast64_t




REAL C_FLOAT 4 float

C_DOUBLE 8 double

C_LONG_DOUBLE 16 long double

C_FLOAT_COMPLEX 4 float _Complex

C_DOUBLE_COMPLEX 8 double _Complex

C_LONG_DOUBLE_COMPLEX 8 long double _Complex




LOGICAL C_BOOL 1 _Bool




CHARACTER C_CHAR 1 char

For example, the type integer with a kind type parameter of C_SHORT is interoperable with the C type short or any C type derived (via typedef) from short.

Notes:

  1. The named constants in the ISO_C_BINDING module are of type INTEGER(4).

  2. In order for any Fortran COMPLEX entity to be interoperable with a corresponding C _Complex entity appearing in C code compiled with gcc, the Fortran code must be compiled with -qfloat=complexgcc.

  3. Fortran REAL(C_LONG_DOUBLE) and COMPLEX(C_LONG_DOUBLE_COMPLEX) entities are only interoperable with the corresponding C types when the C code is compiled with an option that enables 16-byte long doubles.

  4. Fortran integer entities with kind type parameter values of C_LONG_LONG, C_INT64_T, C_INT_LEAST64_T, C_INT_FAST64_T, and C_INTMAX_T are not interoperable with the corresponding C types when the C code is compiled with -qnolonglong.

Character Constants

The following character constants are provided for compatibility with some commonly used C characters that are represented using escape sequences:

Table 28. Fortran Named Constants and C Characters

Fortran Named Constant Definition C Character
C_NULL_CHAR null character '\0'
C_ALERT alert '\a'
C_BACKSPACE backspace '\b'
C_FORM_FEED form feed '\f'
C_NEW_LINE new line '\n'
C_CARRIAGE_RETURN carriage return '\r'
C_HORIZONTAL_TAB horizontal tab '\t'
C_VERTICAL_TAB vertical tab '\v'

Other Constants

The constant C_NULL_PTR is of type C_PTR; it has the value of a C null data pointer. The constant C_NULL_FUNPTR is of type C_FUNPTR; it has the value of a C null function pointer.

Types

The type C_PTR is interoperable with any C data pointer type. The type C_FUNPTR is interoperable with any C function pointer type. They are both derived types with private components.

Procedures

A C procedure argument is often defined in terms of a C address. The ISO_C_BINDING module provides the following procedures. The C_ASSOCIATED function is provided so that Fortran programs can compare C addresses. The C_F_POINTER subroutine provides a means of associating a Fortran pointer with the target of a C pointer. The C_FUNLOC and C_LOC functions are provided so that Fortran applications can determine the appropriate value to use with C facilities.

C_ASSOCIATED(C_PTR_1[, C_PTR_2])

Purpose

Indicates the association status of C_PTR_1, or whether C_PTR_1 and C_ PTR_2 are associated with the same entity.

Class

Inquiry function

Argument Type and Attributes

C_PTR_1
Required; scalar of type C_PTR or C_FUNPTR.

C_PTR_2
Optional; scalar of the same type as C_PTR_1.

Result Type and Attributes

Default logical

Result Value

C_F_POINTER(CPTR, FPTR [, SHAPE])

Purpose

Associates a data pointer with the target of a C pointer and specifies its shape.

Class

Subroutine

Argument Type and Attributes

CPTR
An INTENT(IN) argument; a scalar and of type C_PTR.

FPTR
An INTENT(OUT) argument that is a pointer.

SHAPE
An optional INTENT(IN) argument of type integer and rank one. If present, its size equals the rank of FPTR. SHAPE must be present if and only if FPTR is an array.

Rules

If the value of CPTR is the C address of an interoperable data entity, then:

Otherwise, the value of CPTR will be the result of a reference to C_LOC with a noninteroperable argument X. X (or its target) cannot have been deallocated or have become undefined due to the execution of a RETURN or END statement since the reference to C_LOC. FPTR is a nonpolymorphic, scalar pointer with the same type and type parameters as X. It becomes pointer-associated with X (or its target if X is a pointer).

C_FUNLOC(X)

Purpose

Returns the C address of a function pointer.

Class

Inquiry function

Argument Type and Attributes

X
required interoperable procedure

Result Type and Attributes

Scalar of type C_FUNPTR

Result Value

A value of type C_FUNPTR that represents the C address of the argument.

C_LOC(X)

Purpose

Returns the C address of the argument.

Class

Inquiry function

Argument Type and Attributes

X
required and can be one of the following:

Result Type and Attributes

Scalar of type C_PTR

Result Value

A value of type C_PTR that represents the C address of the argument. IBM Copyright 2003