MainframeMaster

COBOL Data Types Concepts

Data types in COBOL define how data is stored, interpreted, and manipulated within programs. Understanding data types is fundamental to COBOL programming as it determines memory allocation, arithmetic operations, and data representation. COBOL provides various data types including numeric, character, binary, and specialized types for different business requirements.

Understanding Data Types

Data types in COBOL are defined using PICTURE clauses that specify the format, size, and characteristics of data items. Each data type has specific storage requirements, arithmetic capabilities, and display formats. Proper understanding of data types ensures efficient memory usage and correct data processing.

Numeric Data Types

1. DISPLAY Format (PIC 9)

DISPLAY format is the default numeric format in COBOL, storing numbers as character strings. This format is human-readable and suitable for input/output operations but less efficient for arithmetic calculations.

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
DATA DIVISION. WORKING-STORAGE SECTION. 01 DISPLAY-NUMBERS. 05 INTEGER-VALUE PIC 9(5) VALUE 12345. 05 DECIMAL-VALUE PIC 9(5)V99 VALUE 123.45. 05 SIGNED-VALUE PIC S9(4) VALUE -1234. 05 ZERO-FILLED PIC 9(6) VALUE ZEROS. PROCEDURE DIVISION. DISPLAY 'DISPLAY Format Examples:' DISPLAY 'Integer: ' INTEGER-VALUE DISPLAY 'Decimal: ' DECIMAL-VALUE DISPLAY 'Signed: ' SIGNED-VALUE DISPLAY 'Zero-filled: ' ZERO-FILLED

DISPLAY format stores numbers as character strings, making them human-readable but requiring conversion for arithmetic operations. The V indicates decimal point position, and S indicates signed numbers.

2. BINARY Format (COMP)

BINARY format stores numbers in binary representation, providing efficient arithmetic operations and compact storage. This format is ideal for calculations and counters.

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
DATA DIVISION. WORKING-STORAGE SECTION. 01 BINARY-NUMBERS. 05 COUNTER PIC 9(5) COMP VALUE 1000. 05 CALCULATION PIC 9(8)V99 COMP VALUE 12345.67. 05 INDEX-VALUE PIC 9(3) COMP VALUE 50. PROCEDURE DIVISION. DISPLAY 'BINARY Format Examples:' DISPLAY 'Counter: ' COUNTER DISPLAY 'Calculation: ' CALCULATION DISPLAY 'Index: ' INDEX-VALUE *> Efficient arithmetic operations ADD 100 TO COUNTER MULTIPLY 2 BY CALCULATION

BINARY format provides efficient storage and arithmetic operations. Numbers are stored in binary representation, making calculations faster but requiring conversion for display.

3. PACKED DECIMAL Format (COMP-3)

PACKED DECIMAL format stores decimal numbers efficiently while maintaining decimal precision. This format is commonly used for financial calculations where precision is critical.

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
DATA DIVISION. WORKING-STORAGE SECTION. 01 PACKED-DECIMALS. 05 AMOUNT PIC 9(7)V99 COMP-3 VALUE 12345.67. 05 BALANCE PIC S9(8)V99 COMP-3 VALUE -5000.00. 05 RATE PIC 9(3)V99 COMP-3 VALUE 5.25. PROCEDURE DIVISION. DISPLAY 'PACKED DECIMAL Format Examples:' DISPLAY 'Amount: ' AMOUNT DISPLAY 'Balance: ' BALANCE DISPLAY 'Rate: ' RATE *> Precise decimal arithmetic COMPUTE AMOUNT = AMOUNT * RATE / 100

PACKED DECIMAL format maintains decimal precision while providing efficient storage. Each digit is stored in half a byte, with the sign stored in the rightmost half-byte.

Character Data Types

1. Alphanumeric (PIC X)

