You are currently viewing COBOL Data Types Explained: A Comprehensive Tutorial with Code Examples

COBOL Data Types Explained: A Comprehensive Tutorial with Code Examples

COBOL (Common Business Oriented Language) is a high-level programming language primarily used in business, finance, and administrative systems. Understanding COBOL data types is crucial for effective programming. In this tutorial, we’ll explore the various data types available in COBOL along with code examples to illustrate their usage.

1. Numeric Data Types

a. USAGE Clause

The USAGE clause specifies how data is represented internally. Common numeric USAGE clauses include:

  • USAGE IS DISPLAY: Represents data in human-readable format.
  • USAGE IS COMP: Represents data in binary format, optimized for storage.
01 numeric-data PIC 9(4).
01 binary-data PIC 9(4) USAGE IS COMP.

b. Numeric Literals

Numeric literals represent numeric values directly in COBOL code.

MOVE 1234 TO numeric-data.

2. Alphabetic Data Types

a. Alphabetic Literals

Alphabetic literals represent characters or strings.

01 name PIC X(20).
01 initial PIC A.

b. Alphabetic Special Characters

Special characters such as space, hyphen, or slash can be represented using SPACE, ZERO, ZEROES, or HIGH-VALUES.

01 blank-field PIC X VALUE SPACE.
01 all-zeroes PIC 9(5) VALUE ZEROES.

3. Alphanumeric Data Types

a. Alphanumeric Literals

Alphanumeric literals represent a mix of alphabetic and numeric characters.

01 address PIC X(50).

4. Usage of REDEFINES Clause

The REDEFINES clause allows one data item to share storage with another data item, providing alternative data views.

01 employee-id PIC 9(5).
01 employee-name REDEFINES employee-id PIC X(5).

Conclusion

Understanding COBOL data types is fundamental for writing efficient and effective COBOL programs. In this tutorial, we covered numeric, alphabetic, and alphanumeric data types along with examples to illustrate their usage. Experiment with these data types in your COBOL programs to gain proficiency and versatility in your programming endeavors.

Leave a Reply