Sun Studio 12: Fortran Programming Guide

11.6 Labeled COMMON

Fortran labeled COMMON can be emulated in C by using a global struct.

Table 11–16 Emulating Labeled COMMON

Fortran COMMON Definition  

C "COMMON" Definition  


COMMON /BLOCK/ ALPHA,NUM
...

extern struct block {
  float alpha;
  int num;
};
extern struct block block_ ;
main ()
{
...
  block_.alpha = 32.;
  block_.num += 1;
...
}

Note that the external name established by the C routine must end in an underscore to link with the block created by the Fortran program. Note also that the C directive #pragma pack may be needed to get the same padding as with Fortran.

f95 aligns data in common blocks to at most 4-byte boundaries by default. To obtain the natural alignment for all data elements inside a common block and match the default structure alignment, use -aligncommon=16 when compiling the Fortran routines.