What Is G Code?

The G-code or Geometric Code is the programming language CNC machines use to define the movement of their tools. It is a standardized control command that instructs the modern CNC machines or industrial robots how to move the tool from one position to another along the (X, Y, Z) Cartesian coordinate system.
A line of G-code comprises multiple instructions that the machine can read and understand, and is known as a block. It comprises a combination of letters and numbers, and each conveys a specific command. For instance, a block might consist of the G01 command (to move the tool in a straight line) or the M08 command (to activate coolant on.)
Aside from the location instructions or commands, G-code commands give other input, including start point, stop point, wait time, feed rate, tool length offset, speed, and angle in the rotational axis. More so, it enables the precise control of CNC (Computer Numerical Control) machine tools to produce identical parts with high accuracy and consistent results.
Nonetheless, G-code can be complicated since different machines read G-code in varying formats. The difference in the code most machines can read lies in the number of zeros between the letter and number in the codes, and in the absence or presence of a space between command codes. For instance, a machine might use G03 while another uses G3.
Basic G-Code Commands Used in CNC Machine Programming
G-code is a commonly used CNC programming language at the core of unmatched manufacturing flexibility and precision of the CNC machining process. Here are the standard G-codes in programming:
- G00 – Rapid Positioning (Linear): This command code instructs the machine tool to travel quickly to the specified position at the highest speed possible without cutting. The primary function of this code is to instruct the rapid movement of the cutter without cutting the workpiece, which boosts the machine’s productivity for non-cutting tasks. While the rapid move command doesn’t usually doesn’t require a feed rate, it needs location coordinates in the X, Y, and Z axes.
- G01 – Linear Interpolation: This is one of the most used codes in CNC machining. G01 instructs the machine tool to travel at a predefined feed rate in a straight line between two locations.
- G02 – Clockwise Circular Interpolation: G02 directs the cutting tool to move along a clockwise arc path to create circles and arcs. This command guarantees an accurate move.
- G03 – Counterclockwise Circular Interpolation: This command directs the tool in a counterclockwise circular path. Its function is to create circles and arcs that call for a counterclockwise rotation.
- G04 – Dwell: This command tells the CNC machine to hold for a particular amount of time at its present position. The dwell control code is particularly helpful when a spindle needs to attain the proper speed or a cutting tool needs to cool down.
- G09 – Exact Stop: It stops the cutting tool temporarily at a corner and starts it again to create a perfectly sharp corner. While conventional machining produces rounded corners because of the inertia of the cutter, this G-code command stops the cutting tool to create a sharp corner when it is necessary to part design.
What Is M Code?

M code is a programming language used to control the auxiliary functions of CNC machines. These codes begin with the letter ‘M’ and are known as miscellaneous codes. These auxiliary commands manage essential non-geometric CNC machine operations, including activating coolant systems, halting the program, and shutting the machine down after a completed operation.
There is at least one M code embedded in each block of the program in CNC programming. It is imperative to add M code with precision because overlapping commands within a single block can result in programming conflicts and costly operational errors.
CNC machines read M codes in different formats as well. The variance resides in the difference in code formats, including the omission or inclusion of leading zeros. i.e., a machine might recognize an M-code as ‘M03’ while another recognizes ‘M3’. For this reason, you must have a clear understanding of the specific coding requirements of the machine for error-free and smooth machining processes.
Sam
The founder of AT-Machining has 30 years of CNC experience, dedicated to solving complex design and machining challenges for customers.
🔗 Best Practice
Always include safe start blocks: reset modes, define work offset, tool length, spindle state, and feed units before motion, preventing inherited settings causing unpredictable machine behavior.
Basic M-Code Commands Used in CNC Programming
- M00 – Program Stop: In CNC machining, M00 is the command code that halts the CNC program momentarily. It usually requires frequent operator intervention to continue machine operations or restart the program.
- M02 – Program End: This command instructs the machine to terminate the CNC program. It will halt after running the code, and the machine operator might have to reset or restart the unit.
- M03 – Spindle Start (Clockwise): It instructs the machine to start the spindle in a clockwise rotation. CNC programmers often use a speed command (S) after the M-code to set the spindle speed.
- M04 – Spindle Start (Counterclockwise): M04 is used to initiate the spindle in the counterclockwise motion.
- M05– Spindle Stop: It commands the machine to halt the rotation of the spindle. It is commonly used in situations when the spindle must maintain a stationary position for procedures such as tool change.
- M08/M09 –Coolant On/Off: The M08 instructs the machine the activate coolant, while the M09 code commands the machine to turn off coolant flood.
How Does CNC Machine Programming Work?

