G and M Codes: Understanding the CNC Programming Languages

CNC machining is one of the core processes in manufacturing that produce intricate and sophisticated products which have applications in various industries. However, these processes would be difficult to execute without the machines that drive them. Without the G and M codes, these machines can’t function.

While you would find CNC codes being used together in most machine shops, they are quite different from each other. What are the differences? How do these codes control the functions of CNC machines? To find out the answers to these questions and more, read this article to the end.

What is G Code and M Code?

As mentioned earlier, G and M codes are required for CNC machining. However, what is the exact function of the two CNC letters program? How do they control CNC machines? This section will answer these questions and also highlight the differences between both codes.

What is G Code?

G code (also RS-274D) is the most popular CNC programming language. Most G code commands are in alphanumeric format and start with G which stands for geometry. G-code is responsible for the movements of CNC machines, telling the machine where to start, how to move, and when to stop when fabricating a part.

However, G code programming can be quite complicated for machinists because different machines read G codes in different formats. Most machines’ difference is in the presence or absence of spaces between commands and in the number of zeros between the letter and number in the commands. For example, a machine might use G3 while another uses G03. Machinists must always be conversant with the type of machine they’re using. Otherwise, errors in the command can lead to serious problems in parts production.

While G is the most common letter in G codes CNC programming, it is not the only letter used. Other letters also tend to represent different commands. Examples of some letters in the G code include:

A: It directs the tool around the x-axis.

R: It gives the radius of the arcs the machine makes.

X, Y, Z: These three values indicate the tools’ position in three dimensions – X and Y represent the horizontal and vertical dimensions, respectively, while Z represents the depth.

I and J: Both values designate the incremental center of any arc the machine makes.

N: N gives the line number.

The code also uses other letters which depend on the machine’s capabilities.


What is M Code?

Like the G code, the M code also begins with the letter ‘M.’ The M code is a set of auxiliary commands that control all the machine’s non-geometric actions. Machinists refer to the code as miscellaneous codes as it controls non-cutting actions such as stopping programs, flooding the machine with coolants, and shutting it off after the temperature drops.

When setting up the CNC letters program using G and M codes, the M code should only have one command per block of information. This is because they mainly turn the machine on and off. Therefore, using them multiple times in one block could cause program problems.

Just like G codes, M codes also vary between different machines. Some machines allow the omission of the zero between the letter and number, while some do not.


Although machinists use G and M codes concurrently in any CNC machining process, they differ. Here are some examples of how both CNC codes differ:

  1. G code commands tend to differ in most CNC machines, while M codes are similar in most CNC machines.
  2. G codes direct the machine’s motion and function, while M codes direct the operations outside movements.
  3. The G code activates the CNC machine while the M code activates the machine’s programmable logic controller.

How does CNC Programming Control CNC Machines?

Before the advent of computers, machinists used cards or tapes to control machine movements. They punched holes on these cards in a specific order to create the codes. While this was also effective at the time, it was quite tedious. Also, these cards were prone to damage or getting lost in the machine shops. This led to several problems in production at the time.

When machinists started using computers for numerically controlled machines, they still came across a few problems. This was because they had to input the codes manually. This would, of course, be very tedious when they were making quite sophisticated parts that required a lot of instructions.

However, with much more advanced computers and software, machinists can simply instruct the software on what to do. The software will then generate the G codes and M codes that the machine understands. With CAD and CAM software, code generation for machine functions has become very easy.


To start the process, the programmer needs high-level computer-aided software. The programmer then imports the machine model and the machining fixture into the software. He/She also selects the tools and the tooling paths of the spindle. The software then generates the G and M codes that the machine needs to function.


This is the summary of the process through which CNC programming controls the function of CNC machines. Although the process above seems simplified, it could take weeks to generate the codes for a sophisticated part.

Try RapidDirect Now!
All information and uploads are secure and confidential.

G Code and M Code Commands List

This section will illustrate different examples of basic G and M codes and what they mean. Part of these codes means the same thing in G and M codes, although other units may vary. Examples of these codes include:

G Code List – G Codes for CNC Lathe

G00: Rapid Position Motion

G01: Linear Interpolation Motion or Linear Motion, Chamfer and Corner Rounding – Modal

G02: CW Circulation Interpolation Motion – Modal

G03: CCW Circular Interpolation Motion – Modal

G04: Dwell (P) P=Seconds. Milliseconds

G05: Fine Spindle Control Motion (Live Tooling) – Optional

G09: Exact Stop

G10: Programmable Offset Setting

M Codes for CNC Lathes

M00: Program Stop – Modal

M01: Optional Program Stop – Modal

M02: Program End – Modal

M03: Spindle on Forward (S) – Modal

M04: Spindle on Reverse (S) – Modal

M05: Spindle Stop – Modal

M08: Coolant On – Modal

M09: Coolant Off – Modal

M10: Chuck Clamp – Modal

G Code List for CNC Milling

G00: Rapid Motion

G01: Linear Interpolation Motion

G02: CW Interpolation Motion

G03: CCW Interpolation Motion

G04: Dwell

G09: Exact Stop

G10: Programmable Offset Setting

G12: CW Circular Pock Milling (Yasnac)

G13: CCW Circular Pock Milling (Yasnac)

G17: XY Plane Selection

M Codes for CNC Milling

M00: Program Stop

M01: Optional Program Stop

M02: Program End (Setting 39)

M03: Spindle On, Clockwise (S) (Setting 144)

M04: Spindle On, Counterclockwise (S) (Setting 144)

M05: Spindle Stop

M06: Tool Change (T) (Setting 42, 87, 155)

M08: Coolant On (Setting 32)

M09: Coolant Off

M10: 4th Axis Brake On


Using CNC machines is one of the most important processes in CNC machining. However, these machines cannot function without G and M codes which instruct them on what to do. Understanding how to generate these codes is vital to the CNC machining process and successful parts production. Mastery of these codes gives you a head start in your CNC programming career.

RapidDirect CNC Machining Services

Need a company to handle your CNC machining project? Look no further because RapidDirect has all the technical expertise you’ll ever need. With over two decades of manufacturing experience, we have experienced staff in all areas of the manufacturing process.

Try RapidDirect Now!
All information and uploads are secure and confidential.

RapidDirect understands that you want excellent CNC machining service and you don’t want to spend much to get it; therefore, we offer precisely that. With us, you will get the exceptional service you crave at a highly competitive price. Our service is affordable, and we don’t compromise quality at any stage.

Once you assign your project to us, you do not have to worry about the coding or any CNC machining needs. We take full responsibility for the project and keep our communication lines open at all times. We also give experienced recommendations that can make the project a success.


Do all CNC machines use G code commands?

Yes! All CNC machines utilize G code CNC programming. This instructs the machine on when, where, and how to move.

How many G codes are there?

There are about a hundred G codes, Turning and milling machines each have separate codes, although both machines share some of these codes.

Featured Posts

    Share this Post:

    Let's Start A New Project Today