Stecker Machine Blog

What is G-Code? What is M-Code? What Do They Mean in CNC Machining?

07/13/2021 | Dan Karbon and Tom Herr

Two workers using G-code to program a CNC machine

As its name clearly states, CNC (computer numerical control) is automated control of machining tools using a computer. Yet, that doesn’t just happen; it requires the right coded programmed instruction.

So, what is G-code? What is M-code? And how do G-code and M-code programming actually work? This article explains each one, but first let’s touch on what programming actually is.

A program is a sequence of codes and data that tells the CNC machine what to do. The programmed codes, along with the right tooling in a CNC machine center, allow for correct and repeatable part manufacturing.

If you’re interested in the programming process, read this article.


As a CNC machine runs, the code is displayed on the operator’s monitor and is seen scrolling along, being read and acted upon by the machine.

What is G-Code — The Master of Action

Simply put, G-code is a software programming language used to control a CNC machine. And, even with complex CNC machining, G-code is written in a straightforward, logical way.

A “G” is followed by a number, which is a command to change geometry. For instance, “G00” is a command for rapid movement. It moves the tool to part geometry at the maximum rate and is generally used to move the tool and part from near to far or vice versa.

Here are some examples of G-code:

  • G00: Rapid move to specific coordinate position
  • G01: Linear feed move
  • G02/G03: Clockwise/counterclockwise feed move

These codes, such as G00, are followed by a geometric location. The geometry is conveyed with coordinates on axes, so an “X” coordinate and a “Y” coordinate on a plane.

Let’s break down “G00G58X-120.Y-5.” You know the “G00” means a rapid move. “G58” specifies work coordinates, changing geometry relative to work offsets, like part origin. “X-120.” means to go to “-120mm” on the X axis, and  “Y-5.” means to go to “-5mm” on the Y axis.

G-code can repeat an action indefinitely until it is stopped. For example, G01 followed by a sequence of X, Y, or Z positions, means all linear feed moves occur at G01. A typical machining example is face milling, where the sequence of positions is used to machine the part’s face contour.

All of these coordinates in G-code allows the CNC machine to consistently perform and also be flexible enough to produce different parts.


HorizontalMachineAxesThe X and Y axes position the spindle precisely to perform a cut or other function.

What is M-Code — The Organizer of Functions

M-code controls miscellaneous functions, and typically these can be thought of as non-geometry machine functions. Examples include spindle rotation start and stop, coolant on and off, pallet change, etc. Some of these functions may vary by the specific machine.

Here are some examples of M-code:

  • M00: Stop whatever function the program is currently doing
  • M03: Rotate spindle clockwise
  • M08/M09: Flood coolant on/off

Similar to G-code, M-code consists of an “M” and a number. Unlike G-code, however, M-code can be more specific for different machines and is more customizable. Both types are needed to have a CNC machine perform its functions.

Like any language, CNC has many other aspects to it. This chart is just a sampling of all the program lines that could be included in the code.

Program Line Explanation
N102M6T2 Block number 102, Tool change to tool 2
(3" FACEMILL) Comment - not read by machine, but tells operator engineer tool 2 is a 3 inch facemill
M82 Pallet A Check
IF[#10002+#11002LT150.]GOTO2001 Tool height min check
IF[#10002+#11002GT154.]GOTO2002 Tool height max check
G00G90B267. Rapid, absolute positioning, Pallet rotates to B 267 (degrees)
M08 Coolant on
(PART B) Comment

Some examples of code programming lines and what they mean. Anything in parentheses is not read by the machine. These are comments for the programmer and operator.

Other Commands. Other Machines.

In addition to G-codes, M-codes, and the X/Y/Z axes positioning, other commands are used in CNC machining. An M03 spindle rotation start command needs an “S” command, which controls spindle speed in rpm.

Here’s how that gets added to our current code sample: G00G58X-120.Y-5.M03S3820.

The “S3820” indicates a spindle speed of 3820 rpm. So far, the program has set the geometry and the spindle speed.

Other possible functions:

  • F: Feed rate
  • T: Tool number
  • H: Tool length offset call up

Many other alphanumeric designations can be used, depending on the CNC machine’s capabilities.

Keep in mind, the specific G-code and M-code examples we’ve explored are based on horizontal and vertical machining center programs. CNC lathes use G-code and M-code too, but you can expect some different commands and sequences for turning. 

The Power of Software

While machines are frequently programmed directly with G-code, a CNC programmer often uses a high-level computer-aided manufacturing (CAM) programming tool.

Mastercam is a code-producing software that helps programmers create the right code for the project. Standard in the CNC industry, Mastercam takes input from the programmer (everything it needs to do), and it produces the right G-code and M-code programming.

Here’s a simplified example of how Mastercam works. The programmer imports a machine model and the machining fixture. Then, the programmer selects the tools and the tooling paths of where the spindle will go, and the code is produced by the software. The programmer works with the CAD models, but refers to the print for the exact specifications and tolerances.

As powerful as Mastercam is, it could require two weeks of programming if the part is complex (large size and many tools needed), yet it could only take two hours for a simple part. Obviously, a 3-tool job is simple compared to a 100-tool job.

Mastercam software demo

Mastercam software shows a visual of the entire project; at left are folders containing the code needed to perform various functions.

Who Needs to Know G-Code and M-Code?

Depending on your job responsibilities, maybe reading this article is all you need to know about CNC programming using G-code and M-code. Or, you may need to dig deeper to properly execute your daily duties.

Within a CNC machine shop, knowing something about code is helpful for many positions. Shops with a good internal training program will explain the basics of coding to new employees during the orientation process. Others may include it as part of on-the-floor training for operators.

Depending on the part and process, the CNC machine center and program run repeatedly with limited interaction. The operator loads, unloads, inspects, deburrs parts, and packages the part.

In a perfect world, once the correct CNC code is programmed, it’s done, and no edits are needed on the floor. Many projects, however, require slight adjustments, like adjusting work offsets and tool wear.

Machine controls are locked to prevent accidental changes. Editing is possible on the shop floor, but it requires a key to enable. Editing is mostly reserved for experienced engineers, setup pros, and supervisors.

So, you’ve cracked the code! Well, at least you know what G-code and M-code are, and you know some other related commands. Maybe now you’re considering using the expertise of a CNC shop.

Our guide covers nine scenarios that may (or may not) result in working with a CNC machine shop. To get When Do You Know It’s Time to Work With a High-End CNC Machine Shop?, just click the image below.New call-to-action

Dan Karbon and Tom Herr

About the Author

Dan makes sure machines at Stecker Machine run fast. He optimizes cutting tools and programs for peak efficiency, corrects any manufacturing issues, and designs and selects advanced tooling for new jobs and product improvements. Tom is Stecker Machine's Mastercam expert. He handles all CNC mill programming. He also designs fixtures and supports production.

Related Posts