C interoperability

Contents
bind(C) procedures
Matching C types
C pointers round-trip
C strings

bind(C) procedures

The iso_c_binding module connects Fortran with the C language. A procedure marked with bind(C) uses the C calling convention and a fixed external name.

Start with one small interoperable function. Keep dummy arguments simple and interoperable, then test the Fortran side before wiring the C side.

module c_api use iso_c_binding implicit none contains function add_ints(a, b) bind(C, name="add_ints") result(c) implicit none integer(c_int), intent(in), value :: a integer(c_int), intent(in), value :: b integer(c_int) :: c c = a + b end function add_ints end module c_api

Matching C types

Fortran kinds with C names match common C types: c_int for C int and c_double for C double and c_char for C char.

Declare interoperable variables with those kinds and keep all other logic in normal Fortran kinds.

program c_types use iso_c_binding implicit none integer(c_int) :: n real(c_double) :: d n = 10 d = 1.5 print *, "n =", n, "d =", d end program c_types

C pointers round-trip

A C address travels as type(c_ptr). Fortran creates it with c_loc, tests it with c_associated, and converts it back with c_f_pointer.

The null address is c_null_ptr. Always test before converting, because converting a null address is an error.

program ptr_demo use iso_c_binding implicit none integer, target :: val type(c_ptr) :: cp integer, pointer :: fp val = 42 cp = c_loc(val) if (c_associated(cp)) then call c_f_pointer(cp, fp) print *, "fp =", fp end if end program ptr_demo

C strings

A C string is an array of c_char elements ended by a zero character. Fortran builds it element by element and adds achar(0) as the terminator.

Keep the buffer one element longer than the text so the terminator always fits.

program c_string use iso_c_binding implicit none character(kind=c_char) :: buf(4) buf(1) = "H" buf(2) = "i" buf(3) = "!" buf(4) = achar(0) print *, buf(1), buf(2), buf(3) end program c_string

Next: From fixed form to free form

Article author: Arthur Isaev

Related articles
Fortran
Fortran course: zero to hero in 50 lessons
What is Fortran and where it runs
Install gfortran
Your first program
Program structure
Types: integer real complex logical character
Kind parameters
Operators
Conditions
DO loops
Arrays
Array sections
Character strings
Formatted IO
Files
Subroutines and functions
Modules
USE with ONLY
INTENT
OPTIONAL and PRESENT
Explicit interfaces
Derived types
Type-bound procedures
Object orientation
Operator overloading
Finalization
Allocatable arrays
Pointers in Fortran
DO CONCURRENT
Coarrays
OpenMP basics
Namelists
IEEE arithmetic
Intrinsic matrix math
Random numbers
String tools
Command-line arguments
Environment variables
error stop
Testing
Build with gfortran and make
Debugging
C interoperability
From fixed form to free form
Numeric models
Performance
Modern Fortran tour: 2008, 2018, 2023
Capstone: stats tool
Capstone: 1D heat equation
Style guide
Hero roadmap: the whole course on one page
Install gfortran on Windows 11
Install gfortran on Ubuntu
Install gfortran on Rocky Linux
Install gfortran on macOS
Install gfortran on FreeBSD
Fortran in Docker
Banner image

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: