This chapter covers:
- Why invoke C code from Fortran?
- Interfacing C built-in types, structs, and functions from Fortran
- Writing a minimal Fortran TCP client and server
Pure Fortran is powerful for numerical and array-oriented computation that is ubiquitous in physical sciences and engineering. Howewer, there are quite a few things that aren’t possible in Fortran alone, but can be done in a low-level systems programming language such as C. These include reading and writing data to hardware devices, drawing graphics on the screen in real-time, or sending data over the internet. Interoperability with C allows a programmer to call C functions from Fortran programs. This is important for two reasons: First, enables the above-mentioned low-level functionality, and gives Fortran access to the C ecosystem of libraries. Second, C itself is interoperable with many popular programming languages today, such as Python, JavaScript, Go, or Rust. By using C as the interfacing language, Fortran programs can invoke code written in most other languages, and vice versa. In the real world, this allows Fortran code to be used within web servers, databases, and real-time graphics.