A COMMON statement defines one or more contiguous areas, or blocks, of storage shared among separate subprograms. COMMON statements also define the order in which variables, arrays, and records are stored in each common block.
A symbolic name identifies each block. However, you can omit a symbolic name for one block in a program unit. The block without a name is known as the blank common block.
The COMMON statement takes the following form:
COMMON [/[cb]/] nlist[[,] /[cb] /nlist] . . .
Any common block name, blank or otherwise, can appear more than once in one or more COMMON statements in a program unit. The list following each successive appearance of the same common block name is treated as a continuation of the list for the block associated with that name.
You can use array declarators in the COMMON statement to define arrays.
A common block can have the same name as a variable, array, record, structure, or field. However, in a program with one or more program units, a common block cannot have the same name as a function, subroutine, or entry name in the executable program.
When common blocks from different program units have the same name, they share the same storage area when the units are combined into an executable program.
Entities are assigned storage in common blocks on a one-for-one basis. So, the entities assigned by a COMMON statement in one program unit should agree with the data type of entities placed in a common block by another program unit. For example, consider a program unit containing the following statement:
COMMON CENTS
Consider another program unit containing the following statements:
INTEGER*2 MONEY COMMON MONEY
When these program units are combined into an executable program,
incorrect results can occur if the 2-byte integer variable MONEY
is made to correspond to the lower-addressed two bytes of the real
variable CENTS.
In the following example, the COMMON statement in the main program puts HEAT and X in the blank common block, and KILO and Q in a named common block, BLK1.
The COMMON statement in the subroutine makes ALFA and BET share the same storage location as HEAT and X in the blank common block. It makes LIMA and R share the same storage location as KILO and Q in BLK1.
Main Program | Subprogram |
---|---|
COMMON HEAT,X /BLK1/KILO,Q
| SUBROUTINE FIGURE |
. . . | COMMON /BLK1/LIMA,R /
/ALFA,BET |
CALL FIGURE
| . . . |
. . . | RETURN |
END |