GreatLight CNC Machining Factory logo
CNC Machining
Rapid Prototyping
Materials
Industries
News
About GL

Get Instant Quote

CNC knowledge

CNC Program Code: The Letter Addresses Behind Every Block

A CNC program is a list of letter addresses. Each letter tells the control what kind of value follows it, and the order inside the block decides what the machine does first. This page breaks the code into six groups, shows where the boundaries sit, and explains what a specific alarm usually points to.

Letter addressesG and M familiesModal behaviorAlarm reading
CNC program code and G code programming explained
Address system

How a CNC program code block is built

Every line a control executes is a block, and a block is a set of words. A word is one letter plus a number: G01, X50.0, F250. The letter is the address, the number is the value, and the control reads the pair together. Change the letter and the same number means something completely different. X50.0 is a position. F50.0 is a feed rate. T50 is a tool.

Blocks are separated by an end-of-block character, usually a semicolon or a line break. Most controls execute the words inside one block in a fixed internal order, not left to right as written. A few older controls are order-sensitive, but on current Fanuc, Siemens and Mitsubishi controls the internal order holds.

Two rules cover most beginner mistakes. Never repeat the same address inside one block, because the control keeps the last value and silently drops the first. And keep one clear intent per block, so a feed move and a rapid move do not sit on the same line.

Modal words stay active until another word of the same group replaces them. G01 stays in effect for every following block until G00 or G02 appears. This is why a program can look short and still cut a long path. It is also why one leftover modal value at the end of a subprogram can ruin the next operation.

Group 1

O, N and the structure letters

O is the program number. On a Fanuc control it is O plus four digits, and it is how the machine finds the program in memory rather than by file name. Two programs cannot share the same O number on the same machine. When a shop receives a file called 1234.nc, someone still has to decide which O number it gets on the floor.

N is the sequence number, one per block. It has no effect on motion. Its value is in recovery: when a tool breaks at N240, the operator can search that number and restart close to the failure instead of scrolling through hundreds of lines. Number in steps of ten so there is room to insert blocks later.

Subprograms use M98 with a P address for the program number and an L address for repeat count. M98 P1000 L3 runs program O1000 three times. M99 at the end of the subprogram returns control to the calling program. Omission of L means one pass.

Keep this layer boring. N numbers that jump around, O numbers that clash with an existing job, and subprograms without a matching M99 cost more floor time than any cutting parameter.

Group 2

G codes: the motion and mode family

G codes set the mode of the machine. They fall into groups, and only one code from a group can be active. G00 rapid, G01 linear feed, G02 clockwise arc and G03 counterclockwise arc form the motion group. G17, G18 and G19 select the working plane for arcs and cutter compensation. G20 and G21 select inch or metric.

The plane selection matters more than most people expect. An arc programmed in G17 with an X and Z endpoint will alarm or, worse, cut something unintended. G18 expects X and Z, G19 expects Y and Z. Check the plane before you check the arc radius.

Compensation codes change the path the machine actually follows. G41 offsets left of the programmed direction, G42 offsets right, and G40 cancels. The offset value lives in the control's tool offset table under a D number, not in the program. That is a deliberate split: the programmer controls the geometry, the operator controls the wear.

Canned cycles are G codes that hide a longer sequence. G81 drills, G83 pecks with full retract, G73 pecks with partial retract, G84 taps, G76 bores with a shift. Each one uses its own set of address letters for depth, retract and dwell, so read the cycle definition, not just the code number.

Group 3

M codes: machine actions outside the motion path

M codes switch things on and off. M03 starts the spindle clockwise, M04 counterclockwise, M05 stops it. M08 turns flood coolant on, M09 turns it off, and M07 mist sits between them. These are not motion commands, so they do not care where the tool is.

Program stop codes deserve their own paragraph. M00 stops everything and waits for a cycle start. M01 is an optional stop that only takes effect when the operator has the optional stop switch on. M30 ends the program and rewinds to the top. M02 ends the program without the rewind on many controls, which changes what happens on the next cycle start.

Tool changes run through M06, usually paired with a T address called earlier in the block. Some controls accept T then M06 on one line, others want T on the previous line so the tool pre-stages in the changer. Follow the machine builder's sequence.

Pallet and rotary functions follow the same pattern. M84 and M95 handle pallet changes on horizontal machines, M10 and M11 clamp and unclamp a rotary axis. Never guess an M code on an unfamiliar control. Look it up in the builder's manual, because M codes are far less standardized than G codes.

Group 4

Coordinate and feed letters

X, Y and Z are the linear axes. A, B and C are rotary axes around X, Y and Z. U, V and W are commonly incremental versions of X, Y and Z on lathes, and on some controls they are a second parallel axis. The same letter can mean two things on two machines, so read the machine parameter list before trusting a handed-down program.

R and I, J, K define arc geometry. R is the radius form and is easy to read but ambiguous for arcs over 180°, where the control cannot tell which way around the circle you mean. I, J and K are center offsets from the arc start and have no such ambiguity. For anything past a half circle, use I, J, K.

