COBOL by Example: Data Types
Understanding COBOL data type definition using Picture (PIC) clauses instead of traditional type keywords, implementing numeric fields with 9 for digits, alphabetic fields with A for letters, and alphanumeric fields with X for any character, defining fixed-width variables with precise memory control, and using parenthetical notation for field length specification.
Code
IDENTIFICATION DIVISION.
PROGRAM-ID. DATATYPES.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 NUM-VAR PIC 9(3) VALUE 123.
01 ALPHA-VAR PIC A(5) VALUE "HELLO".
01 ALPHANUM PIC X(5) VALUE "A1B2C".
PROCEDURE DIVISION.
DISPLAY "Numeric: " NUM-VAR.
DISPLAY "Alphabetic: " ALPHA-VAR.
DISPLAY "Alphanumeric: " ALPHANUM.
STOP RUN.Explanation
COBOL does not use standard types like int or string found in modern languages. Instead, it uses a Picture (PIC) string to define the exact layout of data in memory. This gives the programmer precise control over the size and format of every variable, which is essential for record-based processing and ensuring data compatibility across different systems.
The three main characters used in PIC strings are: 9 represents a numeric digit (0-9), A represents an alphabetic character (A-Z and space), and X represents an alphanumeric character (any valid character including digits, letters, and special symbols).
The size of the variable is determined by the number of characters in the PIC string. You can repeat the character (e.g., PIC 999) or use parentheses for brevity (e.g., PIC 9(3)). This example shows how COBOL variables are fixed-width; if you store a shorter string in a larger variable, it will be padded with spaces.

