CNC G-code and M-code are the basic command languages behind most CNC machining programs. When you first see lines such as G00, G01, M03, or M08, the program may look difficult to read. In reality, these codes follow a clear logic.
G-code tells the machine how the tool should move, while M-code controls machine actions such as spindle operation, coolant, tool changes, and program stops. In this guide, I will explain both code types in a practical way, so you can understand simple CNC programs and see why they matter in real machining work.
What is CNC Machine Code?

CNC machine code is the instruction language that tells a CNC machine what to do during machining. In numerical control programming, machine code works as a structured command format that can be interpreted into machine actions. A CNC machine cannot read a 2D drawing or 3D model in the same way an engineer can. It needs clear commands that define movement, speed, tool actions, machine functions, and program sequence.
In practical machining, CNC machine code turns design information into controlled machine actions. It tells the machine where the cutting tool should move, how fast it should feed, when the spindle should start, when coolant should turn on, when a tool change should happen, and when the program should stop.
A drawing may define the required shape, material, tolerance, and surface finish, but the machine still needs a program to make the part step by step. The program connects the drawing with the machining strategy, cutting tools, work coordinate system, and machine movement. Without clear machine code, the machine would not know the toolpath, cutting order, coordinate position, or machine actions needed to produce the part.
G-code and M-code are the two main command types in this process. G-code handles movement-related commands, while M-code handles machine-function commands. The following sections explain both in more detail.
What Is CNC G-Code?
After the machine setup and machining direction are planned, the program needs clear movement instructions. This section explains the basic role of CNC G-code and then summarizes common G-codes used in machining programs.

CNC G-Code Basics
CNC G-code is the part of a CNC program that controls tool movement, positioning, and cutting paths. It tells the machine where the cutting tool should move, what type of movement it should use, and how the tool should follow the programmed path.
In real machining, G-code can control rapid positioning, straight cutting movement, circular interpolation, plane selection, unit settings, coordinate modes, and return positions. These commands help the machine move in a controlled way instead of relying on manual operation.
Some G-codes are modal codes. A modal G-code stays active until another code in the same group replaces it. For example, if a program sets absolute positioning, the machine continues to read positions in that mode until the program changes it.
Other G-codes are one-shot codes. A one-shot code only affects the current block or command line. This difference matters because a CNC machine may still follow a mode that was set several lines earlier, even when that code does not appear again.
Common CNC G-Codes for Beginners
The table below summarizes common CNC G-codes used in many CNC machining programs. These codes cover basic movement, circular interpolation, plane selection, unit settings, return position, and coordinate modes. They are widely used across different CNC systems, but the exact format and behavior can vary by controller, so every production program should still follow the machine manual.
| G-Code | Function | Explanation |
| G00 | Rapid positioning | Moves the tool quickly to a target position without cutting. |
| G01 | Linear interpolation | Moves the tool in a straight cutting path at a controlled feed rate. |
| G02 | Clockwise circular interpolation | Creates a clockwise arc or circular toolpath. |
| G03 | Counterclockwise circular interpolation | Creates a counterclockwise arc or circular toolpath. |
| G17 / G18 / G19 | Plane selection | Selects the machining plane for arcs, tool compensation, and related movements. |
| G20 / G21 | Inch / metric units | Sets whether the program uses inch or millimeter units. |
| G28 | Machine return position | Sends the machine to a reference or home position. |
| G90 | Absolute positioning | Reads coordinates from a fixed program zero point. |
| G91 | Incremental positioning | Reads coordinates from the tool’s current position. |
What Is CNC M-Code?
After the tool movement is planned, a CNC program still needs commands to manage the machine actions around cutting. This section explains the basic role of CNC M-code and then summarizes common M-codes used in machining programs.

