TExcelTAD
Home › COBOL › COBOL Coding Format

COBOL Coding Format

COBOL was designed when programs were punched onto 80-column cards, one line per card. That history still shapes fixed-format source, which is the default for GnuCOBOL and for most mainframe code.

The columns

Columns Name Use
1–6 Sequence area Line numbers (once used to put a dropped deck of cards back in order). Ignored by the compiler
7 Indicator area * comment line, / comment + new page in listings, - continuation, D debugging line
8–11 Area A Division, section and paragraph names, FD, level numbers 01 and 77
12–72 Area B Statements, clauses, and level numbers other than 01/77
73–80 Identification area Ignored (was used for the program name on each card)
text
         1         2         3         4         5         6         7
1234567890123456789012345678901234567890123456789012345678901234567890123
000100 IDENTIFICATION DIVISION.
000200*This whole line is a comment because of the * in column 7
000300 PROCEDURE DIVISION.
000400     DISPLAY "Area B starts in column 12".

Anything after column 72 is silently ignored. That is a classic source of bugs: a long literal that runs past column 72 is cut off.

Comments

There are two kinds of comment:

  • A * in column 7 makes the whole line a comment.
  • *> starts an inline comment anywhere on the line (COBOL 2002 and later, supported by GnuCOBOL).
cobol · try it yourself
000100 IDENTIFICATION DIVISION.
000200 PROGRAM-ID. COMMENTS.
000300*Full-line comment: the asterisk is in column 7
000400 PROCEDURE DIVISION.
000500     DISPLAY "Sequence numbers are ignored"     *> inline comment
000600*    DISPLAY "This line is commented out"
000700     DISPLAY "Area B code starts in column 12"
000800     STOP RUN.
Sequence numbers are ignored
Area B code starts in column 12

Case does not matter (except in literals)

COBOL keywords and names are case-insensitive. DISPLAY, display and Display are the same word, and so are WS-TOTAL and ws-total. Text inside quotes is kept exactly. Traditional code is all upper case; many modern teams write lower case.

cobol · try it yourself
       identification division.
       program-id. lower-case.
       data division.
       working-storage section.
       01  ws-city        pic x(6) value "Oslo".
       procedure division.
           display "City: " WS-CITY
           Display "Same item: " Ws-City
           stop run.
City: Oslo
Same item: Oslo

Names and periods

  • User-defined names may contain letters, digits and hyphens, up to 30 characters, and must not start or end with a hyphen: CUSTOMER-NAME, TOTAL-2024.
  • A name cannot be a reserved word (DATA, COUNT, FILE, RECORD, TIME… there are several hundred). Prefixes such as WS- avoid clashes.
  • Division, section and paragraph headers end with a period. Data descriptions end with a period.

Continuing long literals

Long text can be split across lines. The simplest way is to place literals side by side in a DISPLAY, or to join them with & (concatenation of literals):

cobol · try it yourself
       IDENTIFICATION DIVISION.
       PROGRAM-ID. LONG-TEXT.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  WS-MOTTO       PIC X(60) VALUE
           "Data processing that adds up to the cent, " &
           "every night.".
       PROCEDURE DIVISION.
           DISPLAY WS-MOTTO
           DISPLAY "This message is split "
                   "over two source lines."
           STOP RUN.
Data processing that adds up to the cent, every night.
This message is split over two source lines.

Note: The traditional way to continue a literal is a - in column 7 on the next line, with the continued text starting with another quote. It is harder to read and easy to get wrong, so prefer &.

Free format

GnuCOBOL (cobc -free) and COBOL 2002 also accept free-format source without column rules, starting with the directive >>SOURCE FORMAT IS FREE. Lines can be longer and code can start in column 1. This course uses fixed format because that is what you will meet in real systems.

cobol
>>SOURCE FORMAT IS FREE
identification division.
program-id. free-style.
procedure division.
display "No column rules here".
stop run.

Test yourself

What does a * in column 7 do?

  1. Multiplies the line
  2. Makes the whole line a comment
  3. Continues the previous line
  4. Marks a debugging line

Where must a paragraph name such as MAIN-LOGIC. start in fixed format?

  1. Area A (columns 8–11)
  2. Area B (columns 12–72)
  3. Column 1
  4. Anywhere

What happens to source text after column 72?

  1. It causes a compile error
  2. It is joined to the next line
  3. It is ignored
  4. It is treated as a comment only if it starts with *>