G and M Codes: Understanding the CNC Programming Languages

Do you want to learn about programming CNC machines? This article will explain everything about g and m codes, CNC machining's programming languages.

Leon Huang: Rapid Direct Author

G and M Codes: Understanding the CNC Programming Languages

CNC machining is one of the core processes in manufacturing. Its machining processes 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 these 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, CNC machining depends on these two codes. However, what is the exact functions of these codes? How they exert their effects on the CNC machine? This section will answer these questions and also highlight the differences between both codes.

What is G Code?

Although the official name of the code is RS-274D, most people refer to it as G code. This is because most of the commands in this code start with the letter G. The ‘G’ in G code stands for geometry. The G code is written in an alphanumeric format and is responsible for the movements of these machines. It tells the machine where to start, how to move, and when to stop when fabricating a part. Although this seems simple enough, G code programming can be quite complicated for machinists. This is 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. This is because errors in the command can lead to serious problems in parts production.

While G is the most common letter in G code CNC programming, it is not the only letter used. Other letters also have functions in the code, and these letters tend to have the same functions regardless of the company’s format. Examples of some letters in the G code includes:

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.

A typical example of a CNC machining program

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 CNC programs using G and M codes, 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 productions 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 code that the machine understands. With CAD (computer-aided design) and CAM (computer-aided manufacturing) software, code generation for machine functions has become very easy.

A CNC machine operator’s monitor

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.

A code generating software at work

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.

G Code and M Code List

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

RapidDirect G Codes List – G Codes for CNC Lathes

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 Codes 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.

RapidDirect understands that you want excellent 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.

RapidDirect Machining Operations

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.

Let us handle your coding and CNC machining needs today, and you’ll never regret the decision.


Q: Do all CNC machines use G code?

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

Q: How many G codes are there?

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

Share on social media...



Design for Assembly and its Use in Product Manufacturing

One of the problems in product manufacturing is the problem of translating what consumers need […]

19 Apr

Design for Assembly and its Use in Product Manufacturing
Distributed Manufacturing: Is this the Future of Part Production?
15 Apr

Distributed Manufacturing: Is this the Future of Part Production?

Traditional manufacturing can be a very difficult process. Learn why distributed manufacturing is the best alternative for modern production.

Engineering Requirements Document: Useful Tips for Writing A Good One
15 Apr

Engineering Requirements Document: Useful Tips for Writing A Good One

The engineering requirements document is important for successful projects. Find out various criteria and tips for writing the best document.

Optimize Your Heat Sink Design: Principles and Four Practical Tips
09 Apr

Optimize Your Heat Sink Design: Principles and Four Practical Tips

Heat sinks are important components of electronics devices. Learn how to optimize your heat sink design for optimal performance

Get Your Parts Into Production Today

Only 4 simple steps to complete your project. Follow the instruction and give it a try!

file upload icon

Upload Files

arrow down
quotation icon

Get Quotation

arrow up
place order icon

Place an Order

arrow down
start production

Start Production