Machining experts controlled machine movements with tapes or cards before the introduction of computer control in manufacturing. These machinists punch holes in these cards in a specific order to generate the codes. This approach proved tiresome even though it worked well at the time.
More so, these cards resulted in several manufacturing issues at the time since they are vulnerable to damage and often get misplaced in the machine shops. The widespread use of cutting-edge computers and software has completely transformed the manufacturing industry, taking over machine control and enabling automated production.
Hence, machinists now input instructions into computer-aided design (CAD) software, which automatically generates G and M codes necessary for machine operation, making code creation and machine functions more efficient and accessible.
The programmer begins the first step in CNC programming by using advanced computer-aided software to make a digital design, which the CAM software translates to program code (readable instructions) for the machine.
The software creates the necessary G- and M-codes for the CNC machine from the design for CNC machining. The CNC programmer feeds the machining code into the machine and initiates the cycle for the part production.
Yuki
Technical sales engineer at AT-Machining with 12 years of CNC experience, specializing in materials selection and surface treatment solutions.
🔗 Best Practice
Use consistent, documented subprograms and macros for repetitive operations, with clear variable scopes and defaults; validate with dry-runs to avoid unintended modal interactions across calls.
Methods of Programming CNC Machines Using G- and M-Codes
CNC programming involves creating a series of instructions or control commands used to guide a computer-numerically controlled machine to execute precise and complex machining operations, including milling, drilling, electrical discharge machining, etc., to create high-quality parts.
Manual Programming
Manual programming is a fundamental CNC programming technique best used in situations where you have an in-depth understanding of the capacity and functions of the machine. It is best used for machining simple parts.
When programming manually, the programmer generates geometric and technological data required to complete the alphanumeric program based on personal experience and knowledge, application of programming instruments, available information on machines, tools, equipment, and processing modes. Although this CNC programming technique instills proficiency, it is time-consuming and prone to human error.
Programming in Operation (Conversational Programming)

This is a simplified style of programming a CNC machine, since it allows the use of plain language prompts, and it is usually less code-heavy and user-friendly. Due to the easier accessibility of the built-in interface of the machine control panel, some manufacturers prefer programming directly on the machine.
Programming in operation or shop-floor programming is an entirely new CNC programming method, even though the possibility of immediate programming on the machine in the Manual Data Input (MDI) mode was evident from the inception of the application of CNC (Computer Numerical Control).
The control unit supports the prompt programming of the machine in operation due to the integrated additional features. It permits an interactive dialogue with the operator using a keyboard and a graphical interface capable of creating simple processing contours and typed cycles for processing.
As such, extensive machine knowledge is not required because the system automatically generates the necessary control commands in the background as you provide tool settings and dimension information. Depending on the entered queries, the machine’s control unit executes necessary tool path calculations and automates the creation of G-code.
Programming Using CAM/CAD Systems
The geometry of parts used in applications in the military, aerospace, automotive, and other sectors is highly complex, with second-order surfaces that demand a multi-axis machining process. Complex computer programs for processing such surfaces are typically tedious and time-consuming since they require several thousand blocks.
Thus, using a computer for technological preparations is necessary, particularly for programming of CNC machines for fabricating parts with complex geometry on the basis of G – and M-code. CAD and CAM programming can be regarded as computerized CNC programming. CAM and CAD integration has significantly improved numerical control programming, particularly in programming simplification.
The CAD models provide the detailed digital model of a part, CAM software handles the defining of machining parameters, toolpath calculation, and simulation, while the programmer must engage the system actively to include additional technical information, including clamping, processing modes, and cutting tools. More so, CAD/CAM integration and Computer-Aided Process Planning (CAPP) provide an efficient solution for these requirements.
Max
Max is a technical sales expert with 10 years of engineering experience who has solved hundreds of complex technical difficulties and challenges for customers.
🔗 Best Practice
Separate cutting and non-cutting motions with explicit G-code modes, controlled approach feeds, and verified clearance planes; interlock M-codes to enforce coolant, clamping, and spindle synchronization.
Differences Between G Code and M Code in CNC Programming
While CNC programmers can manually write these codes, most CNC shops resort to using custom computer-aided manufacturing software due to its efficient automation, fast-tracking the procedure, and eliminating potential human error. Even though G-code and M-code play complementary roles in the CNC machining industry, it is crucial to understand how they differ.
G-code directs the CNC machine’s motion and function, controlling the path of the cutting tool along different axes to make a specific shape according to the predetermined trajectory. It instructs the tool movement in relation to the workpiece. Besides, G-code chooses the work coordinate system as set by the machinist. However, not all types of machines support every G-code command, and those with specialized or multi-axis machining capabilities may require additional G-codes to function as intended.
M-code directs the auxiliary machine functions that don’t involve movements, including coolant control, tool changes, spindle speed on and off, etc. These codes contribute to machine automation and efficient machining operations.
Nevertheless, G code and M code work in tandem in every program. In every CNC program, the M-code must have a corresponding G-code to make a complete CNC programming file.
Conclusion

G and M code commands are pivotal to the execution of different machining operations with optimal precision, repeatable parts production, and unparalleled manufacturing efficiency. Overcoming the learning curve of CNC programming empowers machinists and every team member with the proficiency to optimize productivity, consistency, avoid costly mistakes, and troubleshoot manufacturing processes.
If you need a reputable and certified CNC machining service provider, AT Machining is the standout choice. We provide an extensive range of machining solutions to meet strict industry standards and intricate machining needs. Contact us now. Our expert team has the mastery of the CNC programming fundamentals and can leverage CNC technology to tackle the requirements of complex parts for consistent results and accurate manufacturing. Upload a CAD file to our user-friendly platform for an instant quote for your product design!