The COBOL character set consists of the following characters:
All COBOL programs must be written using only these characters. Most modern COBOL compilers also support additional characters, but for maximum portability, it's best to stick with the standard character set.
Traditionally, COBOL used a fixed column format with specific areas for different parts of the code. While modern COBOL can use free format, understanding the traditional format is important for maintaining legacy code.
Columns | Area | Usage |
---|---|---|
1-6 | Sequence Number Area | Optional numbering (historically used for card decks) |
7 | Indicator Area | Special characters: * (comment), - (continuation), / (form feed), D (debugging) |
8-11 | Area A | Division names, section names, paragraph names, level numbers 01 and 77, FD, SD |
12-72 | Area B | Statements, clauses, level numbers 02-49, qualifying names |
73-80 | Identification Area | Optional program identification (historically used) |
12345678910111213141516000100 IDENTIFICATION DIVISION. 000200 PROGRAM-ID. EXAMPLE. 000300 ENVIRONMENT DIVISION. 000400 DATA DIVISION. 000500 WORKING-STORAGE SECTION. 000600 01 CUSTOMER-RECORD. 000700 05 CUSTOMER-ID PIC 9(5). 000800 05 CUSTOMER-NAME PIC X(20). 000900 05 CUSTOMER-ADDRESS. 001000 10 STREET PIC X(20). 001100 10 CITY PIC X(15). 001200 10 STATE PIC XX. 001300 10 ZIP-CODE PIC 9(5). 001400 PROCEDURE DIVISION. 001500 DISPLAY "COBOL FORMAT EXAMPLE". 001600 STOP RUN.
In this example, notice how divisions, sections, and level 01 items start in Area A (column 8), while subordinate items are indented into Area B (starting at column 12).
Modern COBOL (2002 and later) also supports free format coding, which removes column restrictions:
1234567891011identification division. program-id. FreeFormat. environment division. data division. working-storage section. 01 customer-record. 05 customer-id pic 9(5). 05 customer-name pic x(20). procedure division. display "Free format COBOL example". stop run.
COBOL has a set of reserved words that have special meaning to the compiler and cannot be used as user-defined names.
Reserved words include:
There are over 500 reserved words in modern COBOL.
COBOL statements follow a structured format that makes the language readable and English-like.
123456789101112131415* Simple statement: MOVE CUSTOMER-NAME TO OUTPUT-NAME. * Compound statement: ADD ITEM-PRICE, TAX-AMOUNT GIVING TOTAL-PRICE ROUNDED. * Conditional statement: IF BALANCE IS GREATER THAN 1000 DISPLAY "Premium Customer" ELSE DISPLAY "Regular Customer" END-IF. * Performing a paragraph: PERFORM CALCULATE-TOTALS.
All COBOL statements are terminated by either a period, or in modern COBOL, by scope terminators like END-IF, END-PERFORM, etc. However, excessive use of periods can lead to logic errors, so modern COBOL programming style minimizes their use except where required by the language.
There are several ways to add comments in COBOL:
12345678* This is a comment line (asterisk in column 7) / This is also a comment line (slash in column 7) MOVE AMOUNT TO TOTAL-FIELD *> This is an inline comment *> This is a modern floating comment style DISPLAY "Result: " TOTAL-FIELD.
When a COBOL statement is too long for one line, it can be continued on the next line:
123456789000100 MOVE "This is a very long literal string that needs to be " 000200- "continued on the next line" TO LONG-MESSAGE. 000300 000400 IF CUSTOMER-STATUS = "A" AND ACCOUNT-BALANCE > 1000 AND 000500- CREDIT-RATING = "GOOD" 000600 PERFORM PREFERRED-CUSTOMER-ROUTINE 000700 ELSE 000800 PERFORM REGULAR-CUSTOMER-ROUTINE 000900 END-IF.
Well-commented COBOL code is essential for maintainability, especially since COBOL programs often have long lifespans and may be maintained by different programmers over decades.
Look at the following COBOL code and identify formatting issues:
12345678910111213141516IDENTIFICATION DIVISION. PROGRAM-ID. BADFORMAT. ENVIRONMENT DIVISION. DATA DIVISION. WORKING-STORAGE SECTION. 01 TOTAL-AMOUNT PIC 9(5)V99. 01 QUANTITY PIC 999. 01 price pic 999v99. PROCEDURE DIVISION. DISPLAY "Enter quantity: " ACCEPT QUANTITY DISPLAY "Enter unit price: " ACCEPT price MULTIPLY QUANTITY BY price GIVING TOTAL-AMOUNT DISPLAY "Total amount: $" TOTAL-AMOUNT STOP RUN.
Identify at least 5 formatting issues in the code above according to standard COBOL conventions.
COBOL's column structure dates back to its creation in the era of punch cards, where each card had 80 columns. The structure was designed to make programs readable and to accommodate the technology of the time. Modern COBOL allows free-format coding, but the traditional format is still used in legacy code and for readability.
Fixed format COBOL requires specific elements to appear in certain columns (Area A, Area B, etc.), while free format COBOL removes these restrictions. Free format was introduced in the COBOL 2002 standard, allowing more flexible coding styles similar to modern programming languages. Free format is indicated by >> SOURCE FORMAT IS FREE in the program or as a compiler option.
COBOL terms were traditionally written in uppercase because early computers and terminals only supported uppercase letters. While modern COBOL is case-insensitive and can be written in lowercase, uppercase is still commonly used for reserved words and user-defined names by convention, making it easier to distinguish between language elements and improving readability.
Each COBOL compiler provides a list of reserved words in its documentation. The list varies slightly between compiler implementations, but the core reserved words are consistent. Most modern COBOL editors and IDEs provide syntax highlighting that identifies reserved words. When in doubt, choose a different name for your variables and procedures to avoid potential conflicts.