CNC G and M Codes: Complete Beginner’s Guide with Examples [2026]

Created on 05.29
CNC machiningserves as one of the most vital core processes in modern manufacturing, enabling the production of intricate, sophisticated, and high-tolerance components for aerospace, automotive, medical, robotics, and consumer product industries. Behind the exceptional efficiency, precision, and consistency of CNC machining lie G codes and M codes — the foundational programming languages that instruct and control all CNC machine movements and functions.
Many new machinists and manufacturing buyers often misunderstand G codes vs M codes, unaware of their unique and complementary roles in CNC operations. Simply put, G codes govern cutting geometry and tool motion, while M codes manage machine hardware and auxiliary functions, such as spindle control and coolant systems. This critical distinction is essential for optimizing CNC machine performance, eliminating programming errors, and producing high-quality custom parts.
In this comprehensive 2026 guide from SMS, we break down everything about CNC programming G and M codes, including definitions, working principles, full program examples, complete code charts, and key differences, helping you master CNC programming fundamentals for milling and turning operations.

What Is CNC Machining? Definition & Core Advantages

Before diving into G code and M code programming, it is necessary to understand the basics of CNC machining. Short for Computer Numerical Control machining, it is an automated manufacturing technology that uses computerized control systems and precision machine tools to fabricate precise, intricate parts from metals, plastics, alloys, and various raw materials.
CNC machining has revolutionized modern manufacturing by replacing traditional manual machining methods. It delivers unmatched accuracy, speed, and flexibility, supporting both custom prototyping and large-scale mass production. Its four core advantages make it indispensable across global industrial sectors:
  • Precision
: CNC machining achieves ultra-tight tolerances and intricate part details that manual machining cannot replicate, meeting strict industrial precision standards.
  • Efficiency
: Automated operation and optimized tool paths streamline material removal processes, reduce manual labor, and shorten production lead times.
  • Versatility
: Compatible with a wide range of materials and machining applications, covering simple structural parts and complex geometric components.
  • Repeatability
: CNC machines execute identical programmed instructions repeatedly, ensuring consistent dimensional quality for batch-produced parts with zero deviation.

How Does CNC Programming Control CNC Machines?

CNC programming acts as the bridge between digital design and physical machining. The evolution of CNC programming control has completely eliminated traditional machining limitations and optimized G&M code utilization.
Before computerized CNC systems became mainstream, machinists relied on manually punched cards and paper tapes to control machine movements. Operators punched holes in fixed sequences to create basic machine codes. While functional in early manufacturing, this method was extremely tedious and inefficient. Additionally, punched cards were easily damaged, deformed, or lost in busy machine shops, frequently causing production errors and processing delays.
After the introduction of computer-controlled CNC equipment, manual code input became standard. However, programming sophisticated custom parts required hundreds of manual code entries, resulting in heavy workloads, low efficiency, and frequent human errors.
The emergence of advanced CAD and CAM software has revolutionized modern CNC programming. Today’s workflow is highly intelligent and user-friendly: Programmers only need to import part models and machining fixtures into professional software, select cutting tools, and set spindle parameters and tool paths. The system will automatically generate accurate G codes and M codes compatible with all mainstream CNC machines.
This software-driven code generation process greatly simplifies CNC programming, improves machining precision, and supports the production of ultra-complex geometric parts, becoming the standard workflow adopted by professional manufacturers including SMS.

What Are G Codes in CNC Programming?

G codes (Geometry Codes), following the RS-274D industry standard, are the most widely used core CNC programming commands. All G-code commands adopt an alphanumeric format starting with the letter “G”, which stands for Geometry. As the foundation of CNC machining, G codes fully control machine tool movements and geometric cutting paths, telling CNC machines where to start cutting, how to move, and when to stop during part fabrication.
A key challenge for machinists is that G code formats are machine-specific. Different CNC equipment brands have subtle formatting differences, such as the presence or absence of spaces between commands and leading zeros (e.g., G3 vs G03). Using mismatched G codes will trigger program errors and defective parts. Therefore, operators must be fully familiar with their machine’s coding rules before programming.
In addition to standard G commands, CNC programming uses dedicated letter addresses to define multi-dimensional machining parameters, enabling precise and intricate machining tasks:
  • A
: Controls tool rotation offset around the X-axis
  • R
: Defines the radius of arcs generated during machining
  • X, Y, Z
: Indicates the 3D coordinates of cutting tools; X/Y for horizontal and vertical positioning, Z for cutting depth
  • I, J
: Represents the incremental center coordinates of machine arcs
  • N
: Marks program line numbers for code sorting and error troubleshooting

Full Practical CNC Program Example (G&M Integrated Code)

