CNC machining is one of the core processes in manufacturing, producing parts with complex shapes and high precision. Behind its efficiency and accuracy are the G codes and M codes, the programming languages that guide CNC machines: G codes control tool movement and positioning, while M codes manage the machine hardware around the cut.
This guide explains the difference between G codes and M codes, provides common G and M code lists, and works through an example CNC program, so that readers finish with a clearer understanding of how CNC machining actually produces a part.
What Is CNC Programming?
CNC programming is the process of creating instructions that control how a Computer Numerical Control machine operates. These instructions define tool movements, machining speeds, tool changes and the other operations required to manufacture a part.
In modern manufacturing, engineers typically create a 3D model in CAD software, then use CAM software to plan tool paths and generate CNC programs. The programs are then prepared for the specific machine and controller before execution. Manual programming is still used for simpler operations, program adjustments and troubleshooting. A typical CNC program combines G codes, M codes and other elements such as coordinates, feed rates, spindle speeds and tool identifiers.
Comparison | G codes | M codes |
What it changes | Tool motion: position, path, feed | Machine state: spindle, coolant, tool change, program end |
How long it lasts | Mostly modal: stays active until another code in the same group replaces it | Depends on the code: some act once; others, such as spindle on or coolant on, stay active until canceled |
How many per block | Several can share one block | One on most controls; some allow more |
If it is wrong | Wrong geometry and dimensions | Damaged tool or scrapped part |
What Are G-Codes in CNC Programming?
A G code is the instruction type in a CNC program that specifies preparatory functions: commands that set up the work the machine is about to do. Many of them describe tool motion, which is why “G” is often read as short for geometric code. In plain terms, a G code defines the state a block works under: where the tool starts, the path it follows, the feed it runs at, and the units and coordinates those values are read in.
For common examples: G00 positions the tool at rapid, G01 moves the tool in a straight line at a specified feed rate, and G02 and G03 cut arcs clockwise and counterclockwise. Depending on the command, G codes work with additional parameters such as coordinates, feed rates and arc center offsets.
Common G-Code List
Each number maps to one motion or one machining function. The table below works as a lookup for the codes most often met in milling and turning. “Modal” means the code stays in effect after it is written, until another code in the same group replaces or cancels it. Exact behavior varies by controller.
Code | Function | Milling | Turning | Modal |
G00 | Rapid positioning | Yes | Yes | Yes |
G01 | Linear interpolation | Yes | Yes | Yes |
G02 | Clockwise circular interpolation | Yes | Yes | Yes |
G03 | Counterclockwise circular interpolation | Yes | Yes | Yes |
G04 | Dwell (timed pause) | Yes | Yes | No |
G17 | XY plane selection | Yes | Varies | Yes |
G18 | XZ plane selection | Yes | Yes | Yes |
G20 | Inch unit selection | Yes | Yes | Yes |
G21 | Metric unit selection | Yes | Yes | Yes |
G28 | Return to machine reference point | Yes | Yes | No |
G40 | Cancel cutter / tool nose radius compensation | Yes | Yes | Yes |
G41 | Cutter / tool nose radius compensation, left | Yes | Yes | Yes |
G42 | Cutter / tool nose radius compensation, right | Yes | Yes | Yes |
G43 | Tool length compensation | Yes | Varies | Yes |
G54–G59 | Work coordinate system selection | Yes | Yes | Yes |
G73 | High-speed peck drilling / pattern repeating cycle | Yes | Varies | Varies |
G76 | Fine boring / threading cycle | Yes | Varies | Varies |
G80 | Cancel canned cycle | Yes | Varies | Yes |
G81 | Drilling canned cycle | Yes | Varies | Yes |
G83 | Peck drilling canned cycle | Yes | Varies | Yes |
G84 | Tapping canned cycle | Yes | Varies | Yes |
G90 | Absolute positioning (milling) | Yes | Varies | Varies |
G91 | Incremental positioning (milling) | Yes | Varies | Varies |
G94 | Feed per minute (milling) | Yes | Varies | Varies |
G95 | Feed per revolution (milling) | Yes | Varies | Varies |
G96 | Constant surface speed mode | Varies | Yes | Yes |
G97 | Constant RPM mode | Varies | Yes | Yes |
What Are M Codes in CNC Programming?
M codes, short for miscellaneous codes, are commands that control auxiliary functions in CNC machines. Unlike G codes, which primarily define tool movements and machining modes, M codes manage operations such as spindle rotation, tool changes, coolant control and program stops.
Each M code consists of the letter M followed by a number, such as M03, M05 or M08. For example, M03 starts clockwise spindle rotation, M05 stops the spindle, and M08 turns on the coolant. Some M codes run independently, while others require additional parameters or depend on previously established settings. Specific commands vary depending on the CNC machine and controller.
Common M Code List and Functions
Code | Category | Function |
M00 | Program control | Stops the program until the operator resumes it |
M01 | Program control | Stops only when the optional stop is enabled at the panel |
M02 | Program control | Ends the program; the cursor stays at the end on many controls |
M30 | Program control | Ends the program and rewinds to the first block |
M03 | Spindle control | Turns the spindle clockwise at the programmed S speed |
M04 | Spindle control | Turns the spindle counterclockwise at the programmed S speed |
M05 | Spindle control | Stops the spindle |
M06 | Tool change | Swaps in the tool called by the T word |
M08 | Coolant | Opens the coolant supply |
M09 | Coolant | Closes the coolant supply |
G and M Codes in a Sample CNC Program
The following example shows how G codes and M codes work together in a CNC program. Their sequence determines how the machine performs each operation, while additional address letters specify details such as tool positions, feed rates and spindle speeds. First, the most common CNC address letters:
Address | Function |
X, Y, Z | Linear axis coordinates |
I, J, K | Arc center parameters |
R | Arc radius, or the retract plane of a canned cycle |
F | Feed rate |
S | Spindle speed |
T | Tool selection |
H | Tool length offset number |
D | Cutter compensation offset number |
N | Program block sequence number |
Example: CNC Milling Program for Contouring and Drilling
The program below completes two operations: it mills a rectangular contour and drills one hole, using a 0.25 in end mill as T1 and a drill as T2. It assumes inch units (G20), absolute positioning (G90), feed per minute (G94), and a three-axis vertical machining center with a compatible control; G54 and both tool offsets are set before the run. All coordinates, spindle speeds, feed rates and offsets are illustrative only.
Block | Description | Purpose |
% | Program start delimiter | Start program |
O0001 (CONTOURED PLATE) | Program number and name | Start program |
(T1 0.25 END MILL) | Tool 1 description | Tool setup |
N1 G17 G20 G40 G49 G80 G90 G94 | Set XY plane, inch units, cancel compensation and canned cycles, select absolute positioning and feed per minute | Initial setup |
N2 T1 M06 | Select and change to Tool 1 | Tool setup |
N3 S9200 M03 | Set spindle to 9200 rpm and start clockwise rotation | Tool setup |
N4 G54 | Select work coordinate system G54 | Initial setup |
N5 M08 | Turn coolant on | Tool setup |
N6 G00 X-0.025 Y-0.275 | Rapid to the contour approach point | Positioning |
N7 G43 H01 Z1.0 | Apply Tool 1 length compensation, move to Z1.0 | Positioning |
N8 G00 Z0.1 | Rapid approach to Z0.1 | Positioning |
N9 G01 Z-0.1 F18. | Feed to cutting depth at 18 in/min | Contour milling |
N10 G41 Y0.1 D01 F36. | Activate left cutter compensation on the lead-in | Contour milling |
N11 Y2.025 | Linear contour movement | Contour milling |
N12 X2.025 | Linear contour movement | Contour milling |
N13 Y-0.025 | Linear contour movement | Contour milling |
N14 X-0.025 | Linear contour movement | Contour milling |
N15 G40 X-0.4 | Cancel cutter compensation on the lead-out | Contour milling |
N16 G00 Z1.0 | Rapid retract to Z1.0 | Contour milling |
M codes, short for miscellaneous codes, are commands that control auxiliary functions in CNC machines. Unlike G codes, which primarily define tool movements and machining modes, M codes manage operations such as spindle rotation, tool changes, coolant control and program stops. | Stop spindle | Tool change |
N18 M09 | Turn coolant off | Tool change |
(T2 0.25 DRILL) | Tool 2 description | Tool change |
N19 T2 M06 | Select and change to Tool 2 | Tool change |
N20 S3820 M03 | Set spindle to 3820 rpm and start clockwise rotation | Tool setup |
N21 M08 | Turn coolant on | Tool setup |
N22 G00 X1.0 Y1.0 | Rapid above the drilling location | Positioning |
N23 G43 H02 Z1.0 | Apply Tool 2 length compensation, move to Z1.0 | Positioning |
N24 G00 Z0.25 | Rapid approach to Z0.25 | Positioning |
N25 G98 G81 Z-0.325 R0.1 F12. | Drill to Z-0.325 at 12 in/min, return to the initial plane | Drilling |
N26 G80 | Cancel the drilling cycle | Drilling |
N27 G00 Z1.0 | Rapid retract to Z1.0 | Drilling |
N28 M05 | Stop spindle | Program end |
N29 M09 | Turn coolant off | Program end |
N30 G91 G28 Z0 | Return Z to the machine reference position | Program end |
N31 G91 G28 X0 Y0 | Return X and Y to their reference positions | Program end |
N32 G90 | Restore absolute positioning | Program end |
N33 M30 | End the program and reset | Program end |
% | Program end delimiter | Program end |
The table is illustrative only: every value depends on the part, the tools and the machine’s offset table, not on the codes themselves. Before a program like this runs on a machine, an engineer reviews the cutter compensation entry and exit moves, the coordinates against the actual setup, and the reference point returns at the end.
Important Things to Know About G and M Codes
Codes Can Vary Between Machines
Not every CNC machine reads G and M codes the same way. Their functions vary with the control, the machine type and how that machine is configured; FANUC, Haas and Siemens, for example, follow their own programming conventions. G90 usually means absolute positioning in milling, but on some lathes it represents a turning cycle. M codes for tool changes and other machine functions differ in the same way. That is why the programming manual for the specific machine and control has to be checked before a code is used.
Follow the Correct Programming FormatChoosing the right G code commands is only half of writing a usable program; they also have to be written in the format the control reads. A CNC program is organized into blocks, and each block holds commands and the parameters they act on. For example, in
G01 X50 Y20 F100, G01 specifies a straight-line move, X50 and Y20 give the target coordinates, and F100 sets the feed rate.
The way a command is written can vary as well: G3 and G03 mean the same thing on most controls, because the leading zero may be left out. A block that omits a code is not turning that function off, because the mode set earlier in the program still applies. Writing to the format and checking how commands combine is what keeps a program from failing on its first run.
From CNC Programming to Precision Machining with Shengmaisi Hardware
G codes and M codes control CNC operations, but producing precision parts also requires planning and technical expertise. From selecting cutting tools to setting tool paths and machining parameters, every decision affects the finished part. Shengmaisi Hardware engineers review the design and plan the machining strategy before production, balancing tolerance, surface finish, cycle time and material choice.
Dongguan Shengmaisi Hardware Products Co., Ltd. is an export-oriented CNC machining manufacturer based in Dongguan, China, producing custom precision mechanical parts for overseas buyers.
milled and turned components produced to customer drawings and models, from prototypes and low-volume batches to production runs.
more than 20 3-axis, 4-axis and 5-axis machining centers, together with precision grinding equipment, covering milling, turning and finishing operations under one roof.
titanium, stainless steel, aluminum grades including 2017A and 1060, 17-4 PH stainless, and other engineering alloys selected to match your design requirements.
capable of holding tolerances down to ±0.01 mm on suitable features where the drawing calls for them.
G-Code and M-Code FAQ
Is G-code hard to learn?
G-code is approachable once you separate the concepts: motion codes, coordinates, feed and speed, and machine state commands. Basic straight moves, arcs and drilling cycles follow consistent patterns, though mastering offsets, canned cycles and controller-specific conventions takes practice and machine time. You do not need to write G-code yourself to order CNC parts; the supplier’s programmers handle the program while you provide the drawing, material and tolerance requirements.
What is the difference between G-code and M-code?
G codes prepare the machining state and control tool motion — where the tool goes, along what path, and at what feed. M codes control auxiliary machine functions such as spindle start and stop, coolant, tool change and program end. Most G codes are modal, while M codes may be one-shot or modal depending on the command.
Do G and M codes work the same on every CNC machine?
No. Commands and conventions differ by controller and machine type. FANUC, Haas and Siemens controls each follow their own programming standards, and some codes carry different meanings between milling and turning. Always check the programming manual for the specific machine and control before relying on a code.
Do I need to know G-code to request CNC machined parts?
No. As a buyer or designer, your role is to provide an accurate 2D drawing or 3D model, material specification, required tolerance and finish. The CNC supplier’s programmers translate that geometry into the appropriate machine code and verify offsets, tool paths and setup before production.
Conclusion
Understanding G and M codes turns a program from a list of numbers into a readable sequence: which tool runs, when the spindle starts, and where each move begins and ends. Commands and conventions differ between machines, but that underlying structure does not. If you would like to turn your own designs into precision machined parts, send your STEP, STP, STL or IGES file and drawing to Shengmaisi Hardware through the contact or quote page,
and our team will review the project and provide a clear quotation.