G and M Code Explained: Differences and Uses in CNC Machining

CNC command codes are core to modern manufacturing processes. CNC machines operate on these CNC programs, hence why manufacturers need them in different manufacturing processes. CNC G and M codes are commonly used software programming languages in CNC machining, depending on the tasks that the machine performs.

However, these CNC programming codes can be complex to generate or implement. Hence, understanding information about these codes helps achieve the ideal results in CNC machining processes.

This article will discuss everything you need to understand about CNC programming language and typical examples of G and M code in CNC machining. You’ll also learn about the difference between CNC G and M codes, and other command codes in CNC machining.

cnc programming machining

What is CNC Programming Language?

CNC programming language is a series of instructions that guides CNC machines through different operations to create highly accurate and precise parts. Since CNC machines are automated, they can perform various tasks like milling, cutting, and drilling by following a generated CNC machine code.

Geometric/general-code (G-code) and Miscellaneous-code (M-code) are the typical CNC code examples used in CNC machining. However, you must understand the CNC machine’s capabilities, the chosen material, and the final design to generate the machine program. Expert CNC programmers often rely on specialized CAM/CAD software to develop G and M codes that direct the CNC machine to execute desired tasks.

Why CNC Programming Codes are Important?

Machinists could only use cards and tapes to control machines before the invention of computers. They would punch these cards in different orders to create codes. Although the method was effective, it was also labor-intensive. Besides, these cards often get damaged or lost. Consequently, there were several complications in the production procedure in the days before computers.

The advent of computers and computer numerically controlled machines remains an effective solution to most production issues. However, these computer numerical control systems require CNC programming codes to complete any task. Manual coding can be labor-intensive, especially when machining parts with complex features.

Fortunately, modern machines follow a series of instructions loaded onto the system by machine operators. The CAM and CAD software have optimized efficiency and accessibility in code generation and machine operation. Once the machining parameters are set, the software generates the necessary programming codes to ensure the effective operation of CNC machines.

cnc g code for turning

What is a G Code?

Geometric code (RS-274D) is the most common CNC programming code. This CNC machine code comprises alphabetic codes that symbolize specific commands for the machine. Most G-code programs are written in alphanumeric format, beginning with G, which means geometry. The CNC G codes dictate the machine’s movement, feed rate and cutting speed, tool changes, and other important parameters.

Every CNC machine possesses its unique set of G-code instructions. Hence, you must program the code according to the machine type and the preferred results. The presence or absence of spaces between the G-code commands marks the differences in the G-code formats. For instance, a machine might use G03 while others are compatible with G3.

Hence, as a CNC machine programmer, you must be conversant with the machine type you use to generate the right commands. Otherwise, command errors may result in severe complications in parts production.

Functions of CNC Machining G Code

As discussed earlier, CNC G codes dictate the movement of the machine. It uses different letters and numbers to give the CNC machine directions on how and where to move. We will discuss the functions of these letters in the CNC machining G-code below.

  • A: Directs the machine tool around the x-axis.
  • B: Number with B indicates the rotational value around the y-axis.
  • C: An auxiliary axis that rotates around the z-axis. It derives its positional value from the C value.
  • D: Indicates how much the system offsets the tool diameter.
  • G: Indicates when the tool needs to move and stop.
  • I and J: Specify the incremental center of any arcs made by the machine.
  • N: Gives the line number.
  • L: Gives the system a value to show repeating operations and the required times to loop.
  • P: Indicates program jumps in time or a delayed time.
  • R: Gives the radius of the arcs made by the machine.
  • S: Instructs the machine on how quickly it should move the spindle.
  • T: Instructs the machine on which tool to use.
  • X, Y, Z: Indicate the tool’s position in three dimensions – X represents horizontal, Y represents vertical, and Z represents depth.

m and g code for cnc machining

What is an M Code?

M code or miscellaneous code encompasses a set of auxiliary instructions key to controlling the miscellaneous or non-geometric functions of a CNC machine. This CNC machine code governs essential operations like activating the coolant system, halting the program, and powering the machine post-operation.

There is only one M code in each block of program information. As a result, using the right M codes in CNC programming would be best to ensure seamless operation because M codes activate or deactivate different machine functions. Operational errors and programming conflicts may occur if these commands overlap within a single block.

CNC M codes are similar to G codes because they vary across different CNC machines. The differences can occur in the codes’ numerical formatting, including the exclusion or inclusion of leading zeros. For example, a CNC machine might use M3 while another uses M03. Hence, you must be conversant with the specific coding requirements of the machine to ensure smooth and error-free machining operations.

Functions of CNC Machining M Code

The M code dictates all the non-geometrical machine functions. In other words, it controls the miscellaneous functions of the machine. For example, the M code controls when the spindle rotation starts and pauses, the coolant on and off, and the pallet changes.

When the CNC machine needs to flood the system with coolant, shutting it off after a decline in temperature, the CNC M codes control these machine operations. The M-code language depends on the CNC machine, as with G codes. However, m codes are more specific and highly customizable for different machines than G codes.

cnc machine code

Common Examples G and M Codes in CNC Machining

Expert machine programmers use CNC G and M codes to prepare CNC machines for various manufacturing processes. Here are commonly used CNC G and M code examples.

Common G Code used in CNC Machining

