Skip to main content
Machinist 10 min read Feb 19, 2026

CNC G-Code Basics Explained

Program structure, coordinate systems, canned cycles, and the codes every operator needs

G-code is the language CNC machines understand. Whether the program was written by hand, generated by CAM software, or created at the machine control with conversational programming, the machine executes G-code commands one line at a time. Understanding the basic codes lets you read any program, make simple edits at the machine, and troubleshoot problems without going back to the programmer.

\n

This guide covers program structure, the coordinate system, motion commands, canned cycles, and the essential codes that every CNC operator should know by memory. The focus is on the ANSI/ISO standard codes that work across Fanuc, Haas, Mazak, and most other common controls.

Program Structure: Safety Block to End

A well-structured CNC program follows a consistent pattern: program start, safety block, tool calls with cutting operations, and program end. The safety block at the beginning cancels any modal codes left over from a previous program and establishes known starting conditions.

\n

A typical safety block for a milling program: G17 G20 G40 G49 G80 G90. This sets the XY plane (G17), inch mode (G20), cancels cutter compensation (G40), cancels tool length compensation (G49), cancels any canned cycle (G80), and sets absolute positioning (G90). Every program should start with some version of this block to prevent unexpected behavior from leftover modal states.

\n

Each tool operation follows a pattern: call the tool (T1 M06), set the spindle speed and direction (S3000 M03), activate coolant (M08), set the work offset (G54), activate tool length compensation (G43 H1 Z1.0), execute the cutting moves, retract, turn off coolant (M09), and stop the spindle (M05). Repeat for each tool.

\n

The program ends with M30 (end of program and rewind) or M02 (end of program without rewind). M30 is more common because it resets the program to the beginning, ready for the next part.

Formula: Standard program structure:
O0001 (PROGRAM NUMBER)
G17 G20 G40 G49 G80 G90 (SAFETY BLOCK)
T1 M06 (TOOL CHANGE)
S3000 M03 (SPINDLE ON)
G54 (WORK OFFSET)
G43 H1 Z1.0 (TOOL LENGTH COMP)
M08 (COOLANT ON)
... cutting moves ...
M09 (COOLANT OFF)
G28 Z0 (RETURN HOME Z)
M05 (SPINDLE OFF)
M30 (END AND REWIND)
Machinist

CNC G-Code Quick Reference

Searchable CNC G-code and M-code reference. Generic descriptions, syntax examples, and mill vs lathe applicability for 70+ codes. Controller-agnostic per ISO 6983.

Launch Calculator →

Coordinate Systems: G54 Through G59

CNC machines use a coordinate system where every point in the work envelope has a unique X, Y, Z address. The machine home position (where the machine goes on power-up or G28 command) is the machine zero. But you do not program parts from machine zero — you set up work coordinate offsets (G54 through G59) that define the part zero relative to machine zero.

\n

G54 is the first (and most commonly used) work offset. When you touch off the part and enter the offset values, you are telling the machine: "When I say X0 Y0 Z0 in the program, I mean this physical location." G55 through G59 provide five additional offsets for multi-part setups, fixtures with multiple stations, or parts that require different zero points for different operations.

\n

The distinction between G90 (absolute) and G91 (incremental) is about how coordinates are interpreted. In G90, X2.000 means "go to X=2.000 from the work zero." In G91, X2.000 means "move 2.000 inches from the current position in the X direction." Most programs use G90 because absolute coordinates are less error-prone — a wrong move in absolute mode puts you in the wrong place, but a wrong move in incremental mode puts you in the wrong place AND every subsequent move is wrong by the same amount.

Tip: When to use G91 (incremental):
• Bolt patterns and hole patterns: one subroutine, called at different positions
• Pecking operations: retract amounts relative to current depth
• Repeat patterns: same move applied at multiple locations

Use G90 for everything else. Return to G90 after any G91 block to avoid confusion.

Motion Commands: G00, G01, G02, G03

G00 (Rapid Traverse): Moves the tool at the machine's maximum speed to the commanded position. Used for non-cutting moves: approaching the part, repositioning between features, retracting after a cut. Never use G00 during cutting — the uncontrolled speed produces no useful machining and risks crashing the tool.

\n

G01 (Linear Feed): Moves the tool in a straight line at the programmed feed rate (F value). This is the primary cutting command. G01 X2.0 Y1.5 F10.0 moves the tool in a straight line to X2.000 Y1.500 at 10 inches per minute. Every straight-line cut, plunge, and ramp uses G01.

\n

G02 (Circular Clockwise): Cuts a clockwise arc. Specify the endpoint (X, Y) and either the arc center (I, J) or the radius (R). G02 X2.0 Y0 I1.0 J0 cuts a clockwise 90-degree arc. The I and J values are incremental distances from the start point to the arc center.

\n

G03 (Circular Counter-Clockwise): Same as G02 but in the opposite direction. G03 is used for counter-clockwise arcs. The same I/J or R syntax applies.

\n