CNC M-Code Basics
CNC M-code is the part of a CNC program that controls machine functions and auxiliary actions. It does not usually define the cutting path directly. Instead, it tells the machine when to start, stop, pause, change tools, turn coolant on or off, or complete the program.
During machining, these commands control the machine state around the cutting process. For example, the program may start the spindle before cutting, turn coolant on during material removal, stop the spindle after an operation, or end the program after the final toolpath.
Beyond individual machine actions, M-code also helps organize the program sequence. Some M-codes create a required stop, an optional stop, a tool change, or a program reset. These actions do not create the part geometry, but they help the machine complete each stage in the right order.
Because M-code is closely connected to machine functions, it should not be treated as fully universal. Many common M-codes are widely used across CNC machining programs, but the exact behavior can still depend on the machine controller and machine configuration, especially for tool changes, coolant systems, and auxiliary functions.
Common CNC M-Codes for Beginners
The table below summarizes common CNC M-codes used in many CNC machining programs. These codes cover program stops, spindle control, tool changes, coolant control, and program end commands. They are widely used across different CNC systems, but machine-specific behavior should always be checked before production.
| M-Code | Function | Explanation |
| M00 | Program stop | Stops the program until the operator restarts it. |
| M01 | Optional stop | Stops the program only when the optional stop is enabled. |
| M03 | Spindle starts clockwise | Starts the spindle rotating clockwise. |
| M04 | Spindle start counterclockwise | Starts the spindle rotating counterclockwise. |
| M05 | Spindle stop | Stops spindle rotation. |
| M06 | Tool change | Commands the machine to change tools. |
| M08 | Coolant on | Turns coolant on during machining. |
| M09 | Coolant off | Turns coolant off. |
| M30 | Program end and reset | Ends the program and resets it to the beginning. |
G-Code vs M-Code: Key Differences Explained
G-code and M-code work together in a CNC program, but they serve different control purposes. Understanding these differences helps you read CNC programs more clearly and avoid mixing toolpath commands with machine operation commands.

Control Purpose in CNC Programs
G-code defines the machine movement needed to create the programmed geometry. It controls tool position, movement type, interpolation, coordinate mode, plane selection, and other motion-related instructions. These commands tell the CNC machine how the tool should travel around or into the workpiece.
M-code controls machine functions that support the cutting process. It manages actions such as spindle start and stop, coolant on and off, tool change, program stop, and program end. These commands do not usually describe the cutting path, but they prepare or control the machine state around that path.
This difference matters when reading a CNC program. A G-code block often needs coordinate values, feed values, or motion context to define movement. An M-code block often appears near spindle, coolant, tool change, stop, or end actions.
Impact on Machining Results
G-code has a direct effect on part geometry and dimensional control. Tool movement, coordinate mode, arc interpolation, and programmed feed movement can affect hole locations, profiles, shoulders, pockets, slots, and other machined features. If the motion command is wrong, the part shape can be wrong.
M-code affects the machining condition rather than the geometry itself. Spindle state, coolant timing, tool change sequence, and program stops can influence tool life, chip evacuation, heat control, and process stability. If the machine action is wrong, the toolpath may be correct, but the cutting condition may still fail.
In production, these two effects often appear together. A correct contour path still needs the right spindle state and coolant support. A correct tool change still needs the next movement command to approach the part safely. Part quality depends on both accurate motion commands and controlled machine functions.
Machine and Controller Dependence
G-code is usually more consistent in its basic movement logic, but it still depends on how the controller reads the program. Common motion codes may look similar, but canned cycles, cutter compensation, work offsets, macro variables, plane selection, and cycle parameters can follow different rules on FANUC, Haas, Siemens, Mazak, or other CNC controls. A drilling cycle, tapping cycle, or tool compensation command may need different formatting even when the cutting operation looks the same on the drawing.
M-code often varies more because it connects directly to machine hardware and auxiliary systems. A machining center, turning center, or mill-turn machine may use different M-codes for tool change, coolant type, spindle orientation, chuck clamp/unclamp, tailstock movement, parts catcher, chip conveyor, probing, or pallet change. In this case, the code not only talks to the controller; it also triggers a real machine function that depends on the builder and machine configuration.
This difference matters when a program moves between machines. A G-code issue may change the toolpath interpretation, compensation behavior, or cycle motion. An M-code issue may start the wrong auxiliary action, skip a required action, or call a function the machine does not support. A CNC program should match the specific controller, machine model, and shop-approved post-processor before production.
G-Code and M-Code Comparison Table
The table below compares G-code and M-code from the main angles that matter in CNC machining: control purpose, typical use, machining effect, and machine dependence. It can help you quickly separate toolpath commands from machine-function commands before reading a full CNC program.
| Item | G-Code | M-Code |
| Main role | Controls tool movement and machining geometry | Controls machine functions and auxiliary actions |
| Main use | Positioning, cutting paths, interpolation, coordinate modes, plane selection, and unit settings | Spindle control, coolant control, tool changes, program stops, program end, and other machine actions |
| Typical examples | G00, G01, G02, G03, G17, G20, G21, G90, G91 | M00, M01, M03, M04, M05, M06, M08, M09, M30 |
| The main question it answers | Where and how should the tool move? | What machine action should happen during machining? |
| Direct effect | Affects toolpath, position, cutting movement, part geometry, and dimensional accuracy | Affects spindle state, coolant use, tool change sequence, process continuity, and machine operation |
How CNC G-Code and M-Code Programs Are Created?
CNC G-code and M-code programs are created by converting part requirements into movement commands and machine-function commands. In real production, this process may involve a CNC programmer, manufacturing engineer, setup technician, and quality team, depending on part complexity and shop workflow.
- Review the part drawing or 3D model: The team checks the drawing, 3D model, material, tolerances, surface finish, holes, threads, and special notes. These details provide the input needed to create toolpaths and machine-function commands.
- Define the machining operations: The team confirms the required operations, such as milling, turning, drilling, tapping, boring, or profiling. Each operation will later become a tool movement, a machine action, and a program sequence command.
- Create toolpaths and machine actions: CAM software generates toolpaths from the part model, stock, tools, and machining operations. Tool movement becomes G-code logic, while tool changes, spindle actions, coolant control, and program stops become M-code logic.
- Post-process the program for the selected machine: The post-processor converts the CAM output into code that the selected CNC controller can read. It outputs G-code for motion and positioning, plus M-code for spindle control, coolant, tool changes, and program end commands.
- Review the generated G-code and M-code: Before machining, the team checks units, coordinate mode, work offset, tool number, spindle speed, feed rate, coolant command, retract movement, and program end. This helps confirm that the program matches the drawing, setup, and controller requirements.
How to Read G-Code and M-Code in a Simple CNC Program?
Reading G-code and M-code becomes easier when you understand both the structure of a program line and the role of each command in the machining sequence. A simple CNC program usually includes setup commands, tool and spindle commands, movement commands, coolant commands, and program-end commands.
Basic Structure of a CNC Program Line
A CNC program is usually made of command blocks. Each block may include letters and numbers that define movement type, machine action, axis position, spindle speed, feed rate, tool number, or program sequence.
The table below shows common address letters you may see in a simple CNC program.
| Address | Meaning | Example |
| G | Movement or mode command | G01 |
| M | Machine function command | M03 |
| X / Y / Z | Axis position | X50.0 |
| F | Feed rate | F300 |
| S | Spindle speed | S3000 |
| T | Tool number | T1 |
| N | Program line number | N10 |
Simple CNC Program Example

