Free format and fixed format
A COBOL source file has a layout, not just a sequence of lines. The original layout is called fixed format: the first columns of every line belong to the compiler, not to the program, because the language was written on 80-column punched cards and the columns were the card. Free format is the modern one: the whole line is program text and a column number means nothing.
GnuCOBOL reads a source as fixed format unless a flag says otherwise. A free-format file given to it without a flag still builds, because the compiler recognises the layout and reports the guess in a note. This course does not rely on the guess: every build command passes -free. The lesson shows both layouts, the same program in each, what the columns do, and the two ways a wrong format costs an afternoon.
Free format first
The program is the one from the first lesson, with a field printed next to a label. In free format the indentation is for the reader: the compiler takes the line as one string of words and stops at the period.
IDENTIFICATION DIVISION.
PROGRAM-ID. FREEFORM.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 AMOUNT PIC 9(5) VALUE 12345.
PROCEDURE DIVISION.
DISPLAY "TOTAL: " AMOUNT.
STOP RUN.
cobc -x -free -Wall -o freefree free.cob
./freefree
TOTAL: 12345
Built without the flag, the same file is read as fixed format first, the compiler notices that the result is not fixed format, and it says so before it builds:
cobc -x -Wall -o freefixed free.cob
./freefixed
free.cob:1: note: free format detected
1 | IDENTIFICATION DIVISION.
2 | PROGRAM-ID. FREEFORM.
3 | DATA DIVISION.
TOTAL: 12345
Forcing the other layout makes the file unreadable, and the message names the column that caused it: the first character of every line, which fixed format reserves.
cobc -x -fixed -Wall -o forced free.cob
free.cob:1: error: invalid indicator 'F' at column 7
1 > IDENTIFICATION DIVISION.
2 | PROGRAM-ID. FREEFORM.
3 | DATA DIVISION.
The file then gets one error per line, each naming the character that sits in column 7, until the last line reports error: PROGRAM-ID header missing: with the first column consumed by the layout, no line begins a division any more.
The fixed-format line
A fixed-format line is read as four zones, and the compiler uses the position of a word, not the word alone:
- 1 to 6: the sequence area. A card deck was sorted by these numbers, so a line could be put back in place. Nothing in them reaches the program, and any character is allowed.
- 7: the indicator area. A space is an ordinary line, * makes the whole line a comment, - continues the previous line, and / ejects the page of a printed listing.
- 8 to 11: area A. Division headers, section headers, paragraph names and the level 01 and 77 declarations belong here, which is why they start in the first text column.
- 12 to 72: area B. Statements and the operands below a declaration go here, which is why a statement is indented against the paragraph name above it.
Column 72 ends the line. A character in column 73 does not exist for the compiler, and the next section shows what that costs.
The same program in fixed format
Here is the same program written to the layout, with sequence numbers in columns 1 to 6 and the text starting in column 8. It is built without -free, because fixed format is what cobc reads by default.
000100 IDENTIFICATION DIVISION.
000200 PROGRAM-ID. FIXED.
000300 DATA DIVISION.
000400 WORKING-STORAGE SECTION.
000500 01 AMOUNT PIC 9(5) VALUE 12345.
000600 PROCEDURE DIVISION.
000700 DISPLAY "TOTAL: " AMOUNT.
000800 STOP RUN.
cobc -x -Wall -o fixed fixed.cob
./fixed
TOTAL: 12345
Two files, one program, the same output. The fixed listing on this page carries sequence numbers in columns 1 to 6, so the check that builds every free-format program printed on a lesson page leaves it alone; it was built by hand with the command above, in fixed format, and the output is that build.
The columns are a convention rather than a cage in GnuCOBOL 3.2: this version accepts a statement written in area A and a paragraph name written in area B without a word of complaint, and the program runs the same. Other compilers are not so forgiving, and a fixed-format file that follows the rule reads the same everywhere, so write to the rule when you edit one.
Past column 72
The trap of fixed format is that the cut is silent. This program counts with two statements on one line: the DISPLAY ends in area B, and the ADD after it begins in column 76. The build reports nothing, because nothing is wrong with the text the compiler can see.
000100 IDENTIFICATION DIVISION.
000200 PROGRAM-ID. PAST72.
000300 DATA DIVISION.
000400 WORKING-STORAGE SECTION.
000500 01 N PIC 9 VALUE 0.
000600 PROCEDURE DIVISION.
000700 DISPLAY "ONE" ADD 1 TO N.
000800 DISPLAY N.
000900 STOP RUN.
cobc -x -Wall -o past72 past72.cob
./past72
ONE
0
N is still 0: the statement that would have made it 1 was never read. The program is valid, the output looks reasonable, and the only evidence is a total that is one short.
When the cut lands inside a literal, the build stops instead, and the snippet in the message is cut at the same column as the compiler saw:
000100 IDENTIFICATION DIVISION.
000200 PROGRAM-ID. TRUNC.
000300 PROCEDURE DIVISION.
000400 DISPLAY "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuv".
000500 STOP RUN.
cobc -x -Wall -o trunc trunc.cob
trunc.cob:5: error: continuation character expected
3 | 000300 PROCEDURE DIVISION.
4 | 000400 DISPLAY "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopq..
5 > 000500 STOP RUN.
6 | <EOF>
trunc.cob:4: error: invalid literal: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567 ...'
2 | 000200 PROGRAM-ID. TRUNC.
3 | 000300 PROCEDURE DIVISION.
4 > 000400 DISPLAY "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopq..
5 | 000500 STOP RUN.
6 | <EOF>
trunc.cob:4: note: missing terminating " character
trunc.cob:4: warning: optional period used [-Wdialect]
The closing quote sat in column 74, the compiler saw a literal that ran to the end of the line, and it asked for a continuation character in column 7 of the next line. The .. in the snippet is the compiler's own mark for text it cut off.
Column 7
The indicator is the one column of fixed format a reader still uses. A star in column 7 comments the whole line, and *> starts a comment wherever it appears, which is the form free format also has:
000100 IDENTIFICATION DIVISION.
000200 PROGRAM-ID. COM.
000300 PROCEDURE DIVISION.
000400* the star in column 7 makes this line a comment
000500 DISPLAY "one".
000600 DISPLAY "two". *> inline
000700 STOP RUN.
cobc -x -Wall -o com com.cob
./com
one
two
A hyphen in column 7 joins the line to the one before, so a statement can be split. It works, and the compiler marks it as a dialect feature because the split is not what a free-format reader expects:
000100 IDENTIFICATION DIVISION.
000200 PROGRAM-ID. CONT2.
000300 DATA DIVISION.
000400 WORKING-STORAGE SECTION.
000500 01 N PIC 9(3) VALUE 42.
000600 PROCEDURE DIVISION.
000700 DISPLAY
000800- "N = " N.
000900 STOP RUN.
cobc -x -Wall -o cont2 cont2.cob
./cont2
cont2.cob:8: warning: continuation of COBOL words used [-Wdialect]
6 | 000600 PROCEDURE DIVISION.
7 | 000700 DISPLAY
8 > 000800- "N = " N.
9 | 000900 STOP RUN.
10 | <EOF>
N = 042
In free format none of this is needed: a statement may end in the middle of a line and continue on the next one, because the period, not the line, ends the sentence.
Which format to use
Three rules cover almost every file:
- New code is free format, and every build command passes -free. The lesson on cobc shows where the flag sits in the command; the rest of the course never leaves it out.
- An existing fixed-format file stays fixed. Keep the sequence numbers and the columns, do not reformat it, and build it without -free, because a file that already works is worth more than a tidy one.
- A file is one format or the other. A free-format line in a fixed-format file loses its first seven characters, and a fixed-format line in a free-format file starts with a sequence number that the compiler reads as the first word of a statement.
The choice is per file, not per program, and the first line of a file is not marked: the format lives in the build command, so a command without the flag is a decision to read fixed format, whatever the file holds.
Exercises
- Build the free-format program of this lesson with -fixed. Which column does the first message name, and which character does it quote?
- Move the ADD 1 TO N. of the past72 program so it starts in column 72. What does the program print now, and what does the build say?
- Put a * in column 1 of a free-format program and build it. Read the message: which line does it point at, and what does it say the compiler did not expect?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Which source layout does
cobcread when no layout flag is given?Answer
Fixed format
The default is fixed format; a free-format file builds only because the compiler detects the layout and reports the guess in a note.
-
What is kept in columns 1 to 6 of a fixed-format line?
Answer
The sequence number, which the compiler ignores
The sequence area held the card number, so a shuffled deck could be sorted again; nothing in it reaches the program.
-
What does the
-freeflag tellcobc?Answer
Read the whole line as program text, with no column zones
Free format has no sequence area and no indicator area: indentation is for the reader, and only the period ends a sentence.
-
Where does the program text of a fixed-format line end?
Answer
Column 72
The card had 80 columns, but 73 to 80 were left out of the program text area, and cobc still cuts the line at 72.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q1. DATA DIVISION. WORKING-STORAGE SECTION. 01 N PIC 9(3) VALUE 48. PROCEDURE DIVISION. DISPLAY "N = " N. STOP RUN.Answer
N = 048
In free format a statement may be split across lines: the DISPLAY and its operand are one sentence, and PIC 9(3) prints three digits.
-
What is the first line of the compiler output for this program?
IDENTIFICATION DIVISION. PROGRAM-ID. Q2. PROCEDURE DIVISION. * a star in column 1 DISPLAY "one". STOP RUN.Answer
error: syntax error, unexpected *
A star comments a line only in column 7 of fixed format; in free format it is a word where a statement should start, and the star itself is the unexpected token.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q3. PROCEDURE DIVISION. DISPLAY "one". *> DISPLAY "two". STOP RUN.Answer
one
*> starts a comment wherever it appears, so the second DISPLAY is not part of the program.
-
What is the first line of the compiler output for this program?
IDENTIFICATION DIVISION. PROGRAM-ID. Q4. PROCEDURE DIVISION. DISPLAY "AB STOP RUN.Answer
error: invalid literal: 'AB ...'
A literal must be closed on its own line in free format too, and the note under the error says the terminating quote is missing.
-
A fixed-format statement begins in column 76. What does the build do?
Answer
Nothing: the statement is past column 72 and is not read
The text area ends at column 72, so a statement written past it disappears without a message and the program runs without it.
-
What happens when a free-format source is built with
-fixed?Answer
It stops at the first line:
error: invalid indicator 'F' at column 7The flag removes the guess, so column 7 of the first line is read as the indicator area, and a letter is not an indicator.
The code on this page was built with cobc (GnuCOBOL) 3.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev