CNC G Code Basics: How a Program Becomes a Cut
This page covers CNC G code basics as the layer between a CAM toolpath and a spinning tool. It is written for design and manufacturing engineers who receive programs, review them, or debug them on the floor. Read it and you can tell which lines set up the machine, which ones cut metal, and where a program will bite you.

In this article
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
Key takeaways
What a single block of CNC G code basics actually contains
A program is a stack of blocks, and each block is one line the control executes before moving to the next. A typical block looks like N120 G01 X45.0 Y-12.5 F250.0. The N number is a sequence label, useful for restarts and searches. G01 tells the machine to move in a straight line at a controlled feed. X and Y give the destination. F sets the feed rate in mm/min or in/min depending on the control setting.
Order matters. The control parses the block left to right and applies the values as it reads them. If you put F after the coordinates, the feed still applies to that move on most modern controls, but older Fanuc-style logic can treat it differently. Writing F before the axes is the safer habit and matches what most post-processors output.
Words are letter-plus-number pairs, and the letters have fixed meanings: G for preparatory commands, M for machine functions, X Y Z for linear axes, I J K for arc centers, S for spindle speed, T for tool selection. Two G words in the same block are fine only if they belong to different modal groups. G01 and G41 can share a block. G01 and G02 cannot, because both set the motion mode.
- 1NSequence number. Optional on most controls, required for M99 subprogram returns.
- 2GPreparatory command. Sets motion mode, plane, units, offsets.
- 3MMachine function. Spindle on/off, coolant, tool change, program end.
- 4F / SFeed rate and spindle speed. Units depend on G20/G21 and the control.
Why modal groups are the core of CNC G code basics
A modal command stays active after its block ends. Issue G01 once and every following move is a feed move until something changes it. This is why a program can have twenty coordinate lines with no G word at all: the motion mode from earlier is still running. G00, G01, G02 and G03 sit in the same modal group, so any one of them cancels the others.
The same logic applies to G90 and G91. Absolute mode means every X value is a distance from the work origin. Incremental mode means every X value is a distance from the current position. A program written in G91 will look almost identical to one written in G90, which is exactly why this is a common source of scrap. Check the mode before you judge a coordinate.
Non-modal commands run once and disappear. G04 dwell is a good example: it pauses for the programmed time and then has no further effect. G28 machine-home return is another. Mixing these up with modal commands leads to programs that behave correctly on line 40 and wrongly on line 400.
Units are modal too. G20 sets inches, G21 sets millimeters. A program that never states the unit falls back to the control default, and a 25.4× error on every axis is hard to catch in a dry run that only moves a few millimeters.
- 1Group 1: motionG00 rapid, G01 feed, G02 CW arc, G03 CCW arc. One active at a time.
- 2Group 3: planeG17 XY, G18 XZ, G19 YZ. Sets which plane arcs and comp use.
- 3Group 5: distanceG90 absolute, G91 incremental.
- 4Group 6: unitsG20 inch, G21 metric.
Coordinate frames and work offsets in CNC machining G code
Every coordinate in a program resolves against a frame. The machine has a zero point set by the builder, usually at a limit or a reference mark. G53 addresses that machine frame directly. Programs almost never use G53 for cutting moves; it is reserved for safe positions like a tool change point.
The work frame is what the operator sets. G54 through G59 hold stored offsets that shift the program origin to a corner or center of the fixtured part. When the operator picks up the corner of a vise jaw and presses a button, that value lands in G54. The program then says X0 Y0 and the control adds the offset behind the scenes.
A second frame can be stacked on top. G54 with a G52 local offset lets a subprogram run at several positions on a plate without rewriting coordinates. This is useful for multi-part fixtures and for families of holes. It is also a place where bugs hide, because a G52 offset that is never cleared with G52 X0 Y0 Z0 will silently shift everything that follows.
On 5-axis work, frames get another layer. A rotary axis changes where the tool tip actually is, so the control needs a tool center point management function, commonly G43.4 or G43.5 depending on the builder, to keep the tip on the programmed path as the table tilts. Without it, the posted coordinates are only correct at one rotary position.
- 1G53Machine frame. One-shot. Use for safe retract only.
- 2G54–G59Work offsets stored in the control and set by the operator.
- 3G52Local offset added on top of the active work frame.
- 4G43.4 / G43.5Tool center point control for simultaneous 5-axis motion.
Feed, speed and cutter compensation in CNC G code programming
Feed rate F and spindle speed S are the two numbers that decide whether a cut sounds right. Feed is in mm/min under G94 and in mm/rev under G95. A finishing pass on aluminum might run 1,200 mm/min at 8,000 rpm with a 10 mm end mill; the same tool in 316 stainless drops to roughly 300 mm/min and 2,500 rpm. Those are starting points, not rules. Rigidity, tool stickout and coolant all move the numbers.
G94 is the default on most mills and G95 on many lathes. Confusing the two means a feed value of 250 is either a normal cut or a near-zero creep that rubs the tool. Check the mode before you blame the tool.
Cutter compensation is where the program stops describing the part and starts describing the tool path. G41 offsets the tool to the left of the direction of travel, G42 to the right, and G40 cancels it. The offset value can come from the control's tool table or from a D word in the block. On a part with a ±0.005 mm tolerance, comp is how an operator dials in size without editing the CAM file.
Comp needs a lead-in move that is longer than the tool radius. If the first G41 block is also the first contact with the wall, the control has no room to build the offset and you get a gouge or an alarm. A straight or arc lead-in of at least one tool diameter is the normal fix.
- 1G40Cancel cutter compensation.
- 2G41Comp left of the travel direction. Climb milling on a standard right-hand cutter.
- 3G42Comp right of the travel direction.
- 4D wordSelects which offset register holds the radius value.
Where the standard breaks: control dialects and machine limits
There is no single G code standard that every machine obeys. ISO 6983 covers the common core, but builders add, drop and rename commands. A canned cycle that runs on one control may need a different R word or return plane on another. The same program posted for a Fanuc mill and a Heidenhain control will not be interchangeable without a post-processor change.
Machine travel is the other hard boundary. A part that fits in a 500 × 500 × 450 mm envelope cannot be cut on a machine with a smaller one, no matter how the program is written. On our floor, the largest travel is 4,000 × 400 × 150 mm, and the compact machines run 500 × 500 × 450 mm. Long parts in one setup usually mean a different machine class, not a different program.
Spindle taper, tool holder length and rotary table size all leak into what the program can do. A Ø400 mm rotary table sets the maximum swing for 4-axis work. A CAT40 holder with 150 mm of gage length limits how deep a pocket tool can reach before it chatters.
This is the practical boundary of CNC G code basics: the language is portable, the machine is not. Read the program together with the machine spec sheet, not on its own.
- 1Canned cyclesG81–G89 differ in return plane and R word between builders.
- 2Rigid tappingG84 needs synchronized spindle and feed; not all controls sync the same way.
- 3High-speed modesG05, G08, G61.1 and M-code look-ahead all vary by builder.
How to read an unfamiliar program in five minutes
A sequence we use before any first run on a new file.
- 1Scan the header for the five modesFind G20 or G21, G17, G90 or G91, the active G54–G59, and the G43 line. Write them down. Everything else depends on these.
- 2Check the tool list against the setup sheetT numbers, H numbers and D numbers should match the tools actually loaded. A mismatched H number means the wrong length offset.
- 3Trace the first approach moveFrom the tool change position to the first cut, confirm the Z clearance is above the stock and the XY path clears every clamp.
- 4Look for modal changes mid-programSearch for G90, G91, G40 and G54. A mode change buried at line 800 is where most surprises start.
- 5Dry run with the spindle offSingle-block through the first part at reduced rapid, or use the control's graphic simulation if it has one.
- 6Confirm the Z zero against the setupTop of stock or top of fixture? A 0.5 mm difference here shows up as a scrapped first article.
Command groups and what happens if you get them wrong
Same block, different outcome. The failure column is what you see on the part or on the screen.
| Command | Modal group | What it does | Typical failure if misused |
|---|---|---|---|
| G00 | Motion | Rapid to a point at max axis speed | Crashes into a clamp left in the path |
| G01 | Motion | Straight move at programmed feed | Rubs instead of cuts when F is missing |
| G02 / G03 | Motion | Clockwise / counterclockwise arc | Wrong radius if I J K do not match endpoints |
| G17 / G18 / G19 | Plane | Selects arc and comp plane | Arc alarms or a gouge on a side wall |
| G20 / G21 | Units | Inch or metric interpretation | 25.4× size error across every axis |
| G40 / G41 / G42 | Comp | Cancel / left / right cutter offset | Oversize wall or a gouge at the lead-in |
| G43 / G49 | Length comp | Apply / cancel tool length offset | Tool drives to the wrong Z and breaks |
| G54–G59 | Work offset | Shift program origin to the part | Cut lands off the stock, not on it |
| G90 / G91 | Distance | Absolute or incremental moves | Second hole cuts at double the X value |
| G94 / G95 | Feed mode | Feed per minute or per revolution | Feed reads 250 but the cut creeps |
The one rule that matters most
If you only remember one thing: read the header before the body. Units, plane, distance mode, work offset and tool length comp are all set in the first twenty blocks, and every coordinate after them depends on those five choices. When a program behaves correctly on one machine and wrongly on another, the header is almost always where the difference lives.
Questions engineers ask about G code
Can I edit a posted program by hand?
Yes, and it is normal practice for small changes: feed rate, a Z clearance, a lead-in length, a work offset number.
The line to avoid is the CAM-generated geometry. Hand-editing arc centers or a compensated profile usually breaks the path in a way that only shows up at the machine. If the change is bigger than a feed or a clearance value, re-post it.
Why does the same program cut a different size on two machines?
Check three things in order: unit mode, work offset, and tool length or cutter comp register.
A machine that has been running inch programs and receives a metric one without G21 will move 25.4× further than expected. A work offset left over from the previous job shifts the part by that residual amount. Comp registers that were never cleared carry the last tool's radius.
What is the difference between G28 and G53 for a safe retract?
G53 moves to a coordinate in the machine frame and takes effect for one block only. It is predictable and easy to reason about.
G28 sends the axis to the machine reference point, usually through an intermediate point you specify. It depends on the machine's stored reference position, which can be annoying to predict on a machine you do not know well. For a simple retract, G53 with an explicit Z value is the clearer choice.
Do I need cutter compensation if CAM already offsets the path?
Not for geometry, but many shops still program the part profile and let comp handle the offset. The reason is size control.
On a ±0.005 mm feature, the operator can nudge the D register by a few microns and bring the wall into tolerance without touching the CAM file or re-posting. Without comp, every size adjustment means a new program.
How does tolerance on the drawing map to what the program can hold?
The program sets the intended path; the machine, tool and fixturing decide what you actually get.
We hold ±0.005 mm (±0.0002 in) on parts that suit the process, with surface finish from Ra 0.2–0.8 μm on fine work up to Ra 1.6–3.2 μm as-machined. A thin wall, a long tool stickout or a soft fixture will move the result well outside what the code asks for.
Can you work from our program, or do you post your own?
Both. If you send a program with the setup sheet, we will review the header, offsets and tool list before the first run.
More often we receive a 3D model and a drawing, run a DFM check, and post for the specific machine. That route catches features that are hard to reach or tolerances that need a different process before metal is cut.
Send the model. We will tell you what the program needs.
Upload a STEP file and a drawing and we return a quotation with a free DFM analysis within 12 hours. Every part ships after 100% inspection, and uploads stay confidential with an NDA on request.
12-hour quote100% inspectionNDA on request