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

Get Instant Quote

CNC knowledge

A Detailed Explanation of CNC G Code: How the Controller Reads Your Program

This page gives a detailed explanation of CNC G code at the level of the machine controller: which code groups exist, how modal state carries between blocks, and when a hand-written program stops being the right tool. It is written for engineers and CAM programmers who debug posts and plan setups.

Modal vs one-shot codesG17/G18/G19 planesG41/G42 cutter compG81–G89 canned cycles
Detailed explanation of CNC G code for milling and turning
Fundamentals

How the controller reads a block of CNC G code

A program is not read as a stack of independent lines. Most G codes are modal: once G01 appears, every following axis move is a feed move until another motion code replaces it. The controller keeps that state in memory and applies it to each new block. This is why a missing G00 before a rapid retract can send a tool through a part at feed rate, or why a stray G02 three lines earlier turns a straight cut into an arc.

A block is a single line of instructions. The controller parses it left to right, then executes the whole line as one motion command. Order matters inside the block: the G code sets the mode, the X/Y/Z words give the destination, and F or S words set feed and speed. If the F word appears after an axis word on some older controls, the feed takes effect on the next block, not the current one. Fanuc and Haas handle this differently, so the post must match the control.

Modal state also crosses program boundaries if you are not careful. A G41 called in one subprogram stays active until G40 cancels it. When a tool change happens mid-program, the new tool inherits the modal plane and compensation of the old one unless the post resets them. Most crashes we see in review come from this inheritance, not from a wrong coordinate.

The practical rule: reset modal state at the start of every tool. Write G17 G40 G49 G80 as a safety line. It costs one block and removes a whole class of failure.

  • 1
    Modal codes persistG00–G03, G17–G19, G40/G41/G42, G43/G49 stay active until changed.
  • 2
    One-shot codesG04, G28, G53, G65 fire once and do not carry forward.
  • 3
    Safety lineG17 G40 G49 G80 at each tool change resets the common traps.
Code groups

The six groups in a detailed explanation of CNC G code

G codes fall into families, and each family has one active member at a time. If you write two codes from the same family in one block, the controller either takes the last one or alarms out. Knowing the families is the fastest way to read an unfamiliar program.

Motion codes set the path shape. G00 is rapid positioning, G01 is linear feed, G02 and G03 are clockwise and counterclockwise arcs. G02/G03 need the arc center via I, J, K or the radius via R. Use I, J, K for arcs over 180°; R is ambiguous above a half circle and controls will pick the short arc. That single detail causes more scrapped bores than any other arc error.

Plane and compensation codes set the reference frame. G17 selects XY, G18 selects XZ, G19 selects YZ. Cutter compensation G41 (left) and G42 (right) offset the path by the tool radius stored in the offset table. On a finish pass, a 0.01 mm error in the offset value shows directly on the part. G40 cancels it.

Coordinate and offset codes set where zero is. G54 through G59 pick work offsets. G43 applies tool length compensation with an H number, G49 cancels it. G92 shifts the coordinate system and is best avoided on modern controls because it is hard to trace.

Canned cycles and control codes handle repeats. G81 drilling, G83 peck drilling, G84 tapping, G85 boring cover most hole work. G04 is a dwell, G28 returns to home, G80 cancels the cycle. G98 and G99 set whether the tool returns to the initial plane or the R plane between holes, which changes cycle time on a plate with a hundred holes.

  • 1
    Motion: G00–G03Rapid, linear, two arc directions.
  • 2
    Plane and comp: G17–G19, G40–G42Reference plane and tool radius offset.
  • 3
    Offsets: G54–G59, G43/G49Work zero and tool length.
  • 4
    Cycles: G80–G89Drilling, pecking, tapping, boring.
Cutter compensation

Why cutter compensation is the sharpest edge in CNC G code

G41 and G42 let you program the part contour and let the control offset the path by the tool radius. The benefit is that you can adjust size at the machine without editing geometry: change the offset value by 0.02 mm and the wall moves 0.02 mm. That is how a machinist holds ±0.005 mm on a bore without reposting.

The cost is that the controller has to build a lead-in and lead-out. It needs a linear move at least as long as the tool radius before the contour starts, otherwise it alarms with a gouge or an interference error. On an inside corner tighter than the tool radius, the control cannot fit the offset and stops. This is a geometry limit, not a programming mistake.

Compensation also fails on arcs with R words when the offset makes the arc radius negative. Use I, J, K for compensated arcs. And never ramp comp on while the tool is already in the cut; the first move after G41 must be a lead-in into open material.

