What is G-Code? Complete CNC Programming Guide

CAM232 Team | April 18, 2026 | 8 min read

G-Code is the standard programming language used to control CNC (Computer Numerical Control) machines. Every CNC mill, lathe, laser cutter, plasma cutter, and even 3D printer relies on G-Code instructions to move the tool, set speeds, and perform cutting operations. In this comprehensive guide, you will learn the fundamental G-Code commands, see practical examples, and understand the differences between major CNC controllers.

How Does G-Code Work?

A G-Code program is a plain text file read line by line by the CNC controller. Each line (called a "block") contains one or more commands that tell the machine what to do. The controller processes these blocks sequentially, moving the cutting tool along precise paths at specified speeds.

A typical G-Code program follows this structure:

  1. Program setup: units, coordinate mode, work offset
  2. Tool selection and spindle start
  3. Machining movements: cutting, drilling, contouring
  4. Spindle stop, coolant off, and program end

Essential G-Code Commands

Motion Commands (G Codes)

CodeDescriptionExample
G0Rapid positioning (no cutting)G0 X100 Y50
G1Linear interpolation (cutting move)G1 X50 Y30 F500
G2Circular interpolation clockwise (CW)G2 X20 Y0 I-10 J0
G3Circular interpolation counter-clockwise (CCW)G3 X20 Y0 I-10 J0
G17XY plane selectionG17
G20Inch unit systemG20
G21Metric unit system (mm)G21
G28Return to reference pointG28 G91 Z0
G40Cancel cutter radius compensationG40
G83Peck drilling cycleG83 Z-20 R2 Q5 F100
G90Absolute coordinate systemG90
G91Incremental coordinate systemG91

Auxiliary Commands (M Codes)

CodeDescription
M3Spindle on clockwise
M4Spindle on counter-clockwise
M5Spindle stop
M6Tool change
M7Mist coolant on
M8Flood coolant on
M9Coolant off
M30Program end and rewind

Example G-Code Program

The following program cuts a simple 50x30mm rectangular pocket:

%
(Rectangular Pocket Operation)
G21 (Metric)
G90 (Absolute)
G17 (XY Plane)
T1 M6 (Select tool 1)
M3 S12000 (Spindle 12000 RPM)
M8 (Coolant on)
G0 Z5 (Safe height)
G0 X0 Y0 (Start position)
G1 Z-2 F100 (Plunge 2mm)
G1 X50 F500 (Cut along X)
G1 Y30 (Cut along Y)
G1 X0 (Return X)
G1 Y0 (Close rectangle)
G0 Z5 (Retract)
M9 (Coolant off)
M5 (Spindle stop)
M30 (Program end)
%

Key Parameters Explained

F - Feed Rate

Defines how fast the tool moves during cutting, measured in mm/min (or inches/min with G20). Typical values: 500 mm/min for aluminum, 200 mm/min for steel. Too fast causes tool breakage; too slow wastes time and generates excessive heat.

S - Spindle Speed

Sets the tool rotation speed in RPM. Calculated using the formula: S = (Vc x 1000) / (PI x D), where Vc is the cutting speed (m/min) and D is the tool diameter (mm).

I, J, K - Arc Center Offsets

Used with G2 and G3 commands to define the arc center relative to the current position. I is the X-axis offset, J is the Y-axis offset, and K is the Z-axis offset.

Controller Differences: Fanuc vs Siemens vs Heidenhain

While G-Code is standardized (ISO 6983), each controller manufacturer has variations:

Fanuc

The most widely used CNC controller. Programs start with O-number (e.g., O0001). Comments use parentheses. Fanuc is the de facto standard that most CAM software targets first.

Siemens Sinumerik

Uses semicolons for comments. Includes built-in CYCLE commands (CYCLE81 for drilling, POCKET3 for pocketing). Variables use R-parameters. Program structure differs slightly with DEF and MSG functions.

Heidenhain

Uses a completely different conversational programming syntax. Linear moves use L commands, circles use C/CC commands, and the coordinate system references differ significantly from ISO G-Code.

Haas

Fanuc-compatible with additional proprietary features. Includes G28 safe return, setting-based customizations, and macro programming capabilities similar to Fanuc.

Generate G-Code Automatically

CAM232 creates G-Code for 13 different operations with support for 8 CNC controllers, 3D simulation, and free usage.

Try It Free

G-Code Best Practices

Conclusion

G-Code is the foundational language of CNC machining. Understanding G0-G3 motion commands, M codes, and feed/speed parameters gives you the knowledge to read, modify, and troubleshoot CNC programs. For generating complex G-Code automatically, CAM software like CAM232 can produce optimized toolpaths in seconds from your DXF designs.