← Back to CNC Programming

Reading and Structuring a CNC Program

A CNC program is not a random list of numbers. This page teaches the anatomy: program number, blocks, word addresses, sequence numbers, and the standard program skeleton every program follows.

Concept

A CNC program consists of blocks (lines), each containing words (letter + number). A word like X25.5 means "the X coordinate is 25.5." A block may contain multiple words: G01 X25.5 Y10. F200. means linear motion to (25.5, 10) at 200 mm/min.

The program number (O1001) identifies the program on the controller. Sequence numbers (N10, N20) label blocks for reference but do not affect execution. Comments in parentheses explain what the code does for human readers.

Why It Matters

One line is a combination of states and actions. G01 stays active until another motion code replaces it. If you forget that G01 is modal and write X100 without F, the feed from earlier still applies. Reading a program means tracking state as you go, not just reading each line in isolation.

A block does not execute "left to right" the way a script does. The controller evaluates all words in a block together: G01 sets the motion mode, X/Y set the target, F sets the feed. Avoid writing conflicting G-codes from the same modal group on one line; the result depends on the controller and can be rejected or ambiguous. Understanding how words group and override is what separates reading code from guessing at it. When in doubt, write one modal group per block and keep the program explicit rather than relying on implicit behavior.

The program number after O identifies this program on the controller and is used when selecting it from memory. On FANUC and Haas, O followed by four or more digits is the program number. The percent signs at top and bottom mark the start and end of the program file; they are optional on some controllers but good practice. Comments in parentheses are ignored by the machine and exist only for humans reading the code. A well-commented program explains the operation, tool, and setup at key points without repeating what the code already says.

How It Works

Anatomy of a Block

N10 G01 X100.0 Y50.0 F300.0
N10 | G01 X100.0 Y50.0 F300 Label Mode X target Y target Feed

Standard Program Skeleton

  1. Program start — % and O number
  2. Startup block — G21 G17 G90 G40 G49 G80 (known state)
  3. Tool change and spindle — T01 M06, S2000 M03
  4. Positioning and machining — G00 approach, G01 cuts, cycles
  5. Retract and shutdown — G00 Z50, M09, M05
  6. Program end — M30, %

Complete Example Program

%
O1001 (DEMO PROGRAM)
G21 G17 G90 G94 G40 G49 G80
G54
T01 M06
S2000 M03
G00 G43 H01 Z50. M08
G00 X0 Y0
G00 Z2.
G01 Z-2. F100.
G01 X50. F300.
G00 Z50. M09
M05
M30
%

Read it top to bottom: startup, tool, spindle, approach, cut, retract, shutdown. Every CNC program follows this skeleton.

Example: Line-by-Line Trace

Entry conditions: Haas mill, tool changed, G54 and H01 offsets already set, Z50 and all XY paths verified clear of fixtures. We trace what the machine does at each line, tracking modal state:

LineWhat happensState after
G21 G17 G90 G94...Known state: metric, XY plane, absolute, feed/min, comp offG21/G17/G90/G94/G40/G49/G80 active
G54Select work offset 54G54 active
T01 M06ATC picks tool 1Tool 1 in spindle
S2000 M03Spindle on CW at 2000 RPMSpindle running CW
G00 G43 H01 Z50.Rapid to Z50 with tool length offset H01Z50 reached, H01 active, M08 coolant on
G00 X0 Y0Rapid XY to (0,0) at Z50X0 Y0 Z50
G00 Z2.Rapid Z to 2 mm above surfaceX0 Y0 Z2
G01 Z-2. F100.Feed down to Z-2 at 100 mm/min (G94)G01 active, F100 modal
G01 X50. F300.Feed to X50 at 300 mm/minF300 modal, at X50 Y0 Z-2
G00 Z50. M09Rapid up to Z50, coolant offG00 active, Z50, M09
M05Spindle stopSpindle off
M30End program, rewindProgram stops

Common Mistakes

Practice

1. Distance 30 mm at F300. Cutting time?

Show answer

30/300 = 0.1 minutes = 6 seconds.

2. What does T03 M06 do?

Show answer

Select tool 3 and execute the automatic tool change on a mill. On a lathe, T0303 indexes turret to station 3 and loads offset 3.

3. Why is the startup block important?

Show answer

It establishes known machine state: metric units, XY plane, absolute mode, compensation off, cycles off. Without it, leftover state from the previous program can cause crashes.

Sources