For parts with thin walls or long reach, comp lets you sneak up on size with a spring pass. Rough leaving 0.15 mm, measure, then adjust the offset. That workflow is more reliable than chasing tolerance with CAM regeneration. At GreatLight we run this on 5-axis and mill-turn work where a repost would cost a setup.

  • 1
    Lead-in lengthAt least one tool radius of straight move before the contour.
  • 2
    Inside cornersCorner radius must exceed tool radius or comp alarms.
  • 3
    Arc styleUse I, J, K on compensated arcs, not R.
Turning and limits

How CNC G code differs on a lathe and where it stops

Turning uses the same families with different letters. G71 is a roughing cycle, G70 a finish cycle, G76 a threading cycle. On a lathe, X is usually diameter, not radius, so a 0.01 mm offset change moves the surface 0.01 mm but the X word changes by 0.02 mm. Programmers who move from milling to turning lose parts to this alone.

Threading with G76 needs the pitch, the thread height, the first pass depth, and the number of spring passes. Get the height wrong and the thread is under or over size. The control will not correct it for you. For inch threads, the height is roughly 0.6495 times the pitch; for metric, use the standard table rather than a guess.

Hand-written code has a limit. A part with free-form surfaces, blended fillets, or five-axis swarf walls needs CAM output, and the G code becomes a long stream of small G01 moves. That is normal. The value of the post is that it manages the modal state, the tool offsets, and the retracts correctly.

So the boundary is this: use hand-written code for simple 2.5D features, setup moves, and prove-outs. Use CAM for anything with a sculpted surface or more than three axes in simultaneous motion. The controller does not care who wrote the block, but the person debugging it does.

For prototype and low-volume runs, we will dry-run a new program in air and check the first part against the drawing before cutting the batch. Inspection reports are available on request, and 100% inspection is standard before shipment.

  • 1
    Lathe X is diameterOffset moves the surface by half the X word change.
  • 2
    G76 threadingSet pitch, height, first pass, spring passes.
  • 3
    CAM territorySculpted surfaces and simultaneous 5-axis motion.
Quick reference

G code groups and what they control

One active code per group at a time.

GroupCommon codesControlsWhen it bites
MotionG00, G01, G02, G03Path shape and feed or rapidArc over 180° cut with R instead of I, J, K
PlaneG17, G18, G19Active interpolation planeG18 left active turns a face cut into a side cut
Cutter compG40, G41, G42Tool radius offset left or rightLead-in shorter than tool radius alarms out
Length offsetG43, G49Applies tool length from H numberWrong H number drives the tool into the table
Work offsetG54–G59Sets part zero locationTwo setups sharing G54 lose position on the second
Canned cyclesG81, G83, G84, G85Drill, peck, tap, bore patternsG98 vs G99 changes cycle time on deep plates
Cancel and dwellG04, G80Stops cycle or pauses in placeMissing G80 runs the next hole as a drill cycle

The clear split

If the feature is 2.5D, has sharp corners, and fits three axes, write or edit the G code by hand and adjust size with the offset table. If the surface is sculpted, the wall is swept, or the tool has to stay normal to the part, use CAM and treat the G code as generated output. Mixing the two on one program is where crashes live.

FAQs

Common questions on G code

Can the control tell me what modal state is active?

Most Fanuc and Haas controls have a modal or current-command screen that lists the active G codes. On a prove-out, watch that screen at each block rather than the toolpath graphic.

If the control does not have it, add a safety line at every tool change: G17 G40 G49 G80. That resets the groups that cause most unexpected moves.

Why does my arc alarm out only sometimes?

The usual cause is an arc over 180° programmed with an R word. The control picks the short arc and the endpoint does not match the geometry, so it faults.

Use I, J, K center offsets for any arc past a half circle, and for every arc that runs under cutter compensation.

Does G41/G42 work on a lathe?

Yes, with a different convention. On turning, G41 and G42 refer to the tool tip direction rather than the cut side, and the tool nose radius offset is applied from the offset table.

Set the tip orientation number correctly or the thread and shoulder geometry will be off by the nose radius.

How much does cutter comp change cycle time?

It adds a lead-in and lead-out move per contour, typically a few tenths of a second each. On a part with dozens of small pockets, that adds up.

The trade is that you gain the ability to adjust size at the machine, which usually saves more time than the lead moves cost.

What is the fastest way to check a new program?

Run it in single block with the feed override low and the rapid override at 25%, watching the distance-to-go display. That catches offset and modal errors before the tool reaches the part.

Then dry-run in air, then cut a test part and measure before running the batch.

Send us the drawing, not just the file

Upload a STEP file and a PDF drawing and we will return a quote with a DFM note within 12 hours. Free DFM analysis is included, and uploads stay confidential with an NDA on request.

Quote in 12 hours100% inspectionFrom one prototype to 10,000+

Follow the shop

More machining notes

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