Self-Assessment Quiz: C-Fortran Interoperability

Twenty questions to confirm you can cross the language boundary safely before moving on to Python in Chapter 15. Aim for 16 or more. Answers and a topic map are at the end — try the whole quiz first.


Question 1

Which statement requests the intrinsic interoperability module? - A. use iso_c_binding_module - B. use, intrinsic :: iso_c_binding - C. import c_binding - D. #include <iso_c_binding>

Question 2

Which Fortran declaration is interoperable with a C double? - A. real :: x - B. double precision :: x - C. real(c_double) :: x - D. real(dp) :: x

Question 3

What does the bind(c) attribute primarily give a Fortran procedure? - A. Faster execution - B. A predictable, un-mangled C linkage name - C. Automatic parallelization - D. Protection from aliasing

Question 4

With bind(c, name="step_c"), the symbol the linker sees is: - A. __mod_MOD_step_c - B. step_c_ - C. step_c - D. STEP_C

Question 5

A C function takes a scalar double x by value. The matching Fortran dummy must be declared: - A. real(c_double), intent(in) :: x - B. real(c_double), value :: x - C. real(c_double), pointer :: x - D. real(c_double), target :: x

Question 6

If you forget the value attribute on a scalar argument that C passes by value, what happens? - A. A compile error - B. A link error - C. The program runs but produces garbage or crashes - D. Nothing — value is optional

Question 7

A C function takes const double *v (a pointer to an array). The matching Fortran dummy is: - A. real(c_double), value :: v(n) - B. real(c_double), intent(in) :: v(n) (by reference, no value) - C. type(c_ptr), value :: v - D. real(c_double), allocatable :: v(:)

Question 8

When you compile a program with a C main and a Fortran module, you should drive the final link with: - A. gcc, always - B. ld directly - C. gfortran, so libgfortran is linked in - D. either, with no extra flags

Question 9

Why can you not link a Fortran program unit and a C main() into one executable? - A. They use different calling conventions - B. Both claim the executable's single entry point (a duplicate main) - C. Fortran cannot be linked with C - D. The linker cannot mix object formats

Question 10

A type, bind(c) derived type may not contain: - A. integer(c_int) components - B. real(c_double) components - C. an allocatable or pointer component - D. more than three components

Question 11

Fortran stores a 2-D array in __ order; C stores it in ____ order. - A. row-major; column-major - B. column-major; row-major - C. column-major; column-major - D. row-major; row-major

Question 12

A shared 2-D array between C and Fortran comes out transposed. The best fix is to: - A. Transpose the array on every call - B. Agree a layout contract so the fast axis is contiguous on both sides - C. Switch Fortran to row-major with a compiler flag - D. Copy the array element by element

Question 13

To pass a Fortran string to a C function expecting a char *, you must: - A. Nothing special — Fortran strings are already C strings - B. Append c_null_char so the string is null-terminated - C. Reverse the characters - D. Convert each character to an integer

Question 14

Which iso_c_binding derived type is interoperable with a C void *? - A. c_void - B. c_ptr - C. c_address - D. c_null

Question 15

c_loc(a) requires a to have which attribute? - A. allocatable - B. value - C. target (or pointer) - D. intent(out)

Question 16

Which procedure turns a type(c_ptr) back into a usable Fortran array pointer? - A. c_loc - B. c_f_pointer - C. c_associated - D. transfer

Question 17

True or false: a default Fortran logical is guaranteed to be interoperable with C's _Bool.

Question 18

The interoperability module and bind(c) were standardized in: - A. FORTRAN 77 - B. Fortran 95 - C. Fortran 2003 - D. Fortran 2018

Question 19

code/example-02-fmean.f90 computes sum(v) / real(n, c_double). Why is real(n, c_double) used rather than sum(v) / n? - A. It is required — integer n cannot divide a real - B. To make the division explicitly happen in c_double and document intent - C. Because sum(v) returns an integer - D. It changes the result from wrong to right

Question 20

What does this print, given a C driver printf("%d\n", twice(21)); where the Fortran bind(c) function twice has integer(c_int), value :: n and returns 2*n? - A. 21 - B. 42 - C. garbage - D. a link error


Answer Key

Q Ans Why
1 B use, intrinsic :: iso_c_binding requests the intrinsic module.
2 C real(c_double) is the interoperable kind for C double.
3 B bind(c) gives a clean, un-mangled C linkage name.
4 C name="step_c" makes the symbol exactly step_c.
5 B By-value scalars from C need the value attribute.
6 C It compiles but passes an address where a value is expected — silent garbage.
7 B A C array pointer matches a by-reference (no value) explicit-shape dummy.
8 C Link with gfortran to include the Fortran runtime libgfortran.
9 B A Fortran program and C main both claim the single entry point.
10 C Interoperable types forbid allocatable/pointer components.
11 B Fortran is column-major; C is row-major.
12 B Agree a layout contract; a per-call transpose is wasteful.
13 B C strings are null-terminated; append c_null_char.
14 B c_ptr is interoperable with void *.
15 C c_loc needs a stable address, so target (or pointer).
16 B c_f_pointer(cp, fp[, shape]) recovers a Fortran pointer.
17 False Default logical is not interoperable; use logical(c_bool).
18 C iso_c_binding and bind(c) arrived in Fortran 2003.
19 B The conversion makes the division happen in c_double and states intent (mixed-mode would promote n anyway; the real trap is integer/integer division).
20 B twice(21) = 2*21 = 42; value makes the by-value int match.

Topics to review by question

  • Q1–2, 14–16 → §14.1 (the module, C-interoperable kinds, c_ptr/c_loc/c_f_pointer).
  • Q3–4, 18 → §14.2 (bind(c), linkage, name mangling, history).
  • Q5–6, 20 → §14.3 (calling C from Fortran; the value attribute).
  • Q7–9, 19 → §14.4 (calling Fortran from C; linking; the arithmetic).
  • Q10–13, 17 → §14.5 (arrays, the column-major trap, strings, structs, c_bool).

Scored below 16? Re-read the flagged sections — the by-value/by-reference rule (Q5–7) and the column-major/row-major trap (Q11–12) are the two that cause the most real-world grief, so make sure those are solid before Chapter 15.