Quick answer:
G-code and M-code are the two primary programming languages used to control CNC machines. G-code (Geometric code) controls the movement of the tool—where it goes, how fast, and along what path. M-code (Miscellaneous code) controls the machine’s auxiliary functions—spindle on/off, coolant flow, tool changes, and program stops. You need both to run a CNC machining operation, and understanding the difference is the first step toward reading, editing, or writing your own programs.
If you are sourcing CNC machining services, you don’t need to write G-code yourself. But knowing how these codes affect cycle time, surface finish, tool wear, and machine compatibility can help you evaluate a supplier’s capability and avoid costly miscommunication.
Table of Contents
Toggle01What Is G-Code?
G-code is the language that tells your CNC machine where to move. Every movement—linear, circular, rapid positioning, or feed-controlled—is defined by a G-code command. These codes are modal, meaning once you set a G-code, it stays active until you change it.
For example, G00 triggers rapid movement, while G01 activates a controlled feed rate. G02 and G03 define clockwise and counterclockwise arcs. Without G-code, the tool has no path.
Most CNC programs begin with a safety block that cancels previous commands and sets the coordinate system. Then G-code directs the tool through each operation. A mistake in a single G-code can scrap a part or damage the tool.
For buyers, the key takeaway is that G-code determines cycle time and geometric accuracy. A well-optimized program reduces machining time and improves surface finish. A poorly written one wastes hours and material.
02What Is M-Code?
M-code controls everything that is not a cutting movement. It turns the spindle on and off, engages coolant,changes tools, and stops the program. These are auxiliary functions, but they are just as critical to safe and efficient machining.
M03 starts the spindle clockwise. M05 stops it. M08 turns coolant on. M06 triggers a tool change. M30 ends the program and resets the machine.
Without proper M-code logic, the machine might start cutting with the spindle off, forget to apply coolant during a heavy cut, or change tools before reaching a safe position. These errors cause crashes, broken tools, and scrapped parts.
From a procurement perspective, M-code affects machine utilization and process reliability. If a supplier’s programs skip coolant commands or use incorrect tool-change sequences, it signals poor programming discipline. That often leads to inconsistent quality and longer lead times.

03Key Differences Between G-Code and M-Code
| Aspect | G-Code | M-Code |
|---|---|---|
| Primary function | Controls tool movement and path | Controls machine auxiliary functions |
| Examples | G00, G01, G02, G03 | M03, M05, M08, M30 |
| Modality | Usually modal | Often one-shot or modal |
| Safety impact | Determines geometric accuracy | Prevents crashes and damage |
| Cycle time influence | Directly affects cutting time | Affects non-cutting time |
| Common errors | Wrong feed or path | Missing coolant or spindle stop |
Both code types appear in the same program block. A single line might contain a G-code for movement and an M-code for spindle control. Understanding their interaction helps you read a program and spot potential issues.
04Common G-Codes You Should Recognize
G00 – Rapid positioning. Moves the tool at maximum speed. Use only when the tool is clear of the workpiece.
G01 – Linear feed. Moves the tool at a controlled speed along a straight line. This is the most common cutting command.
G02 and G03 – Circular interpolation. G02 is clockwise arc. G03 is counterclockwise. These define radii and contours.
G17, G18, G19 – Plane selection. G17 selects XY plane, G18 selects XZ, G19 selects YZ. Required for arc commands.
G20 and G21 – Unit selection. G20 is inches. G21 is metric. Mixing these up is a common cause of scrapped parts.
G28 – Return to home position. Used at the end of a cycle or before a tool change.
G90 and G91 – Absolute vs incremental positioning. G90 uses fixed coordinates. G91 uses relative distances. Each has specific use cases.
G43 – Tool length compensation. Adjusts for the actual tool length. Forgetting G43 can cause a crash.
05Common M-Codes You Should Recognize
M00 – Program stop. Pauses the program until the operator restarts. Used for manual checks.
M01 – Optional stop. Works like M00 but only activates if the operator enables it. Useful for batch inspection.
M03 – Spindle on, clockwise. Almost all right-hand tools require M03.
M04 – Spindle on, counterclockwise. Used for left-hand tools or specific threading operations.
M05 – Spindle stop. Should always appear before a tool change or program end.
M06 – Tool change. Stops the spindle, moves to the tool-change position, and swaps tools.
M08 – Coolant on. Critical for heat management and chip evacuation.
M09 – Coolant off. Prevents unnecessary coolant use after cutting.
M30 – Program end and reset. Stops the machine and returns to the beginning of the program.
06How G-Code and M-Code Work Together in a Program