The image above shows a simple CNC program example. The list below breaks down each program line by its machining role. It shows how setup commands, tool and spindle commands, positioning moves, cutting moves, coolant control, and program-end commands work together in a simple CNC sequence.
- G21 G90 G17 sets the basic machining environment: G21 tells the machine to read dimensions in millimeters, G90 sets absolute positioning from the program zero point, and G17 selects the XY plane for machining.
- T1 M06 prepares the cutting tool: T1 calls tool 1 from the tool list, and M06 tells the machine to complete the tool change before cutting starts.
- S3000 M03 prepares spindle rotation: S3000 sets the spindle speed to 3000 rpm, and M03 starts clockwise spindle rotation, so the tool is ready for cutting.
- G00 X0 Y0 Z5.0 moves the tool to the start position: G00 is a rapid positioning move, while X0 Y0 Z5.0 places the tool above the workpiece with safe Z clearance.
- M08 turns coolant on before cutting: This helps support chip removal and heat control during the cutting move.
- G01 Z-1.0 F200 feeds the tool into the material: G01 is a controlled cutting move, Z-1.0 sets the cutting depth, and F200 sets the feed rate for this entry move.
- G01 X50.0 Y0 F300 performs the main cutting move: The tool moves in a straight path to X50.0 Y0 at a feed rate of 300, creating the programmed cut.
- G00 Z10.0 retracts the tool after cutting: This rapid move lifts the tool away from the workpiece to a safer height.
- M09 turns the coolant off after cutting: The coolant stops after the cutting operation is complete.
- M30 ends and resets the program: It tells the machine that the program is complete and usually returns the program to the beginning.
How G-Code and M-Code Affect Real CNC Machining Results?
G-code and M-code do not only make a CNC machine “run.” They affect whether the machine cuts in the right position, follows the correct sequence, uses the right support actions, and keeps the process stable. In real machining, a small code problem can lead to dimensional error, poor surface finish, tool wear, or production risk.

