C Is Not a Low-level Language

Re: “Fortran COMMON is another thing that isn’t exactly like C extern”, but BIND(C) clause indeed makes it like a C extern to a struct datatype.

#include <stdio.h>

typedef struct {
   int n;
} foo_c;
extern foo_c foo;

// Fortran function
void Fsub();

int main()
{
   Fsub();
   printf("In C main: foo.n = %d\n", foo.n);
   return 0;
}
subroutine Fsub() bind(C, name="Fsub")
   use, intrinsic :: iso_c_binding, only : c_int
   integer(c_int) :: n
   common / foo / n
   bind(C, name="foo") :: / foo /
   n = 42
   return 
end subroutine 
C:\temp>gfortran -ffree-form c.c s.f -o c.exe
cc1.exe: warning: command-line option '-ffree-form' is valid for Fortran but not for C

C:\temp>c.exe
In C main: foo.n = 42
1 Like