Fortran

Guide To Learn

Intermezzo: Matching compatible C and Fortran data types

Now that you’ve got a taste of interfacing C-structs with functions from Fortran, it’s a good time to go into more detail on compatible C and Fortran data types. I mentioned earlier that C types are different from Fortran types: int is not exactly integer, and float is not exactly real. What matters, however, is that we have a reliable way to match the compatible data types between C and Fortran such that their internal representation in memory is the same. To that end, a wide array of C-conforming type kind parameters is available out of the box in the iso_c_binding module. Table 11.1 lists them all.

Table 11.1 Type kind parameters available in iso_c_binding and their corresponding types in C (continued)

Base typeFortran type kind parameterC type
integerc_intint
 c_int8_tint8_t
 c_int16_tint16_t
 c_int32_tint32_t
 c_int64_tint64_t
 c_int_least8_tint_least8_t
 c_int_least16_tint_least16_t
 c_int_least32_tint_least32_t
 c_int_least64_tint_least64_t
 c_int_fast8_tint_fast8_t
 c_int_fast16_tint_fast16_t
 c_int_fast32_tint_fast32_t
 c_int_fast64_tint_fast64_t
 c_intmax_tintmax_t
 c_intptr_tintptr_t
 c_size_tsize_tssize_t
 c_shortshort int
 c_longlong int
 c_long_longlong long int
 c_signed_charsigned charunsigned char
realc_floatfloat
 c_doubledouble
 c_long_doublelong double
complexc_float_complexfloat complex
 c_double_complexdouble complex
 c_long_double_complexlong double complex
logicalc_boolbool
characterc_charchar

As you can see from table 11.1, C has many built-in types, and Fortran’s iso_c_binding provides type kind parameters to match them.

The C types bool and complex were introduced in the C99 revision to the C Standard. They may appear as _Bool and _Complex, respectively, in C code that was meant to be compliant with earlier C standards. You can refer back to this table as you write Fortran interfaces with other C code. For now, you don’t need to know any more details about many of these C types and their Fortran siblings. Let’s go back to our TCP server, where we’ll encounter a few new C types, such as int64_t and size_t.

C type not supported?

If a C type isn’t supported by your Fortran or C compiler, either you’ll get a compile-time error or the type kind parameter imported from iso_c_binding will have a negative value.

Intermezzo: Matching compatible C and Fortran data types

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top