A typical CNC program alternates between G-code for movement and M-code for machine actions. Here is a simplified example:
N10 G21 G90
N20 M06 T01
N30 M03 S1200
N40 G00 X0 Y0 Z2
N50 G01 Z-0.5 F10
N60 G01 X5 Y5 F15
N70 G00 Z2
N80 M05
N90 M30
In this program:
N10 sets metric units and absolute positioning.
N20 changes to tool 1.
N30 starts the spindle at 1200 RPM.
N40 rapidly moves the tool to the start position.
N50 feeds the tool into the material.
N60 cuts a straight line.
N70 retracts the tool.
N80 stops the spindle.
N90 ends the program.
If M03 were missing, the spindle would not turn. If M06 were placed after a cutting move, the machine might crash. The order matters.
YPMFG evaluates every program before production. If a supplier’s code contains sequencing errors or missing safety blocks, it raises a red flag. Reliable machining starts with correct code.
07Why This Matters for CNC Machining Buyers
You may never write a line of G-code or M-code. But understanding what they do helps you ask better questions when evaluating a CNC machining service.
Ask your supplier:
Do you use post-processors that optimize G-code for each machine?
How do you verify code before cutting?
What is your process for handling tool changes and coolant commands?
Can you show me a sample program for a similar part?
A supplier who treats G-code and M-code as an afterthought often produces parts with inconsistent geometry, longer cycle times, and higher reject rates. A supplier who treats programming as a core skill typically delivers tighter tolerances and faster turnaround.
YPMFG writes and reviews every program with machine-specific parameters. This reduces setup time and eliminates common programming errors before material touches the spindle. If you have a part that requires complex contouring or tight tolerances, you can send your specifications to YPMFG for a program review and machining quote.
08Common Questions About CNC G-Code and M-Code
Is G-code the same on all CNC machines?
Not exactly. While common codes like G00 and G01 are standardized, different machine manufacturers may add custom codes or modify existing ones. Always verify the code set for the specific machine being used.
Do I need to learn G-code to order CNC parts?
No. Your supplier writes the program based on your CAD model and specifications. However, knowing the basics helps you communicate requirements for cycle time, surface finish, and tooling preferences.
What happens if an M-code is missing in the program?
The machine may skip a critical action. Missing M03 means the spindle won’t start. Missing M08 means no coolant. Both can damage the tool or the workpiece. This is why program verification is essential.
Can G-code and M-code be edited after the program is written?
Yes. Most CNC programs are editable. Operators often adjust feed rates, spindle speeds, or tool offsets to optimize the process. The original CAM output is usually a starting point, not a final version.
How do I know if a supplier writes good G-code?
Look for clean, logical code with proper use of safety blocks, coolant commands, and tool-change sequences. A supplier who rushes through programming often produces code that is hard to read and prone to errors.
Does the type of material affect G-code selection?
Yes. Harder materials require slower feed rates and lower spindle speeds. Soft materials need different chip loads. A good programmer adjusts G-code parameters to match the material properties.
What is the most common mistake in CNC programming?
Forgetting to set the correct coordinate system or unit mode. G20 (inches) vs G21 (metric) and G90 (absolute) vs G91 (incremental) are frequent sources of scrapped parts.
Can one program run on multiple machines?
Rarely without modification. Different machines have different tool-change positions, spindle orientations, and control systems. A program written for one machine often requires adjustments before running on another.
09Choosing the Right CNC Machining Partner
G-code and M-code are not just technical details. They directly affect the quality, cost, and delivery of your machined parts. A supplier who treats programming as a quick step before cutting is likely to produce inconsistent results. A supplier who verifies, optimizes, and documents their code will deliver parts that meet your specifications on time.
When you evaluate a CNC machining service, ask how they handle programming. Ask about their verification process. Ask how they handle tool changes, coolant control, and program edits.
YPMFG builds every program with machine-specific parameters and safety checks. If you have a part that requires custom fixturing, complex tool paths, or tight tolerances, contact YPMFG to discuss your project. You can request a quote, send your engineering drawings, or ask for a program review before committing to production.