G codes in CNC machining ease complex operations with standardized codes, ensuring operation performance and consistency.

  • G00 – Rapid Positioning: This code commands the machine tool to move to a specific coordinate at maximum speed. Usually, it positions the machine tool without cutting material, enhancing efficiency in non-cutting movements.
  • G01 – Linear Interpolation: The G01 command code is one of the standard G codes used in CNC machining. It directs the tool to follow a straight path between two points at a programmed feed rate. Machinists often use this code for straight-line cutting operations.
  • G02 – Circular Interpolation-Clockwise: This code creates circles and arcs by directing the tool along a circular path in the clockwise direction. It helps in circular machining tasks and facilitates precise movement to a preferred endpoint.
  • G03 – Circular Interpolation-Counter-Clockwise: This code mirrors the G02 code, but the tool moves along the circular path in a counter-clockwise direction. It is well-suited for making circles and arcs that need a counter-clockwise approach.
  • G04 – Dwell: The G04 code instructs the CNC tool to pause at its present location for a particular period temporarily. The dwell is best used when the cutting tool needs to cool down, or the spindle needs to attain a preferred speed.

A list of function G Codes includes:

Command CodeCategoryFunctionFor Milling or TurningModal
G03CCW (Circular Interpolation)Make arcs and circles (Counter Clockwise)BothYes
G04DwellPause for a defined durationBothNo
G21Unit SystemMetric SystemBothYes
G20Unit SystemInch SystemBothYes
G19Plane SelectionYZ Plane SelectionTurningYes
G18Plane SelectionXZ Plane SelectionTurningYes
G40Cutter CompensationCancel Cutter CompensationMillingYes
G91Positioning and ModesIncremental ModeBothYes
G96Speed and FeedsRegular surface speedTurningYes

Common M Code used in CNC Machining

Here are some of the M codes used in CNC machining, along with their functions:

  • M00 – Program Stop: This code instructs the machine to stop the CNC program temporarily. It usually requires operator intervention to continue the program.
  • M02 – Program End: This code commands the CNC machine to end a program. The control stops after executing this program, and the machinist has to restart or reset the CNC machine.
  • M03 – Spindle On (Clockwise): It initiates the spindle rotation along the clockwise direction. A speed command (S) often accompanies it to adjust the spindle speed.
  • M04 – Spindle On (Counterclockwise): The same as M03, the machinist uses this code to start the spindle and rotate it in the counterclockwise direction.
  • M05 – Spindle Stop: Stops the spindle rotation. Machinists often employ this command to keep the spindle stationary for specific operations, such as tool change.

Below is a list of function M codes:

CodeCategoryFunctionFor Milling or TurningModal
M00Program ControlProgram StopBothNo
M02Program ControlProgram EndBothNo
M03Spindle ControlSpindle On, ClockwiseBothNo
M04Spindle ControlSpindle On, CounterclockwiseBothNo
M05Spindle ControlSpindle StopBothNo
M06Tool ChangeTool ChangeBothNo
M08CoolantCoolant on or floodBothNo
M09CoolantCoolant OffBothNo
M19Spindle ControlChange Spindle OrientationsMillingNo
M42Auxiliary FunctionsHigh Gear SelectTurningNo

Differences Between CNC G and M Codes

As mentioned above, CNC G and M codes have certain similarities, but they differ in their uses for controlling CNC machines. The G code directs the CNC machine’s motion and function. In contrast, the M code oversees operations unrelated to movement, such as turning the spindle on and off, changing CNC cutting tools, stopping programs, and activating coolant systems. CNC G codes relate to geometry and are helpful in product design.

On the other hand, M codes relate to machine functions and are useful in different miscellaneous operations. Furthermore, the G code activates the CNC machine while the M code activates the programmable logic controller (PLC) of the CNC machine.

cnc language code for prototyping

Other Command Codes in CNC Programming

Although G and M codes are the widely used command codes in CNC machine programming, there are other CNC programming codes that play important roles in machining processes.

Dwell Codes

Dwell codes pause the operation of the machine at a specified period and location. Machine programmers indicate the dwell time using the P-code, indicating the dwell time in seconds. For instance, G04 P2 would make the machine to pause for 2 seconds.

F Code (Feed Rate)

The F code specifies the required feed rate at which the tool should move along the programmed tool path. The value accompanying the F code indicates the feed rate in units per minute. For instance, the F200 puts the spindle speed at 200 units per minute.

S Code (Spindle Speed)

With the S code, machinists specify the required spindle speed for CNC machining operations. The value after the S code indicates the spindle speed in revolutions per minute (RPM). For instance, S 1000 program the spindle speed to 1000 RPM.

T Codes (Tool Selection)

T codes define the required tool number or tool offset used for machining. These codes specify the appropriate tool in the tool magazine or tool holder to engage for specific operations. For instance, T01 picks tool number 1, T05 chooses tool number 5, and it continues in that order.

Started Expert CNC Machining Services with XinCheng

XinCheng Machining is a modern CNC machine shop with different multi-axis CNC machines. Our expert machinists and engineers have in-depth knowledge of complex CNC programming languages. We can offer various CNC machining services and surface finish options, no matter whether CNC prototyping or complex machining projects, we can ensure high-quality products and keep CNC machining costs within your budget.


The use of CNC machines is one of the most essential procedures in CNC machining. However, you should note that these machines operate based on programming languages like CNC G and M codes, instructing them on what to do. Hence, mastering these CNC programming codes helps you understand why they are essential in CNC machining processes and how to achieve successful parts production.


Why is the importance of CNC programming in machining?

CNC programming is useful in machining operations because it guarantees higher levels of automation, efficiency, productivity, and precision. This technology facilitates the creation of complex parts with superior quality and high speed.

Why do programmers use tool offsets in CNC programming?

Machine programmers use tool offsets to make up for tool lengths and diameter variations. Hence, it adjusts the tool’s position to ensure accurate machining.

What are the common methods of CNC programming?

Standard methods programmers use to create CNC programs for manufacturing processes include manual programming, programming in operation, and programming with CAD/CAM systems.

Scroll to Top