Below is a complete, industry-standard CNC machining program integrating mainstream G codes and M codes, covering safety initialization, milling, drilling, tool change, and program reset. This working code is compatible with most FANUC, Haas, and Siemens CNC machines:
CNC Machining Program Block Reference Guide
Block
Description
Purpose
%
Start of program
Start Program
O00001 (Project 1)
Program number (Program Name)
Start Program
(T1 0.25 END MILL)
Tool description for operator
Start Program
N1 G17 G20 G40 G49 G80 G90
Safety block to ensure the machine is in safe mode
Start Program
N2 T1 M6
Load Tool #1
Change Tool
N3 S9200 M3
Spindle Speed 9200 RPM, On CW
Change Tool
N4 G54
Use fixture Offset #1
Move to Position
N5 M8
Coolant on
Move to Position
N6 G00 X-0.025 Y-0.275
Rapid above part
Move to Position
N7 G43 Z1.0 H1
Rapid to the safe plane, use tool length Offset #1
Move to Position
N8 Z0.1
Rapid to feed plane
Move to Position
N9 G01 Z-0.1 F18
Line move to cutting depth at 18 IPM
Move to Position
N10 G41 Y0.1 D1 F36
Cutter Diameter Compensation left Lead in Line, Dia. Offset #1, 36 IPM
Machine Contour
N11 Y2.025
Line Move
Machine Contour
N12 X2.025
Line Move
Machine Contour
N13 Y-0.025
Line Move
Machine Contour
N14 X-0.025
Line Move
Machine Contour
N15 G40 X-0.4
Turn Cutter Diameter Compensation off with lead-out move
Machine Contour
N16 G00 Z1
Rapid to safe plane
Machine Contour
N17 M5
Spindle Off
Change Tool
N18 M9
Coolant Off
Change Tool
(T2 0.25 DRILL)
Tool description for operator
Change Tool
N19 T2 M6
Load Tool #2
Change Tool
N20 S3820 M3
Spindle Speed 3820 RPM, On CW
Change Tool
N21 M8
Coolant On
Move to Position
N22 X1 Y1
Rapid above hole
Move to Position
N23 G43 Z1 H2
Rapid to safe plane, use tool length Offset 2
Move to Position
N24 Z0.25
Rapid to feed plane
Move to Position
N25 G98 G81 Z-0.325 R0.1 F12
Drill hole (canned) cycle. Depth Z-.325, F12
Drill Hole
N26 G80
Cancel drill cycle
Drill Hole
N27 Z1
Rapid to safe plane
Drill Hole
N28 M5
Spindle Off
End Program
N29 M9
Coolant Off
End Program
N30 G91 G28 Z0
Return to Machine Home Position in Z
End Program
N31 G91 G28 X0 Y0
Return to Machine Home Position in XY
End Program
N32 G90
Reset to absolute positioning mode (for safety)
End Program
N33 M30
Reset the program to the beginning
End Program
%
End Program
End Program

Commonly Used Basic G Codes Functions

Standard G codes convert complex machining operations into standardized, repeatable tasks, ensuring stable machine performance and consistent part quality. The following are the most frequently applied G codes for daily CNC milling and turning:
  • G00 – Rapid Positioning
: Moves the cutting tool to preset coordinates at maximum machine speed without material cutting. It optimizes machining efficiency by speeding up all non-cutting positioning movements.
  • G01 – Linear Interpolation
: The most commonly used CNC cutting command. It drives the tool to move in a precise straight line at a set feed rate, ideal for all straight-line cutting and flat surface machining tasks.
  • G02 – Clockwise Circular Interpolation
: Guides the tool along a clockwise circular path to fabricate arcs and circular part features, ensuring precise curved machining results.
  • G03 – Counter-Clockwise Circular Interpolation
: Executes counter-clockwise arc movement, mirroring G02 functions to complete reverse curved and circular machining tasks.
  • G04 – Dwell
: Pauses machine movement for a predefined duration. This function allows cutting tools to cool down or enables spindles to reach stable target speeds, improving surface machining quality.

Complete G Code Function List Table

This detailed G code chart covers mainstream commands for milling and turning, including classification, core functions, modal attributes, and applicable scenarios:
Code
Category
Function
Modal
For Turning or Milling
G17
Plane Selection
XY Plane Selection
Yes
Both
G96
Speeds and Feeds
Constant Surface Speed
Yes
Turning
G91
Positioning and Modes
Incremental Mode
Yes
Both
G03
Circular Interpolation (CCW)
Create arcs and circles (Counter-Clockwise)
Yes
Both
G04
Dwell Function
Pause for a specified duration
No
Both
G18
Plane Selection
XZ Plane Selection
Yes
Turning
G19
Plane Selection
YZ Plane Selection
Yes
Turning
G20
Unit System
Inch System Mode
Yes
Both
G21
Unit System
Metric System Mode
Yes
Both
G40
Cutter Compensation
Cancel Cutter Compensation
Yes
Milling

What Are M Codes in CNC Programming?