F is feed rate in units per minute in G94 mode, or units per revolution in G95 mode. On a lathe, feed per revolution is the usual choice because it keeps chip load constant when the spindle speed changes. On a mill, feed per minute is more common. Mixing the two modes mid-program is a reliable way to break a tool.

S is spindle speed, either direct RPM or a surface speed value when constant surface speed is active. Dwell uses P or X depending on the control, and the unit is usually milliseconds or seconds by parameter. Check which one applies before you write a dwell for a spot face.

Group 5

What the control does with your code before it cuts

A modern control does not execute your file directly. It reads the block, resolves the modal state, applies tool length and diameter offsets, and only then commands the servos. This is why a program that looks correct can still cut the wrong size: the offsets are part of the arithmetic, and they live outside the file.

Look-ahead changes the motion itself. The control scans ahead several blocks and blends the corners so the machine does not stop at every line. On a contour with tight tolerances, that blending can cut the corner slightly. G61 forces exact stop and G64 returns to normal blending, so the choice between them is a real accuracy decision, not a style preference.

Cutter compensation adds a second layer. The programmed path is the part edge, and the control shifts the tool by the D value. If the D value is wrong, the part is wrong even though the code is right. Always verify the offset table against the tool list before the first cut.

Subprograms and macro variables sit on top of all of this. A WHILE loop in a macro can run the same block thousands of times with different values, so the visible file is short and the executed path is long. When debugging, count executions rather than lines.

Reference

Letter groups and where they go wrong

Each row is one address family. The failure column is the mistake we see most often on incoming programs.

LettersWhat they controlTypical failure
O, NProgram and sequence numbersDuplicate O number; broken restart point
GMotion mode, plane, compensationWrong plane for the arc; offset left active
MSpindle, coolant, stops, palletsM02 used where M30 was intended
X Y Z A B C U V WAxis position and directionAbsolute and incremental mixed in one block
R I J KArc radius or arc centerR used on an arc over 180 degrees
F S T D HFeed, speed, tool, offset, lengthFeed per minute written in G95 mode
P Q LDwell, cycle depth, repeat countRepeat count omitted, sub runs once

When to read the program and when to read the machine

If the error repeats at the same block every cycle, it is the code or an offset value. If it appears at random blocks or drifts across a run, stop editing the program and check the machine, the tool holder and the material first.

FAQs

Questions we get about CNC program code

Do all CNC controls use the same letter addresses?

No. X, Y, Z, F and S are close to universal, but M codes and canned cycle parameters vary by builder. Fanuc, Siemens, Heidenhain and Mitsubishi each have their own set for things like pallet change, probing and high-speed modes.

Treat G and M code lists as machine-specific documentation, not general knowledge. When a program moves between two machines in the same shop, re-check every M code before the first run.

Why does my program alarm on an arc that looks correct?

Check the active plane first. G17 expects X and Y, G18 expects X and Z, G19 expects Y and Z. An arc written in the wrong plane will alarm on the radius or the endpoint.

If the plane is right, look at the arc span. Arcs over 180° should use I, J, K center offsets rather than R, because R gives the control two possible centers and it may pick the wrong one.

What is the difference between M00, M01 and M30?

M00 stops the program and waits for the operator to press cycle start. M01 does the same but only when the optional stop switch is active, which makes it useful for inspection pauses you do not always want.

M30 ends the program and returns to the top of the file. M02 ends it without that rewind on many controls. If the next cycle start behaves strangely, a missing M30 is a common cause.

Can I write a whole program with only G01 and no canned cycles?

Yes, and for a one-off part it is often faster than looking up cycle parameters. The trade-off is program length and readability. A peck drilling cycle replaces dozens of lines with one.

On production runs, canned cycles also reduce the chance of a typo in the repeated blocks. Use them once you are confident about the address letters each cycle expects.

How do I know if the problem is the code or the tool offset?

Run the same block in dry run with the tool clear of the part and watch the position display. If the commanded position matches the drawing, the code is right and the offset table is the suspect.

Compare the D and H values against the tool list for that setup. A single transposed digit in a length offset shifts every Z move in the program.

Should sequence numbers appear on every block?

Not every block, but on every block that matters. Restart points, tool changes and cycle starts are the ones an operator will search for after a break.

Number in steps of ten so you can insert a block later without renumbering the file. Skipping numbers costs nothing at the control.

Send us the part, we will send back a program-ready quote

Upload a STEP file and get a quotation with free DFM analysis within 12 hours. No minimum order quantity, from one prototype to 10,000+ part runs.

12-hour quote100% inspectionNDA on request

Follow

More from the shop floor

We publish setup notes, tooling trials and inspection data from the factory floor.

FacebookTikTokYouTubeLinkedInInstagramThreadsPinterest

Trusted by engineers and manufacturers worldwide

Tesla Ford Motor Company BYD Auto Denso Magna International Boeing Airbus Medtronic KUKA FANUC