Master CNC G Code: What Every Engineer Should Read Before Cutting Metal
G code is the instruction set a machine controller executes, line by line, to move a tool through material. This page covers the modal states, coordinate systems, and safety lines that decide whether a program cuts clean or scraps the part. Written for design and process engineers who review programs but do not write them daily.

In this article
- 1
- 2
- 3
- 4
- 5
- 6
What G code actually is, and what it is not
G code is a text instruction set standardized under ISO 6983. Each line, called a block, carries one or more words: a letter address plus a number. G01 X25.4 Y0 F200 tells the controller to move in a straight line to that coordinate at 200 mm/min. The controller reads blocks in order and executes each one before reading the next.
It is not a design file. The geometry lives in the CAM output or the drawing. G code only describes motion, feed, speed, tool changes, and machine state. When a part comes out wrong, the cause is often upstream in the CAM strategy or the workholding, not in the G code itself.
Different controllers share the core word set but diverge on the edges. Fanuc, Siemens, Haas, and Mitsubishi controls all accept G00, G01, G02, and G03, but canned cycles, macro syntax, and high-speed look-ahead differ. A program that runs clean on one control may need edits on another.
For an engineer reviewing a program, the useful question is not whether the code is elegant. It is whether the setup is safe, the coordinates match the fixture, and the toolpath matches the drawing. That is where most scrapped parts come from.
Modal states: why one line can change everything after it
Most G codes are modal. Once you call G01, every following block with coordinates stays in G01 until another motion code appears. This is efficient, and it is also the most common source of crashes. A single missing G00 before a rapid move, or a leftover G41 from a previous operation, carries forward into everything below it.
The three motion codes you will see in almost every program are G00 for rapid positioning, G01 for linear feed, and G02 or G03 for clockwise and counterclockwise arcs. G00 moves at the machine's maximum rapid rate and is never used for cutting. G01 moves at the programmed feed rate F. Arcs need I, J, K offsets or an R value, depending on the control.
Modal state also applies to units, plane selection, and feed mode. G20 selects inches, G21 selects millimeters. G17, G18, and G19 set the active plane for arcs and cutter compensation. G94 sets feed per minute, G95 sets feed per revolution. A program that mixes these without resetting them will produce wrong dimensions that look correct on screen.
The practical rule: at the start of every tool, reset the states you depend on. Do not assume the previous tool left the controller in a known condition. A short safety block at the top of each operation costs nothing and prevents most of the errors that reach the spindle.
Work offsets and the difference between machine and part coordinates
Machine coordinates are fixed to the machine. Part coordinates are set by the work offset, usually G54 through G59. The controller adds the offset to the programmed coordinate to find the physical position. If the offset is wrong, every dimension shifts by the same amount, and the error is easy to spot because it is uniform.
Setting G54 means touching off the part or fixture and storing that position in the offset table. On a vise setup, the X and Y zero is often the corner of the stock, and Z zero is the top face. On a tombstone or a 5-axis trunnion, the offset may reference a rotary center point instead. The choice changes how the CAM posts the program.
A common failure is a mismatch between the CAM setup and the physical offset. The programmer assumes Z zero is the top of the stock; the operator set it to the top of the vise jaw. The tool plunges into the jaw or cuts air. Both the setup sheet and the program header should state the zero reference explicitly.
For parts held in soft jaws or custom fixtures, record the offset values in the setup documentation. When the job runs again six months later, the second setup is faster and the risk of a wrong zero drops sharply. This is a documentation habit, not a machining skill, and it pays off more often.
Cutter compensation, tool length, and the offsets that keep dimensions in tolerance
Cutter compensation, G41 for left and G42 for right, shifts the toolpath by the tool radius so the programmed path describes the finished part edge, not the tool center. The controller reads the radius from the offset table. If the operator updates the radius after a wear measurement, the part dimension changes without editing the program.
This is why cutter comp matters for tolerance control. On a feature held to ±0.005 mm, the operator can adjust the offset in small increments and re-cut. Editing the program coordinates instead would be slower and riskier. Most production shops run comp on all profile cuts for exactly this reason.
Tool length offsets work the same way. Each tool has a stored length; the controller applies it when the tool is called. A wrong length offset means the tool either crashes into the part or cuts air above it. After a tool change or a new insert, the length should be re-measured or verified with a touch-off.
G43 applies the length offset, usually with an H number that matches the tool. G49 cancels it. A safe block before any cutting move is G43 H01 Z50.0, which applies the offset and positions Z at a safe height. Skipping the safe height is one of the few errors that damages both the part and the machine.
Safety lines, dry runs, and when G code is the wrong tool for the job
A safe start line resets the states that matter: G21 for millimeters, G17 for the XY plane, G90 for absolute positioning, G94 for feed per minute, and G40 to cancel any active cutter compensation. It also cancels any active canned cycle with G80. This block is not optional on a machine that runs more than one programmer's work.
A dry run with the tool retracted, or a single-block run at reduced feed, catches most collisions before they happen. On a new program, run the first tool with rapid override down and watch the distance-to-go display. If the remaining travel looks wrong, stop. The display tells you more than the screen preview does.
G code is a poor fit when the geometry is complex and the batch is small. A 5-axis contoured surface with hundreds of small moves is better posted from CAM with look-ahead enabled. Hand-editing that path risks gouges that are hard to see in simulation. Use the code as a review artifact, not a writing exercise.
It is also the wrong tool for a one-off hole pattern that a manual mill can drill in ten minutes. Programming, setup, and proving a CNC program has a fixed cost. Below a certain part count or complexity, that cost does not pay back. Knowing when to stop is part of the skill.
Common G codes and what they change
Word addresses and modal behavior vary slightly by controller. Verify against the machine manual before running.
| Code | Function | Modal? | Typical use |
|---|---|---|---|
| G00 | Rapid positioning | Yes | Move between features, never cutting |
| G01 | Linear feed move | Yes | Straight cuts at programmed feed rate |
| G02 / G03 | Circular arc, CW / CCW | Yes | Fillets, radii, circular pockets |
| G17 / G18 / G19 | Plane selection XY / XZ / YZ | Yes | Set before arcs and cutter comp |
| G20 / G21 | Inch / metric units | Yes | Reset at the top of every program |
| G40 | Cancel cutter compensation | Yes | Safe start line, end of profile cuts |
| G41 / G42 | Cutter comp left / right | Yes | Profile cuts held to tight tolerance |
| G43 / G49 | Apply / cancel tool length offset | Yes | Before and after each tool change |
| G54–G59 | Work coordinate offsets | Yes | Set part zero relative to machine |
| G80 | Cancel canned cycle | Yes | After drilling or tapping cycles |
| G90 / G91 | Absolute / incremental positioning | Yes | Absolute for most production work |
| G94 / G95 | Feed per minute / per revolution | Yes | Per revolution for tapping and turning |
When to trust the code, and when to trust the setup sheet
If the part is simple and the batch is large, invest in the program and let cutter comp hold the tolerance. If the geometry is complex and the batch is small, invest in the CAM strategy and the fixture, then treat the G code as output you verify rather than edit.
Questions engineers ask about G code
Do I need to read G code to review a machining quote?
No. The quote depends on geometry, material, tolerance, and quantity, not on the code. But reading the setup sheet tells you whether the shop has thought about workholding, tool access, and zero reference.
If a shop cannot state which face is the Z zero and how the part is held, that is a sign the process plan is thin. Ask for the setup sheet, not the program.
Why does the same program produce different dimensions on two machines?
Work offsets, tool length offsets, and cutter comp values live on the machine, not in the program. If the offsets differ, the part differs. Thermal growth and machine wear also shift dimensions over a long run.
The fix is to standardize the setup sheet and verify offsets at the start of each run. On tight tolerances, a first-article inspection before full production catches the drift early.
What does G41 actually do to the finished size?
G41 shifts the toolpath left of the programmed direction by the tool radius stored in the offset table. The programmed path then describes the finished edge. Increasing the stored radius cuts the feature smaller; decreasing it cuts larger.
This is why operators adjust the offset rather than the program. A 0.01 mm offset change moves the wall by 0.01 mm, which is how features held to ±0.005 mm are dialed in.
Is hand-written G code still used in production?
For simple features like facing, drilling patterns, and basic pockets, yes. It is fast and predictable. For contoured 3D surfaces, CAM output is standard because the move count is too high to write by hand.
Most shops use a mix. The programmer posts from CAM, then edits the header, the safe start line, and the tool change blocks by hand.
How do I know if a program is safe to run?
Check the safe start line, the work offset, and the tool length offset before the first cut. Run the first tool with rapid override reduced and single-block enabled. Watch the distance-to-go display, not just the simulation.
A program that has been proven on the same machine, fixture, and material is low risk. A program moved between machines is not, even if the code is unchanged.
Does G code affect the surface finish I can hold?
Indirectly. Feed rate, stepover, and tool radius in the code set the theoretical finish. The machine's rigidity, the tool condition, and the material decide what you actually get. A finish of Ra 0.8–1.6 μm is achievable on aluminium with the right parameters, but the code alone does not guarantee it.
If the finish matters, specify it on the drawing and let the shop choose the parameters. Then inspect the first article against that number.
Send us the drawing, not the code
Upload your CAD file and we will return a quotation with a DFM analysis within 12 hours. No minimum order quantity, from one prototype to 10,000+ parts.
12-hour quoteFree DFM analysisNo minimum orderNDA on request