Alphanumeric data type stores character data including letters, numbers, spaces, and special characters. This is the most flexible character type in COBOL.

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
DATA DIVISION. WORKING-STORAGE SECTION. 01 CHARACTER-DATA. 05 NAME PIC X(20) VALUE 'John Smith'. 05 ADDRESS PIC X(50) VALUE '123 Main Street'. 05 PHONE PIC X(12) VALUE '555-123-4567'. 05 EMPTY-FIELD PIC X(10) VALUE SPACES. PROCEDURE DIVISION. DISPLAY 'Character Data Examples:' DISPLAY 'Name: ' NAME DISPLAY 'Address: ' ADDRESS DISPLAY 'Phone: ' PHONE DISPLAY 'Empty: "' EMPTY-FIELD '"'

Alphanumeric fields can store any character and are commonly used for names, addresses, descriptions, and other text data. Spaces are used to pad shorter values.

2. Alphabetic (PIC A)

Alphabetic data type stores only letters and spaces, providing validation for fields that should contain only alphabetic characters.

cobol
1
2
3
4
5
6
7
8
9
10
11
12
DATA DIVISION. WORKING-STORAGE SECTION. 01 ALPHABETIC-DATA. 05 FIRST-NAME PIC A(10) VALUE 'John'. 05 LAST-NAME PIC A(15) VALUE 'Smith'. 05 CITY PIC A(20) VALUE 'New York'. PROCEDURE DIVISION. DISPLAY 'Alphabetic Data Examples:' DISPLAY 'First Name: ' FIRST-NAME DISPLAY 'Last Name: ' LAST-NAME DISPLAY 'City: ' CITY

Alphabetic fields automatically validate that only letters and spaces are stored, preventing numeric or special characters from being entered.

Specialized Data Types

1. USAGE Clauses

USAGE clauses specify how data is stored in memory, affecting performance and storage efficiency. Different usage types are optimized for different operations.

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
DATA DIVISION. WORKING-STORAGE SECTION. 01 USAGE-EXAMPLES. 05 DISPLAY-NUM PIC 9(5) USAGE DISPLAY VALUE 12345. 05 BINARY-NUM PIC 9(5) USAGE COMP VALUE 12345. 05 PACKED-NUM PIC 9(5)V99 USAGE COMP-3 VALUE 123.45. 05 INDEX-NUM PIC 9(5) USAGE INDEX VALUE 100. 05 POINTER-VAL USAGE POINTER VALUE NULL. PROCEDURE DIVISION. DISPLAY 'USAGE Clause Examples:' DISPLAY 'Display: ' DISPLAY-NUM DISPLAY 'Binary: ' BINARY-NUM DISPLAY 'Packed: ' PACKED-NUM

USAGE clauses control memory storage format. DISPLAY is default, COMP provides binary storage, COMP-3 provides packed decimal, INDEX is optimized for array indexing, and POINTER stores memory addresses.

2. Data Type Conversion

Data type conversion is essential when working with different data types. COBOL provides automatic conversion in many cases, but explicit conversion may be necessary for optimal performance.

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
DATA DIVISION. WORKING-STORAGE SECTION. 01 CONVERSION-DATA. 05 DISPLAY-VAL PIC 9(5) VALUE 12345. 05 BINARY-VAL PIC 9(5) COMP. 05 CHAR-VAL PIC X(5). PROCEDURE DIVISION. DISPLAY 'Data Type Conversion Examples:' *> Convert DISPLAY to BINARY MOVE DISPLAY-VAL TO BINARY-VAL DISPLAY 'Display to Binary: ' BINARY-VAL *> Convert numeric to character MOVE DISPLAY-VAL TO CHAR-VAL DISPLAY 'Numeric to Character: ' CHAR-VAL *> Convert character to numeric MOVE '98765' TO CHAR-VAL MOVE CHAR-VAL TO DISPLAY-VAL DISPLAY 'Character to Numeric: ' DISPLAY-VAL

Data type conversion allows different data types to work together. COBOL automatically converts between compatible types, but explicit conversion ensures proper handling and performance optimization.

Best Practices for Data Types

Common Data Type Patterns