M codes (Miscellaneous Codes) are essential auxiliary CNC programming commands, starting with the letter “M”. Unlike G codes that control geometric cutting motions, M codes govern all non-geometric machine operations, managing the auxiliary hardware functions required throughout the machining process.
M codes control core machine behaviors including program pause, program termination, spindle operation, coolant system switching, and automatic tool changes. A critical programming rule for all CNC equipment: only one M code is allowed per program block. Multiple overlapping M codes in a single line will cause function conflicts, program crashes, and serious machining errors.
Similar to G codes, M code formatting varies across different machine brands, such as M3 vs M03. Professional machinists must be familiar with equipment-specific coding standards to ensure seamless, error-free machining workflows.

Commonly Used Basic M Codes Functions

While partial M codes are machine-specific, the following basic commands are universally compatible with almost all CNC mills and lathes:
  • M00 – Program Stop
: Temporarily pauses the running CNC program, requiring manual operator intervention to resume machining.
  • M02 – Program End
: Fully terminates the machining program and stops machine control operation, requiring manual reset to restart.
  • M03 – Spindle On (Clockwise)
: Activates clockwise spindle rotation, always paired with S-series spindle speed commands.
  • M04 – Spindle On (Counterclockwise)
: Starts counterclockwise spindle rotation for reverse cutting operations.
  • M05 – Spindle Stop
: Stops spindle rotation completely, essential for safe tool changes and program termination.

Complete M Code Function List Table

This full M code chart covers all mainstream auxiliary commands, with clear classification, functional descriptions, modal attributes, and applicable machining types:
Code
Category
Function
Modal
For Turning or Milling
M08
Coolant Control
Coolant flood or On
No
Both
M42
Auxiliary Functions
High Gear Select
No
Turning
M19
Spindle Control
Change spindle orientations
No
Milling
M00
Program Control
Program Stop
No
Both
M02
Program Control
Program End
No
Both
M03
Spindle Control
Spindle On, Clockwise
No
Both
M04
Spindle Control
Spindle On, Counterclockwise
No
Both
M05
Spindle Control
Spindle Stop
No
Both
M06
Tool Change
Automatic Tool Change
No
Both
M09
Coolant Control
Coolant Off
No
Both

G Codes vs M Codes: Ultimate Difference Summary Table

To help readers quickly distinguish and memorize the difference between G codes and M codes, we summarized their core definitions, functions, and working principles in one intuitive comparison table:
Feature
G Codes (Geometry Codes)
M Codes (Miscellaneous Codes)
Core Function
Direct the geometric motion and core cutting function of CNC machines
Control non-motion auxiliary operations of CNC machines
Key Operations
Rapid positioning, linear feed, circular interpolation, 3D coordinate movement
Program stop/end, tool change, spindle on/off, coolant switch
Functional Attribute
Geometric codes for part design and forming
Miscellaneous codes for machine auxiliary operations
Control Object
Tool path, cutting geometry, part dimensional accuracy
Machine PLC system and electromechanical auxiliary functions
Working Logic
Activate and execute CNC machine cutting motions
Activate machine PLC to switch auxiliary hardware status

SMS Professional CNC Machining Expertise

Accurate G code and M code programming is the foundation of zero-defect, high-precision CNC parts production. At SMS, our professional CNC programming team has in-depth mastery of standard and machine-specific G&M code rules, with rich practical experience in optimizing tool paths and programming logic.
We provide one-stop custom CNC machining solutions for global clients, covering CAD design, CAM programming, code optimization, precision machining, and finished part delivery. Every program is strictly inspected to eliminate programming conflicts and motion errors, ensuring stable quality for both prototype customization and mass production orders.
Our user-friendly service platform supports instant CNC quotation and real-time project tracking, helping global manufacturers acquire cost-effective, high-precision CNC parts efficiently.

Conclusion

CNC machines cannot operate automatically or produce qualified precision parts without standard G codes and M codes. G codes define the geometric cutting paths that shape part structures, while M codes guarantee the stable operation of machine auxiliary systems. The perfect synergy of these two code sets ensures the high efficiency, precision, and repeatability of modern CNC machining.
Mastering common G and M codes and their core differences is a basic skill for all CNC programmers and machinists. For manufacturing buyers, cooperating with professional manufacturers like SMS with proficient programming capabilities effectively reduces defective rates, shortens production cycles, and improves overall manufacturing efficiency.
Contact
Leave your information and we will contact you.

Services

CNC Machining Service


Turning Processing Service


Custom Sheet Metal Processing


3D Printing Service


Rapid Prototyping

Quick Links

Home


Blog


Contact Us

Contact

Email:eve@sms-hardware.com


Mobile: 13118040687


WhatsApp:+8613118040687


Address:No.39, Panlong Road, Liaobu Town, Dongguan City, Guangdong Province, China

WhatsApp
微信