These four commands account for the vast majority of all tool motion in a CNC program. Learn them cold and you can trace through any program path with a pencil and the print.

Warning: G00 safety rules:
• Never rapid into a cut. Always switch to G01 before the tool contacts material.
• Rapid Z moves first (retract), then XY (reposition), then Z again (approach). This prevents dragging the tool across the part.
• Many controls move all axes simultaneously on G00. This means the tool takes a diagonal path, which may collide with clamps or fixtures that a straight-axis move would clear.
Machinist

Job Setup Sheet Generator

Generate CNC job setup sheets with automatic speed, feed, and MRR calculations. Printable shop floor documents with chipload verification, workholding, and coolant notes.

Launch Calculator →

Canned Cycles: G81, G83, G84, and More

Canned cycles are pre-programmed motion sequences for common hole-making operations. Instead of programming every move of a drilling operation (rapid to R plane, feed to Z depth, rapid out), you call a canned cycle with the required parameters and the control handles the motion automatically.

\n

G81 (Drill): Simple drilling. Rapids to R plane, feeds to Z depth, rapids out. Used for shallow holes where chip clearance is not a concern. G81 X1.0 Y1.0 Z-0.500 R0.100 F5.0 drills a 0.500-deep hole at X1 Y1.

\n

G83 (Peck Drill): Drilling with peck cycle for chip clearing. The tool feeds to the first peck depth (Q value), rapids out to R plane to clear chips, rapids back to the previous depth minus a small clearance, feeds the next peck, and repeats until reaching Z depth. Essential for deep holes (depth > 3x diameter).

\n

G84 (Tapping): Rigid tapping cycle. The spindle speed, feed rate, and pitch must be synchronized. The control automatically calculates: F = S × pitch (in IPR) or S / TPI (in IPM with pitch in inches). Rigid tapping produces better thread quality than floating tap holders.

\n

G85/G86/G89 (Boring): Various boring cycles with different retract behaviors — feed out (G85), rapid out with spindle stop (G86), or dwell and feed out (G89). Used for precision bores where the retract behavior affects surface finish.

\n

Cancel canned cycles with G80 before programming any non-drilling operation. Canned cycles are modal — they stay active until cancelled.

Formula: G83 Peck Drill syntax:
G83 X_ Y_ Z_ R_ Q_ F_

X, Y = hole location
Z = final depth (negative)
R = reference plane (start of feed)
Q = peck depth increment
F = feed rate

Example: G83 X1.0 Y1.0 Z-1.500 R0.100 Q0.250 F4.0
Pecks 0.250 per pass to 1.500 deep at 4 IPM.
Machinist

CNC G-Code Quick Reference

Searchable CNC G-code and M-code reference. Generic descriptions, syntax examples, and mill vs lathe applicability for 70+ codes. Controller-agnostic per ISO 6983.

Launch Calculator →

Frequently Asked Questions

Yes. CAM generates the G-code, but you need to read it to verify the program is correct before running it, to make edits at the machine (adjusting feeds, adding dwell, changing approach paths), and to troubleshoot when something goes wrong. Operators who understand G-code can solve problems at the control; operators who do not must send the program back to the programmer for every change.
G98 retracts to the initial Z level (the Z height before the canned cycle was called) after each hole. G99 retracts to the R plane after each hole. Use G98 when you need to clear clamps or other obstructions between holes. Use G99 when the path between holes is clear and you want to save cycle time by not retracting all the way up.
The canned cycle stays active (it is modal). The next time the machine sees an X or Y move, it will execute the canned cycle at that location — drilling a hole you did not intend. This can crash the tool into the part or fixture. Always program G80 after the last hole in a canned cycle pattern.
The basic motion codes (G00, G01, G02, G03) are the same. But many codes differ: mills use G17/G18/G19 for plane selection, G43 for tool length comp, and G81-G89 for hole cycles. Lathes use G70/G71 for roughing cycles, G76 for threading, G96 for constant surface speed, and different axis names (X for diameter, Z for length). Always verify codes against your specific machine's programming manual.

Calculators Referenced in This Guide

Shops & Outbuildings Live

Speeds & Feeds Calculator

Calculate optimal RPM and feed rate for milling and drilling operations. Select material and tool diameter to get recommended cutting speeds, chip load, and material removal rate with risk tier classification.

Machinist Live

Bolt Circle Calculator

Generate X/Y hole coordinates for bolt circle patterns. DRO-ready output with multiple pattern support, interactive SVG diagram, incremental coordinates, and G-code generation.

Machinist Live

CNC G-Code Quick Reference

Searchable CNC G-code and M-code reference. Generic descriptions, syntax examples, and mill vs lathe applicability for 70+ codes. Controller-agnostic per ISO 6983.

Related Guides

Shops & Outbuildings 10 min

How Speeds and Feeds Actually Work

SFM fundamentals, chip load theory, HSS vs carbide differences, why chatter means your feed is too light, and how to dial in speeds on a manual mill.