💻 COBOL
COBOL Data Types and Variables
Beginner 🕑 20 min read
👁 4 views
Understanding COBOL Data Types
COBOL uses PICTURE (PIC) clauses to define the type and size of data items. Understanding data types is fundamental to COBOL programming.
Numeric Data Types
| PIC Clause | Description | Example |
|---|---|---|
| 9 | Numeric digit (0-9) | PIC 9(5) - 5 digits |
| S | Sign (+ or -) | PIC S9(5) - signed number |
| V | Implied decimal point | PIC 9(5)V99 - 5.2 format |
| P | Assumed decimal position | PIC 9(3)PP - multiply by 100 |
Alphanumeric Data Types
| PIC Clause | Description | Example |
|---|---|---|
| X | Any character | PIC X(20) - 20 characters |
| A | Alphabetic only | PIC A(10) - 10 letters |
Edited Data Types
Used for formatting output:
- Z - Zero suppression
- . - Decimal point
- , - Comma insertion
- $ - Currency symbol
- - - Minus sign for negative
Level Numbers
COBOL uses level numbers to create data hierarchies:
- 01 - Record level
- 02-49 - Group and elementary items
- 66 - RENAMES clause
- 77 - Independent elementary items
- 88 - Condition names
Code Example
DATA DIVISION.
WORKING-STORAGE SECTION.
* Numeric variables
01 WS-COUNT PIC 9(5) VALUE ZEROS.
01 WS-AMOUNT PIC S9(7)V99 VALUE ZEROS.
01 WS-RATE PIC 9V9(4) VALUE ZEROS.
* Alphanumeric variables
01 WS-NAME PIC X(30) VALUE SPACES.
01 WS-CODE PIC A(5) VALUE SPACES.
* Edited fields for display
01 WS-DISPLAY-AMT PIC $ZZZ,ZZ9.99.
01 WS-DISPLAY-DATE PIC 99/99/9999.
* Group item example
01 WS-EMPLOYEE-REC.
05 WS-EMP-ID PIC 9(6).
05 WS-EMP-NAME PIC X(30).
05 WS-EMP-SALARY PIC 9(7)V99.
05 WS-EMP-DEPT PIC X(10).
* Condition name example
01 WS-STATUS PIC X(1).
88 STATUS-ACTIVE VALUE "A".
88 STATUS-INACTIVE VALUE "I".
88 STATUS-PENDING VALUE "P".