COBOL GnuCOBOL 3.2.0 Β·
β verified by execution on 2026-08-16
Modern languages use structs or classes to group related variables together in memory, forming complex objects that the runtime tracks. COBOL was designed before these concepts existed, built specifically to process fixed-width records from punched cards and magnetic tape. Instead of defining a type and then instantiating it, COBOL programs define a hierarchical structure directly in memory using group items.
A group item is a variable that contains other variables, which are called elementary items. You create a hierarchy by using different level numbers. The lower the level number, the higher it is in the hierarchy. The 01 level is the highest level, typically representing an entire record layout on a file or a screen. This directly mirrors how data is stored, which is why COBOL is still uniquely suited for processing vast amounts of structured text data in banking and finance systems.
Customer Record: 12345JOHN DOE 035
First Name: JOHN
Your output
The Illusion of Objects
A reasonable programmer coming from Java, Python, or C++ might look at CUSTOMER-RECORD and think it acts like an object or a pointer reference. It does not. It is not a distinct entity with its own metadata or memory allocation.
In COBOL, a group item is nothing more than a convenient name for a contiguous, uninterrupted block of bytes. When you refer to CUSTOMER-RECORD, you are referring precisely to the 28-byte block of memory that starts at CUSTOMER-ID and ends at CUSTOMER-AGE (5 + 10 + 10 + 3 = 28 bytes).
This is why group items never have a PIC clause. They do not hold their own data; their size and content are entirely defined by the mathematical sum of their elementary items. If you add a PIC clause to a group item, the compiler will instantly reject your code.
The Boundary: Group Items are Alphanumeric
Because a group item is fundamentally just a raw span of bytes, the compiler always treats group items as alphanumeric strings (PIC X), regardless of what data types are defined inside them.
You can MOVE a group item as a single chunk of text, which is an incredibly efficient way to copy entire records from one place in memory to another without iterating over fields. However, this alphanumeric nature leads to a common beginner mistake: you cannot perform math on a group item.
Can you predict what will happen if we move a group item that starts with a number into a group item that starts with letters?
Predict the outputcobol
Read the code. What exactly will it print? Commit to an answer before you look.
IDENTIFICATION DIVISION.PROGRAM-ID. GROUP-MOVE.DATA DIVISION.WORKING-STORAGE SECTION.01 SOURCE-REC. 05 ID-NUM PIC 9(2) VALUE 12. 05 MY-CODE PIC X(3) VALUE 'ABC'.01 DEST-REC. 05 FLDA PIC X(3) VALUE 'XYZ'. 05 FLDB PIC 9(2) VALUE 99.PROCEDURE DIVISION. MOVE SOURCE-REC TO DEST-REC. DISPLAY DEST-REC. STOP RUN.
Output
12ABC
You predicted
If COBOL were trying to align sub-fields by their data type, it might try to match the PIC 9 numbers together or throw an error about type mismatch. It doesnβt do any of that. The entire SOURCE-REC is treated as a single 5-character string "12ABC", which is then blindly copied over the 5 bytes of DEST-REC.
This strictly enforced boundary ensures that COBOL programs can rapidly copy entire fixed-width record layouts from disk to memory and back again, without the overhead of deeply inspecting every single elementary item during a MOVE.
Check yourself
Why does a group item never have a PICTURE (PIC) clause?
Reveal answer
Its size and type are entirely determined by its subordinate elementary items. β A group item is just a name for the contiguous block of bytes that make up its elementary items. It has no data of its own.
If you have a group item containing both alphabetic (PIC A) and numeric (PIC 9) elementary items, how does the compiler treat the group item as a whole?
Reveal answer
As an alphanumeric string (PIC X). β Group items are always treated as alphanumeric strings, regardless of what they contain.
Which of the following is true about level numbers in a group item hierarchy?
Reveal answer
They just need to increase to show subordination (e.g. 01, 05, 10). β Level numbers only need to increase to indicate subordination. It is common practice to count by 5s (01, 05, 10) to leave room for future additions.
Challenges
π Bug Hunt+20 XP
This program tries to do math on a group item, which causes a compiler error! Fix the code so it correctly adds 1 to the numeric age.
This code runs. It just does the wrong thing. Read it, find the defect,
fix it β the tests below decide when you are right.
cobolβ solution verified at build time
COBOL has no in-browser runtime, so this one is pen-and-paper: work out what the
fixed code should be, then open the solution and compare. The expected output
below is real β it came from compiling the solution with GnuCOBOL when this page
was built.
Test 1 β expects "1234036"
Need a hint? (β25% XP)
You cannot perform math on a group item because it is alphanumeric. Add to the elementary item CUST-AGE instead.
Show solution (0 XP)
IDENTIFICATION DIVISION.PROGRAM-ID. FIX-GROUP.DATA DIVISION.WORKING-STORAGE SECTION.01 CUSTOMER. 05 CUST-ID PIC 9(4) VALUE 1234. 05 CUST-AGE PIC 9(3) VALUE 035.PROCEDURE DIVISION. ADD 1 TO CUST-AGE. DISPLAY CUSTOMER. STOP RUN.
π Bug Hunt+20 XP
This program fails to compile because the programmer gave the group item a PIC clause. Fix the code so it successfully compiles and outputs ABCDEF.
This code runs. It just does the wrong thing. Read it, find the defect,
fix it β the tests below decide when you are right.
cobolβ solution verified at build time
COBOL has no in-browser runtime, so this one is pen-and-paper: work out what the
fixed code should be, then open the solution and compare. The expected output
below is real β it came from compiling the solution with GnuCOBOL when this page
was built.
Test 1 β expects "ABCDEF"
Need a hint? (β25% XP)
Remove the PIC clause from the 01 MY-RECORD item. Group items must not have a PICTURE clause.