G-code and M-code are two of the most important command groups used in CNC programming. They appear together in CNC programs and coordinate everything from tool positioning and cutting paths to spindle operation, coolant, tool changes, and program stops.
The common explanation is that G-code controls movement while M-code controls machine functions. This is useful for beginners, but it is not the complete picture. Many G-codes do not directly create cutting motion. They can select units, coordinate systems, machining planes, compensation modes, and canned cycles. Likewise, some M-codes, such as an automatic tool-change command, can cause physical machine movement even though their primary purpose is to control an auxiliary machine function.
Another important issue is compatibility. Basic commands such as G00 and G01 are widely recognized, but CNC programs are not completely universal. G-code behavior can differ between milling and turning controls, while M-codes can vary significantly between machine builders. A program generated for one CNC machine should therefore not automatically be assumed safe for another.
This guide explains the practical differences between G-code and M-code, how the commands work together, how CAM software generates them, why post processors matter, and which programming mistakes are most likely to affect CNC machining accuracy and safety.
What Is G-Code?
G-code is a group of preparatory commands interpreted by a CNC controller. These commands establish how the machine should perform a machining operation.
Some G-codes directly control tool motion. For example, G00 typically commands rapid positioning, G01 produces controlled linear interpolation, and G02 and G03 produce circular interpolation.
However, G-code has a broader function than movement alone. Depending on the controller, G-codes can also establish:
- Absolute or incremental positioning
- Work coordinate systems
- Machining planes
- Eenheden
- Cuttercompensatie
- Tool length compensation
- Drilling and tapping cycles
- Machine-coordinate moves
- Feed modes
- Coordinate transformations
For example, G54 commonly selects a work coordinate system. It does not itself cut the workpiece. G20 and G21 are commonly associated with inch and metric programming, while G17, G18, and G19 select machining planes.
This is why describing every G-code simply as a movement command is inaccurate. A better description is that G-codes establish the geometric and preparatory conditions under which CNC machining occurs.
What Is M-Code?
M-code refers to miscellaneous machine-function commands. These commands generally control machine actions that support the machining process rather than defining the geometric toolpath.
Veelvoorkomende voorbeelden zijn:
- M03 – spindle forward or clockwise
- M04 – spindle reverse or counterclockwise
- M05 – spindle stop
- M06 – tool change on many machining centers
- M08 – coolant on
- M09 – coolant off
- M00 – program stop
- M01 – optional stop
- M30 – program end and reset on many controls
These commands allow the CNC program to coordinate machine hardware with the programmed cutting path.
For example, starting a G01 cutting move without first ensuring that the correct spindle condition has been established could damage the tool or workpiece. G-code and M-code therefore operate as parts of the same machining sequence rather than as independent programming systems.
What Is the Difference Between G-Code and M-Code?
| Feature | G-Code | M-Code |
|---|---|---|
| Primaire doelstelling | Sets motion and machining modes | Controls auxiliary machine functions |
| Typical examples | G00, G01, G02, G03, G54, G90 | M03, M05, M06, M08, M09, M30 |
| Controls geometry | Frequently | Normally no |
| Controls spindle/coolant | Normally no | Frequently |
| Controller dependent | Ja | Yes, often more strongly |
| Can remain modal | Many can | Some can |
The most useful distinction is therefore not simply “movement versus no movement.”
G-code primarily defines machining geometry, positioning, and operating modes, while M-code primarily coordinates auxiliary machine functions.
What Are the Most Common G-Codes?
The following commands are commonly encountered on CNC milling machines, although programmers must always confirm their meaning in the specific controller documentation.
| Code | Typical Function | Waarom dit belangrijk is |
|---|---|---|
| G00 | Rapid positioning | Moves between machining positions quickly |
| G01 | Linear interpolation | Produces straight controlled cutting moves |
| G02 | Clockwise circular interpolation | Machines arcs and circular features |
| G03 | Counterclockwise circular interpolation | Machines arcs in the opposite direction |
| G17 | XY plane selection | Defines the active interpolation plane |
| G20/G21 | Inch/metric mode | Controls interpretation of dimensional values on many controllers |
| G40 | Cancel cutter compensation | Returns to uncompensated toolpath behavior |
| G41/G42 | Cutter compensation left/right | Compensates for cutter radius |
| G43 | Tool length compensation | Accounts for actual tool length |
| G54 | Work coordinate system | Defines which part-zero system is active |
| G90 | Absolute positioning on many milling controls | Coordinates reference the active datum |
| G91 | Incremental positioning on many controls | Coordinates reference the current position |
De Haas CNC milling G-code reference provides a useful example of how G-codes are grouped into motion, plane selection, units, compensation, coordinate systems, and other functions.
What Are the Most Common M-Codes?
| Code | Typical Function |
|---|---|
| M00 | Program stop |
| M01 | Optional program stop |
| M03 | Spindle forward |
| M04 | Spindle reverse |
| M05 | Spindle stop |
| M06 | Tool change on many machining centers |
| M08 | Coolant on |
| M09 | Coolant off |
| M19 | Spindle orientation on many machines |
| M30 | Program end/reset on many controls |
M-codes become increasingly machine-specific when additional hardware is involved. A machine may have commands for pallet changers, rotary-axis brakes, chip conveyors, through-spindle coolant, probes, part catchers, bar feeders, clamping systems, doors, robots, or custom automation.
For this reason, the machine builder’s manual is the safest source when an unfamiliar M-code appears in a program.
Are G-Codes and M-Codes Universal?
No. This is one of the most important practical points for anyone learning CNC programming.
Fundamental commands such as G00, G01, G02, and G03 are widely used, particularly on controllers influenced by the common RS-274 programming format. This creates the impression that G-code is universal.
In reality, differences appear among Fanuc, Haas, Siemens, Heidenhain, Mazak, LinuxCNC, and other controllers. Differences can also exist between mills, lathes, routers, grinders, lasers, and other CNC equipment.
M-codes generally vary even more because they are closely connected to the hardware installed on a particular machine.
A good example of why programmers should be cautious is G90. On many CNC milling controls, G90 specifies absolute positioning. On some CNC lathe controls, however, G90 can identify a turning cycle. The machine type and controller context therefore matter.
Never take a code found online and assume that the same number performs the same function on every machine.
Why Is the CNC Machine Manual More Important Than a Generic Code List?
Generic G-code charts are useful for learning concepts, but they cannot document every machine configuration.
The same controller family can be connected to machines with different:
- Tool changers
- Coolant systems
- Rotary axes
- Spindels
- Probes
- Pallet systems
- Bar feeders
- Automatiseringsapparatuur
Machine builders can therefore assign additional M-functions to their own hardware.
This issue appears frequently in machinist forums when operators search online for an unfamiliar M-code and cannot find a reliable answer. The correct reference is normally the programming or operating documentation for that exact machine and controller.
What Does Modal G-Code Mean?
Understanding modal behavior is more useful than memorizing hundreds of individual commands.
A modal command remains active until another command from the same modal group replaces it or the controller resets that state.
For example, after G01 establishes linear interpolation, subsequent coordinate blocks may continue using linear interpolation without repeating G01 on every line.
Similarly, G90 and G91 typically belong to the same distance-mode group. Activating one replaces the other.
De LinuxCNC G-code documentation groups commands into categories such as motion, plane selection, distance mode, units, cutter compensation, tool-length compensation, and coordinate systems.
This explains why a CNC program can contain several different G-codes in one block. They may control different modal groups simultaneously.
Why Are G90 and G91 Frequently Confused?
On controls where G90 represents absolute positioning, programmed coordinates refer to the established coordinate origin.
With G91 incremental programming, the commanded distance is measured from the tool’s current position.
Consider a simplified example:
G90 G00 X50.0
G91 G00 X10.0
The first move positions the axis at X50 in the active coordinate system. After switching to incremental mode, the following command requests another 10 units of movement from the current position.
The dangerous part is not that either mode is difficult to understand. The problem occurs when a programmer assumes the machine is in one mode while another remains active.
This is why safe-start blocks often explicitly establish critical modal conditions instead of depending on whichever modes were left active by a previous program.
What Does G54 Do?
G54 commonly selects the first work coordinate system.
Machine coordinates describe positions relative to the machine’s own reference system. Work offsets allow the programmer to establish part zero at a more useful location, such as a corner, bore center, fixture datum, or top surface of a workpiece.
Without correct work offsets, an otherwise perfect toolpath can run in the wrong physical location.
G54-G59 and additional work-offset systems are also useful when several fixtures or workpieces are installed on the machine table.
Voor precisie CNC milling, the relationship among CAD geometry, programmed datum, fixture location, probe measurements, and work offsets must remain consistent.
Why Is G43 Important?
Different tools have different lengths. A CNC machine therefore needs a way to relate the programmed tool position to the actual cutting tip.
G43 is commonly used to activate positive tool-length compensation together with an H offset value.
A typical block might contain:
G43 H01 Z25.0
The exact behavior depends on the controller, but the important concept is that the program calls a stored tool-length value so the machine knows where the cutting edge actually is.
An incorrect tool-length offset can produce a machining error far larger than an ordinary feed or speed mistake. If the wrong offset is active, the tool can cut too deep, remain above the workpiece, or collide with the setup.
How Do G-Code and M-Code Work Together in a CNC Program?
A simplified milling sequence may look like this:
T1 M06
G00 G90 G54 X20.0 Y20.0
S4000 M03
G43 H01 Z10.0
M08
G01 Z-2.0 F200
G01 X60.0 F500
G00 Z10.0
M09
M05
M30
The program combines several types of information.
- T1 identifies the selected tool.
- M06 requests a tool change on machines that use this command.
- G00 selects rapid positioning.
- G90 establishes the applicable positioning mode.
- G54 selects the work coordinate system.
- S4000 defines the commanded spindle speed.
- M03 starts the spindle.
- G43 activates tool-length compensation.
- H01 selects the associated tool-length offset.
- M08 activates coolant.
- G01 generates controlled cutting movement.
- F defines feed rate.
- M09 stops coolant.
- M05 stops the spindle.
- M30 ends the program on many controls.
This example also demonstrates another important point: a CNC program contains more than G and M commands. Address words such as X, Y, Z, F, S, T, H, D, I, J, K, P, and R provide parameters that the controller uses with those commands.
What Do F, S, T, H and D Mean in CNC Programs?
Beginners often focus so heavily on G-code and M-code that they overlook the other addresses that actually define the machining conditions.
- F normally specifies feed rate.
- S normally specifies spindle speed.
- T identifies a tool.
- H commonly identifies a tool-length offset.
- D commonly identifies cutter compensation data.
- X, Y and Z specify linear-axis coordinates.
- A, B and C are commonly associated with rotary axes.
- I, J and K are frequently used when defining arcs or other functions.
- P, Q and R can have different meanings depending on the active cycle.
The command and its accompanying addresses must therefore be interpreted together.
Does CAM Software Automatically Create G-Code and M-Code?
Yes, modern production CNC programming is commonly performed through CAM software rather than writing every movement manually.
The programmer defines operations such as facing, pocketing, contouring, drilling, turning, adaptive roughing, finishing, and multi-axis machining inside the CAM environment.
The CAM software calculates the toolpath geometry. A post processor then translates that toolpath into NC code that the target machine and controller can interpret.
This distinction is extremely important.
The same CAM toolpath may require different NC output for a Haas machining center, a Fanuc-controlled machine, or a different multi-axis configuration.
What Is a CNC Post Processor?
A post processor is the translation layer between CAM software and the actual CNC machine.
It defines how toolpath information should be expressed for a particular control and machine configuration, including:
- Program formatting
- Tool changes
- Coordinate output
- Rotary-axis behavior
- Work offsets
- Canned cycles
- Coolant commands
- Spindle commands
- Safe retracts
- Machine-specific M-codes
This is why copying a post processor simply because two machines both “use G-code” can be risky.
The post must match the machine configuration closely enough that the resulting output corresponds to the actual machine behavior.
If CAM Generates the Program, Do Machinists Still Need to Learn G-Code?
This question appears frequently among CNC programmers and engineering students.
For modern complex machining, manually programming thousands of toolpath points is generally inefficient. CAM software is much better suited to producing adaptive roughing, freeform surfaces, and simultaneous multi-axis toolpaths.
However, understanding G-code remains valuable.
A machinist or programmer who can read the program can quickly recognize:
- Which work offset is active
- Which tool is being used
- Whether positioning is absolute or incremental
- When the spindle starts
- When coolant activates
- Where a tool-length offset is called
- Whether a drilling cycle is active
- Where the program ends
This is particularly useful for setup, troubleshooting, proving a program, and communicating between the CAM programmer and machine operator.
For complex generated toolpaths, however, repeatedly editing posted code at the machine can create version-control problems. When the underlying CAM strategy or post processor is wrong, correcting the source and reposting the program is usually more maintainable.
What CNC Programming Errors Cause the Most Problems?
Wrong Work Offset
A program can contain perfectly correct toolpaths but machine the wrong physical location if the wrong G54-family offset is active.
Wrong Absolute or Incremental Mode
An unexpected G90/G91 state can turn a small intended move into a completely different motion.
Incorrect Tool-Length Compensation
Calling the wrong H value or using incorrect measured tool data can shift Z positions and potentially cause collisions.
Wrong Unit Mode
Unit interpretation should never be assumed when transferring or reviewing a CNC program.
Incorrect Post Processor
A program can simulate correctly inside CAM while producing unsuitable output if the post does not represent the target machine.
Machine-Specific M-Code Assumptions
An M-code copied from another machine may perform a different operation or may not exist on the target machine.
Unintended Modal State
Some commands remain active after the line on which they are programmed. Failing to understand this behavior can affect later tool movements.
Can You Run CNC Code From Another Machine?
Never assume that an NC file is portable simply because both machines use similar G-code syntax.
Before transferring a program, programmers may need to review:
- Controller type
- Machine kinematics
- Axis configuration
- Travel limits
- Tool-change logic
- Work-offset conventions
- Tool offsets
- M-code functions
- Rotary-axis direction
- Canned cycles
- Spindle limits
- Coolant functions
For complex programs, regenerating the NC file using the correct post processor is normally preferable to blindly modifying a program written for another machine.
How Should a New CNC Program Be Verified?
Program verification should happen before full-speed production machining.
The workflow may include CAM simulation, post-processor validation, controller simulation where available, offset verification, tool verification, setup inspection, and controlled program proving on the machine.
Operators commonly use machine features such as single-block execution, feed or rapid overrides, and appropriate dry-run or graphics functions while proving unfamiliar programs. Exact procedures depend on the machine and should follow the manufacturer’s operating instructions.
A visual toolpath simulation is useful, but simulation does not automatically prove that the real fixture, tool length, work offset, machine configuration, and post processor are correct.
Are G-Codes Different for CNC Mills and Lathes?
Yes.
The fundamental programming concept remains similar, but milling and turning machines use different geometry and machine functions.
CNC mills commonly work primarily with X, Y, and Z linear axes and may add A, B, or C rotary axes. CNC lathes normally describe operations around a rotating workpiece using X and Z as the principal axes, with additional axes and tooling on more advanced turning centers.
Turning controls also include specialized cycles for roughing, grooving, threading, facing, and other lathe operations.
This is one reason a general online G-code chart should never replace the controller manual.
Tuofa CNC Germany uses both CNC turning and milling processes according to part geometry. The required program structure, tooling, workholding, offsets, and machine commands therefore depend on the selected manufacturing process.
Are G-Code and M-Code Different in 5-Axis CNC Machining?
The fundamental concept remains the same, but the programming environment becomes more complex.
Five-axis programs may contain simultaneous linear and rotary movement, tool-center-point control, coordinate transformations, dynamic work offsets, and specialized compensation functions.
Machine kinematics become particularly important because two 5-axis machines can have very different rotary-axis arrangements.
Consequently, a post processor designed for one 5-axis machine should not be assumed suitable for another simply because both machines have five axes.
For complex custom components, Tuofa CNC Germany evaluates geometry and manufacturing requirements before choosing an appropriate process through its broader custom CNC machining services.
Do CNC Customers Need to Provide G-Code?
Normally, no.
A customer ordering custom CNC machined components usually provides manufacturing information rather than machine-specific NC programs.
The most useful files are typically:
- 3D CAD-model
- 2D engineering drawing
- Materiaalspecificatie
- Kritieke toleranties
- GD&T requirements
- Thread specifications
- Oppervlakteafwerking
- Required quantity
- Inspectievereisten
The CNC manufacturer then develops the process, workholding, tools, CAM operations, post-processed machine program, and inspection method.
An existing NC program can sometimes provide useful manufacturing information, but it should not replace the drawing or CAD definition because the code may contain assumptions specific to another machine, fixture, tool library, post processor, or shop.
How Do G-Code and M-Code Affect CNC Part Quality?
G-code and M-code do not determine quality independently. They form one part of the complete manufacturing process.
Dimensional accuracy and surface finish also depend on:
- Part design
- Machine condition
- Fixture rigidity
- Tool selection
- Gereedschapsslijtage
- Cutting parameters
- Thermisch gedrag
- Work and tool offsets
- Material properties
- Inspectie
However, programming determines how these manufacturing decisions are executed.
An incorrect work offset can shift the entire part. Incorrect compensation can change feature dimensions. Poor toolpaths can increase vibration or tool wear. Incorrect spindle or coolant sequencing can affect cutting conditions.
CNC programming should therefore be considered part of process engineering rather than simply a file required to make the machine move.
Frequently Asked Questions About G-Code and M-Code
Is G-Code Hard to Learn?
Basic G-code is relatively straightforward because a small group of commands covers common positioning and machining operations. Advanced programming becomes more difficult when macros, probing, complex cycles, multi-axis transformations, and machine-specific functions are involved.
Can a CNC Machine Run With Only G-Code?
A simple machine or very limited program may use few M-functions, but normal production machining typically requires auxiliary control for functions such as spindle operation, tool changing, coolant, pauses, or program completion.
Is M-Code the Same on Every CNC Machine?
No. Common M-codes may have similar meanings across many machines, but manufacturers can implement additional or different machine-specific functions. Always verify unfamiliar commands in the machine manual.
Does G-Code Control Spindle Speed?
Spindle speed is normally specified by an S address, while an M-code such as M03 or M04 commands the applicable spindle operating direction on many machines.
Does G-Code Control Feed Rate?
Feed rate is normally specified using an F address. G-codes can determine the movement or feed mode under which that F value is interpreted.
Can I Learn G-Code Without a CNC Machine?
Yes. CNC simulators, CAM software, controller training software, and example programs allow users to learn program structure without immediately running code on production equipment.
Should I Manually Edit CAM-Generated G-Code?
Small controlled edits are sometimes used in shops, but frequent manual changes can cause the posted file and source CAM project to become inconsistent. When possible, recurring problems should be corrected in the CAM operation or post processor and the program regenerated.
Why Does the Same G-Code Work on One Machine but Not Another?
The machines may use different controllers, machine configurations, supported cycles, parameters, or interpretations of the command. Machine-specific documentation should be checked before transferring programs.
Conclusion
G-code and M-code work together to convert CNC programming instructions into an actual machining process. G-code primarily establishes toolpaths, coordinates, compensation, and machining modes, while M-code primarily controls supporting machine functions such as spindle operation, coolant, tool changes, and program control.
The important distinction is more detailed than “G-code moves the tool and M-code does everything else.” G-codes also control non-motion modes, and M-codes can trigger machine mechanisms that physically move.
It is equally important to understand that CNC code is not completely universal. Fundamental commands are widely shared, but controller, machine type, hardware configuration, and post processor can change how a program must be written.
Modern CAM software reduces the need to manually create every program line, but machinists and CNC programmers still benefit from understanding G-code and M-code. That knowledge makes it easier to verify work offsets, compensation, tool changes, machine states, and program logic before a cutting problem becomes a damaged tool or defective component.
For customers ordering CNC components from Tuofa CNC Germany, machine-level programming normally does not need to be supplied. Providing accurate CAD data, drawings, tolerances, materials, surface requirements, and quantities allows the manufacturing team to select the appropriate machine, generate the correct toolpaths, apply the required post processor, and verify the program for the actual production setup.