Toolpath Commands Affect Part Accuracy
G-code controls the path that creates the part geometry. Movement commands, coordinate modes, plane selection, and arc interpolation affect where the tool cuts and how it reaches each feature. If these commands are wrong, the machine may cut the wrong position, leave extra stock, overcut a profile, or miss a hole location.
This is especially important for holes, slots, pockets, shoulders, sealing faces, and assembly features. For example, a wrong coordinate mode can shift a toolpath from the intended work zero, while a wrong arc command can distort a radius, circular pocket, or curved profile.
G-code affects dimensional accuracy because it turns drawing geometry into actual tool movement. The drawing defines the target size, but the programmed movement decides how the cutter reaches that size.
Cutting Moves and Spindle Commands Affect Surface Finish
Surface finish depends on the cutting movement and the machine state during cutting. G-code commands, such as controlled linear or circular cutting moves, work together with feed values, while spindle commands start or stop the rotation needed for cutting.
If feed movement, spindle speed, and spindle state do not match the material and tool, the machined surface can show heavy tool marks, chatter marks, burrs, heat discoloration, or surface tearing. In some cases, the tool may rub instead of cutting cleanly, which can increase heat and shorten tool life.
The code does not create surface finish by itself, but it controls key conditions that shape the final surface. Cutting moves, feed values, spindle commands, tool condition, material behavior, and machine rigidity all need to work together.
Coolant and Tool Change Commands Affect Process Stability
M-code controls support actions that keep machining stable. Common examples include spindle start, spindle stop, coolant on, coolant off, tool change, optional stop, and program end. These commands do not create geometry directly, but they affect whether the tool can cut under the right conditions.
If the coolant turns on too late or turns off too early, chips may stay in the cutting zone, and heat may build up. This can cause rough surfaces, faster tool wear, poor chip evacuation, or tool breakage, especially in deep pockets, slots, holes, and heat-sensitive materials.
Tool change commands also affect process stability. If the program calls the wrong tool, skips a tool change, or continues with an unsuitable cutter, the machine may cut the correct path with the wrong tool. That can damage the part, break the tool, or produce features outside the drawing requirement.
Code Sequence Affects Machining Safety
G-code and M-code must appear in a logical order. The machine needs the right unit setting, coordinate mode, work offset, tool call, spindle state, coolant command, approach move, cutting move, retract move, and program end. A single command may look correct, but it can still create risk if it appears at the wrong time.
For example, a rapid move without enough Z clearance can bring the tool too close to the workpiece or fixture. A cutting move before the spindle starts can damage the tool and workpiece. A coolant command after the cut begins may leave the tool unsupported during the highest-load part of the operation.
Safe CNC machining depends on both correct commands and correct command order. This is why G-code and M-code should be reviewed as a complete program, not as separate code definitions.
Why Basic G-Code and M-Code Knowledge Helps CNC Part Buyers?

Most CNC part buyers do not need to write or review G-code and M-code themselves. However, if you understand the basic role of these commands, you can better see what happens between a drawing and a finished CNC-machined part, especially when cost, lead time, tolerance control, surface finish, or complex features need clearer discussion.
- Understand the machining process: G-code and M-code show that CNC machining is not just “uploading a file.” The part must be converted into tool movement, spindle actions, coolant control, tool changes, and machining sequence.
- Better understand cost and lead time: Parts with complex G-code toolpaths, multiple tool changes, strict coordinate control, or machine-specific tool changes may need more programming and checking before production. This can affect quotation time, setup preparation, and lead time.
- Discuss quality requirements more clearly: G-code and M-code knowledge helps you connect quality issues with program details, such as toolpath accuracy, coordinate mode, spindle commands, coolant timing, and tool changes.
- Identify parts that need machining review: Complex features may need review because the G-code path and M-code sequence can affect safe movement, tool changes, coolant timing, and machining order before production.
Conclusion
CNC G-code and M-code help turn a machining program into controlled movement and machine actions. Once you understand their basic roles, simple CNC programs become easier to read, and code-related issues become easier to connect with tool movement, spindle control, coolant timing, and machining sequence.
You do not need to memorize every code at once. Start with the basic role of G-code and M-code, then read simple programs line by line to see how movement commands and machine actions work together. If you need custom CNC-machined parts, you can send your drawings, 3D files, materials, tolerances, and surface finish requirements to DZ Making for machining review and quotation.
FAQs
1. What is the main difference between G-code and M-code?
G-code controls tool movement, positioning, coordinate modes, and cutting paths. M-code controls machine actions such as spindle start, coolant on, tool change, program stop, and program end.
2. Is G-code the same for every CNC machine?
Not completely. Many basic G-codes are similar across CNC machines, but controller rules, canned cycles, compensation formats, and machine settings can vary.
3. What are the most common G-codes and M-codes?
Common G-codes include G00, G01, G02, G03, G17, G20, G21, G90, and G91. Common M-codes include M00, M01, M03, M05, M06, M08, M09, and M30.
4. Can CAM software generate G-code and M-code?
Yes. CAM software can generate toolpaths from a 3D model, and a post-processor converts those toolpaths into CNC G-code and M-code for a specific machine controller.
5. Can wrong G-code or M-code affect part quality?
Yes. Wrong G-code can affect toolpath position, dimensions, profiles, and surface finish, while wrong M-code can affect spindle state, coolant timing, tool changes, and process stability.
6. Do CNC part buyers need to understand G-code and M-code?
CNC part buyers do not need to write G-code or M-code themselves. However, basic knowledge helps them understand toolpath planning, machine actions, setup time, code review, and why complex parts may need machining review before production.