If you've ever looked at a CNC machine program and felt lost staring at lines of letters and numbers, you're not alone. G-code and M-code are the two building blocks of every CNC program, and understanding the difference between them is the first real step toward writing, reading, or troubleshooting machine programs with confidence. Whether you're a hobbyist running a desktop CNC router or a machinist working with industrial mills, knowing how these two code types work together will save you time, prevent crashes, and help you get better results from your machine.

What exactly is G-code in CNC machining?

G-code stands for "geometry code" (sometimes called "geometric code"). These are the commands that control tool movement and positioning. When a CNC machine cuts a straight line, arcs a circle, or moves at a specific feed rate, G-code is what makes that happen.

Each G-code line starts with the letter G followed by a number. That number tells the machine what kind of motion or mode to use. For example:

  • G00 Rapid move (the tool moves as fast as possible to a position)
  • G01 Linear interpolation (a straight-line cut at a set feed rate)
  • G02 Clockwise arc
  • G03 Counter-clockwise arc
  • G90 Absolute positioning
  • G91 Incremental positioning

Think of G-code as the directions you'd give someone to drive from point A to point B. It says where to go, how fast to move, and what path to follow. These codes directly shape the finished part by controlling the cutter's path through the material.

What does M-code do on a CNC machine?

M-code stands for "miscellaneous code" (some machinists also call it "machine code"). These commands control machine functions that aren't related to tool movement. They handle things like turning the spindle on and off, activating coolant, changing tools, or stopping the program.

Common M-code examples include:

  • M03 Spindle on (clockwise rotation)
  • M05 Spindle off
  • M06 Tool change
  • M08 Coolant on
  • M09 Coolant off
  • M30 End program and rewind

If G-code is the driving directions, M-code is everything else you do during the trip turning on the headlights, opening the garage, or stopping the engine. M-code manages the supporting actions that let the cutting happen safely and correctly.

How are G-code and M-code different at a glance?

The easiest way to separate them:

  • G-code controls where the tool goes and how it moves (geometry and motion).
  • M-code controls what the machine does outside of tool movement (spindle, coolant, program flow).

They work together in every CNC program. A single block (line) of code can include both a G-code and an M-code. For instance:

G01 X50.0 Y25.0 F200 M08

In this line, G01 tells the tool to cut a straight line to coordinates X50 Y25 at a feed rate of 200, while M08 turns the coolant on at the same time. They complement each other one handles movement, the other handles machine state.

If you're new to reading these types of commands, our beginner's reference guide for maker codes breaks down more code definitions in a simple format.

Why do some G-codes and M-codes vary between machines?

This is one of the most confusing parts for beginners. Not every CNC machine manufacturer uses the exact same set of codes. While G00, G01, G02, G03, M03, M05, and a handful of others are nearly universal, many codes are manufacturer-specific.

For example, a Fanuc controller might use a certain M-code for a bar feeder that a Haas controller assigns to a different function. Some machines use M-codes in the range of M50–M99 for custom functions like pallet changers, probe routines, or part catchers.

This is why you should always check the machine's programming manual before assuming a code does what you expect. Running a code that does something unexpected on your specific machine can cause a crash or damage tooling.

What does a real CNC program look like with both codes?

Here's a short example of a basic CNC milling program mixing G-code and M-code:

O0001 (SAMPLE PART)
G90 G21 G17
M06 T01 (END MILL)
M03 S1200
G00 X0 Y0
G43 H01 Z5.0
G01 Z-5.0 F100 M08
G01 X50.0 Y0 F200
G01 X50.0 Y50.0
G01 X0 Y50.0
G01 X0 Y0
G00 Z25.0 M09
M05
M30

Reading it line by line:

  • G90 G21 G17 Absolute mode, metric units, XY plane selected (all G-codes setting up the environment)
  • M06 T01 Change to tool #1 (M-code action)
  • M03 S1200 Turn spindle on clockwise at 1200 RPM (M-code sets the spindle; S is a speed parameter)
  • G00 X0 Y0 Rapid move to origin (G-code movement)
  • G01 Z-5.0 F100 M08 Cut down to Z-5 at feed 100, turn on coolant (G-code motion + M-code machine function in the same line)
  • G01 moves follow Cutting a square at the set feed rate
  • G00 Z25.0 M09 Rapid retract, turn off coolant
  • M05 Stop spindle
  • M30 End and rewind program

This pattern setup M-codes, movement G-codes, cleanup M-codes repeats in almost every CNC program you'll encounter. For a closer look at interpreting code on a lathe specifically, see reading maker codes on a CNC lathe.

What are the most common mistakes people make with G-code and M-code?

Here are real errors that cause problems on the shop floor:

  • Forgetting M03 before a cutting move. If the spindle isn't running, the tool will just drag through the material and break.
  • Missing M08 (coolant). Dry cutting materials like aluminum or stainless steel without coolant leads to poor surface finish and shorter tool life.
  • Confusing G90 and G91. Absolute (G90) uses coordinates from a fixed origin. Incremental (G91) uses coordinates from the tool's current position. Mixing them up sends the tool to the wrong place.
  • Using codes from a different machine's manual. A code that's safe on one controller might trigger a tool change or spindle command on another.
  • Skipping G43 (tool length offset). Without it, the machine doesn't know how long the tool is, and Z-depth will be wrong possibly cutting into the table or missing the part entirely.
  • Not ending with M30. The program won't rewind properly, and the next time you hit cycle start, the machine may behave unexpectedly.

What helpful tips make working with maker codes easier?

These habits will help you avoid mistakes and build confidence with CNC code:

  • Always dry run first. Run the program with the spindle off and the tool well above the workpiece to watch the toolpath without risk.
  • Use single block mode. This lets you step through the program one line at a time so you can verify each move before the machine executes it.
  • Keep a printed cheat sheet at the machine. Having the most common G-codes and M-codes visible saves you from guessing mid-program.
  • Comment your programs. Adding notes in parentheses like (DRILL HOLES) or (FINISH PASS) makes programs much easier to understand later.
  • Learn the codes your specific machine uses first, not a generic list. Start with the controller and brand you actually work with.
  • Use a consistent program structure. Setup block, tool changes, cutting moves, cleanup keeping this order the same every time reduces errors.

Using a monospaced typeface like Courier in your text editor can also make it much easier to visually scan code and spot errors, since every character lines up in neat columns.

Where should you go from here?

Understanding the difference between G-code and M-code is foundational, but it's only the start of becoming comfortable with CNC programming. The more programs you read and write, the more these codes become second nature. Start simple, practice on safe setups, and build from there.

CNC maker code quick-start checklist

  • Know the top 10 G-codes your machine uses (G00, G01, G02, G03, G17/G18/G19, G20/G21, G28, G40/G41/G42, G90/G91, G96/G97)
  • Know the top 10 M-codes your machine uses (M00, M01, M02, M03, M04, M05, M06, M08, M09, M30)
  • Check your machine's manual for any non-standard codes before running an unfamiliar program
  • Dry run every new program in single block mode before cutting material
  • Keep a code reference nearby at your machine a printed sheet or digital file works
  • Comment your programs so you (or someone else) can read them later
  • Practice reading programs line by line, identifying which codes are G-codes and which are M-codes

Take one program, read it block by block, and label each command. That single exercise builds more understanding than memorizing a chart. Start there, and you'll quickly get comfortable with both G-code and M-code working together in your CNC programs.