Fortran 2008/2018: what's new in ISO_C_BINDING?

Dear Fortraners,
in my gtk-fortran project, I am heavily using the intrinsic ISO_C_BINDING module, introduced in Fortran 2003, to access the GTK functions (written in C) from Fortran programs.
But it is still based on the Fortran 2003 standard (my reference is the book fortran 95/2003 explained). I know that knew features have been introduced since in the ISO_C_BINDING. Perhaps some of them could be interesting for my library ?
Is someone familiar with these new features ?

The one I find very convenient is that optional parameters can be used to interface with C routines that accept NULL when an argument is not needed.

I’ve used this to wrap the NLOPT library:

1 Like

Can this document be of any help:

1 Like

Thank you,
yes, page xiv, there is a summary of the new features in ISO_C_BINDING since Fortran 2008.

Another new feature I found interesting was the “ISO_Fortran_Binding.h” header file. Using the descriptors and routines it is possible to write C code which allocates Fortran entities (like allocatable arrays and pointers) in a “native” way. This way if you have a C library of routines, you can build an API that integrates tightly with Fortran. However, it requires some work and carefulness on the C side.

An example using the METIS library can be found here. This was over a year ago and the code worked with Intel Fortran compiler, but returned an error with gfortran.

1 Like

Others have pointed out references. In general, the initial F2003 version of C interop included features that corresponded to places where Fortran and C had matching capabilities and provided a way for Fortran code to “look like” C, allowing Fortran programmers access to a lot of C libraries. Examples included correspondences for intrinsic types, passing arguments by value, declaring and accessing global data, … The newer version, in F2018, builds on this by adding ways for C to “look like” Fortran and be able to take advantage of Fortran things like alloocatable and assumed-shape arrays, (through the use of a descriptor), and optional arguments.


Keep in mind that ISO_C_BINDING is just a module, and is not the whole of C interoperability features.

1 Like

As alluded to in other comments, this topic goes beyond the intrinsic module ISO_C_BINDING. What Fortran 2018 offers is enhanced interoperability with C. This work appears to have been motivated by the MPI community with their use of Fortran codes with C, C++.

Interested readers may want to review “Modern Fortran Explained Incorporating 2018” ( Chapter 21 in this book describes all the new facilities in Fortran 2018 re: interoperability with C.

18-007r1 document toward Fortran 2018 provides the following summary in its Introduction section:

A dummy data object can assume its rank from its effective argument. A dummy data object can assume
the type from its effective argument, without having the ability to perform type selection. An interoperable
procedure can have dummy arguments that are assumed-type and/or assumed-rank. An interoperable
procedure can have dummy data objects that are allocatable, assumed-shape, optional, or pointers. The
character length of a dummy data object of an interoperable procedure can be assumed. The argument
to C_LOC can be a noninteroperable array. The FPTR argument to C_F_POINTER can be a noninteroperable
array pointer. The argument to C_FUNLOC can be a noninteroperable procedure. The FPTR
argument to C_F_PROCPOINTER can be a noninteroperable procedure pointer. There is a new named
constant C_PTRDIFF_T to provide interoperability with the C type ptrdiff_t.
Additionally to ISO/IEC TS 29113:2012, a scalar actual argument can be associated with an assumedtype
assumed-size dummy argument, an assumed-rank dummy data object that is not associated with an
assumed-size array can be used as the argument to the function C_SIZEOF from the intrinsic module
ISO_C_BINDING, and the type argument to CFI_establish can have a positive value corresponding to
an interoperable C type.

1 Like