What Is G-Code in a CNC Machine — The Short Answer G-code, short for geometric code, is the primary programming language used to control CNC (Computer Numerical Control) machines. It tells the machine exactly where to move, how f...
READ MORE
Content
G-code, short for geometric code, is the primary programming language used to control CNC (Computer Numerical Control) machines. It tells the machine exactly where to move, how fast to move, and what path to follow — translating a digital design into precise physical cuts, drills, and contours on a workpiece. Without G-code, a CNC machine is just a stationary assembly of motors, spindles, and axes with no instructions to act on.
Every single movement a CNC machine makes is driven by a G-code command. Whether you are milling aluminum, turning a stainless steel shaft on a lathe, or routing a wood panel, G-code is the underlying language making it happen. It is also standardized under ISO 6983-1 (also called RS-274), which means a programmer trained on one type of CNC equipment can transfer knowledge to another machine with only minor adjustments for brand-specific variations.
G-code was first developed at the Massachusetts Institute of Technology (MIT) in 1958, and the first standardized version — RS-274 — was published in 1963 by the Electronic Industries Alliance. Over six decades later, it remains the dominant language across virtually all CNC equipment in manufacturing worldwide, from small hobbyist mills to industrial five-axis machining centers producing aerospace components.
G-code is a plain-text language made up of alphanumeric commands. Each line of code — called a block — contains one or more instructions the machine reads and executes sequentially, from left to right and top to bottom, much like reading a book. The format is compact because early CNC controllers had very limited memory, so every character had to carry maximum meaning.
A typical G-code block looks like this:
G01 X50.0 Y25.0 Z-10.0 F200
Breaking this down:
G-codes are modal, meaning once a command is activated, it remains in effect for all subsequent blocks until it is cancelled or overridden by another command. This design reduces repetition and keeps programs shorter. For example, if G01 is set on one line, the machine continues executing linear cutting moves on every following line without needing G01 repeated every time — until a different movement type like G00 (rapid travel) is called.
CNC equipment reads and interprets these commands through its internal controller — a dedicated computer running firmware (such as Fanuc, Siemens, or Heidenhain systems). The controller converts the G-code into electrical signals that drive servo motors, which in turn move the machine axes with tolerances often measured in thousandths of a millimeter.

While hundreds of G-code commands exist across different machine types, a core set of commands covers the vast majority of everyday machining operations. Understanding these is fundamental to working effectively with any CNC equipment.
| G-Code | Function | Typical Use Case |
|---|---|---|
| G00 | Rapid positioning (no cutting) | Moving tool to start position at maximum speed |
| G01 | Linear interpolation (cutting) | Milling flat surfaces, drilling, turning |
| G02 | Circular interpolation clockwise | Cutting arcs and circular pockets |
| G03 | Circular interpolation counter-clockwise | Cutting arcs in opposite direction |
| G04 | Dwell (pause) | Pausing spindle at drill depth to clean bore |
| G20 / G21 | Inch / Metric units | Setting measurement system for the program |
| G28 | Return to home position | Sending axes back to machine zero at program end |
| G54–G59 | Work coordinate offsets | Setting part origin relative to machine datum |
| G90 / G91 | Absolute / Incremental positioning | Switching between coordinate reference modes |
G00 and G01 are by far the most frequently used commands in any CNC program. G00 moves the tool at the machine's maximum rapid speed — which can exceed 30 meters per minute on modern machining centers — without cutting. G01 slows the tool to a programmed feed rate for actual material removal. The ability to switch between these two modes efficiently is central to optimizing cycle time on CNC equipment.
One distinction that trips up new CNC programmers is the difference between G90 (absolute) and G91 (incremental) positioning modes. In absolute mode, every coordinate refers to a fixed point of origin — typically the part's datum or machine zero. In incremental mode, each coordinate represents a distance to move from the tool's current position. Most programs default to G90, but G91 is valuable when repeating identical moves across different locations on a part, such as drilling a row of equally spaced holes.
G-code handles geometry — movement, positioning, and interpolation. But a CNC machine also needs to control non-movement functions: starting and stopping the spindle, turning coolant on and off, changing tools, and ending the program. These are managed by M-codes, which stand for miscellaneous codes.
G-code and M-code run in parallel throughout every CNC program. A complete machining operation typically involves both types working together in every cycle. Consider a basic face milling sequence:
Key M-codes used across most CNC equipment include:
One important difference between G-code and M-code is standardization. While G-codes are largely consistent across CNC equipment manufacturers, M-codes can vary more significantly between machines. An M-code that triggers a pallet changer on one machining center might do nothing — or something unexpected — on a different brand. Always verify M-code functions against the specific machine's documentation before running a program on unfamiliar CNC equipment.
Additional letter-address codes work alongside G and M codes. The S-code controls spindle speed in RPM (e.g., S2500 = 2,500 RPM). The F-code sets feed rate. The T-code selects the tool number. Together, these address codes give the CNC program full control over every aspect of the machining operation.

In modern manufacturing environments, machinists rarely write G-code by hand. Instead, a workflow connects three layers of software to produce ready-to-run G-code automatically:
The engineer or designer creates a 3D model of the part in CAD software. This model defines the exact geometry — dimensions, tolerances, features like holes, pockets, and profiles — but contains no machining instructions yet.
The CAD model is imported into CAM software, where the programmer defines toolpaths — the routes the cutting tool will follow — along with cutting parameters like spindle speed, feed rate, depth of cut, and tool selection. The CAM system calculates all the necessary coordinates and movements required to machine the part.
Once the toolpaths are defined, the CAM software uses a post-processor — a translation layer specific to the target CNC equipment — to convert the generic toolpath data into machine-specific G-code. This step is critical because G-code syntax and available commands vary between controller brands such as Fanuc, Siemens, Heidenhain, and others. A post-processor configured for a Fanuc-controlled machining center will output slightly different code than one configured for a Siemens controller, even for identical operations.
The resulting G-code file — a plain text file typically with a .nc, .tap, or .cnc extension — is transferred to the CNC machine via USB, network connection, or direct RS-232 serial link in older facilities. The machine's controller loads the file, displays the code on the operator's monitor, and executes it line by line when the cycle start button is pressed.
Despite the automation that CAM software provides, a solid understanding of G-code remains valuable for machinists and programmers who need to troubleshoot a program at the machine, make small edits without returning to the CAM system, or optimize a program for faster cycle times.
G-code is not limited to one type of machine. It is the shared language across a broad range of CNC equipment, though the specific commands used and the axes involved vary by machine type.
Milling machines operate on at least three axes (X, Y, Z) and use rotating cutting tools to remove material from a stationary workpiece. G-code controls tool movement across all three axes simultaneously — enabling complex 3D contours, pockets, slots, holes, and surface profiles. Five-axis machining centers add two rotational axes (often called A and B or B and C), allowing the tool to approach the workpiece from virtually any angle. This is especially common in aerospace and medical device manufacturing, where complex curved surfaces must be machined to tight tolerances.
On CNC lathes, the workpiece rotates while the cutting tool moves along two primary axes — X (diameter) and Z (length). G-code commands for turning operations differ somewhat from milling: for example, G96 sets a constant surface speed (CSS) in meters per minute, automatically adjusting the spindle RPM as the tool moves closer to or further from the center of the part. This is essential for maintaining a consistent surface finish when turning parts of varying diameter.
CNC routers use the same G-code fundamentals as milling machines but are typically designed for softer materials such as wood, plastics, composites, and foam. They are widely used in woodworking, sign-making, and furniture manufacturing. Feed rates on routers can be significantly higher than on metal-cutting CNC equipment — often 10,000 to 20,000 mm per minute or more — reflecting the lower cutting forces involved in routing softer materials.
FDM (fused deposition modeling) 3D printers use a derivative of G-code to control the print head's movement across the X and Y axes, the build plate's movement in Z, and the extrusion of filament. The commands are generated by slicing software rather than CAM, but the underlying G-code structure — coordinates, feed rates, and modal commands — is fundamentally the same as that used on metal-cutting CNC equipment.
Electrical discharge machining (EDM), plasma cutting, laser cutting, and waterjet cutting equipment all use G-code to define the cutting path in X and Y. The primary difference from traditional machining G-code is that instead of spindle speed and feed depth commands, these machines use power level, gas pressure, or beam intensity parameters specific to their cutting process.

The real-world impact of G-code on manufacturing goes far beyond just controlling tool movement. It is the foundation that makes modern high-volume, high-precision production possible.
CNC equipment controlled by well-written G-code can hold dimensional tolerances of ±0.005 mm (5 microns) or tighter on precision machining centers. This level of accuracy is impossible to achieve consistently with manual machining, no matter how skilled the operator. Industries such as aerospace, medical devices, and automotive rely on this precision for components where a deviation of a few hundredths of a millimeter can cause part failure.
Once a G-code program is verified and proven, it can produce identical parts indefinitely — whether that means 10 parts or 100,000 parts. Every part follows exactly the same tool path at exactly the same speeds and feeds. This repeatability is fundamental to quality management systems like ISO 9001 and automotive standards like IATF 16949, which require documented, controlled manufacturing processes.
G-code automation allows CNC equipment to run unattended for extended periods. A machining center with an automatic tool changer (ATC) and pallet changer can execute a complex program involving dozens of tool changes, multiple operations, and both roughing and finishing passes — all without operator intervention beyond loading raw material and pressing the cycle start button. Many facilities run "lights-out" machining overnight, where CNC equipment operates fully automated with no staff present.
When a design change occurs, the engineer updates the CAD model, regenerates toolpaths in CAM, and produces a revised G-code program. The updated program can be running on the CNC machine within hours — sometimes minutes — of the design change being approved. This flexibility dramatically accelerates product development cycles compared to traditional tooling-based manufacturing.
A well-structured G-code program follows a logical sequence that ensures safe operation and efficient machining. While there is no single universal format, most programs for CNC equipment follow this general structure:
Comments can be inserted throughout a G-code program inside parentheses, for example: (FACE MILL TOP SURFACE - 20MM ENDMILL). The CNC controller ignores anything within parentheses, but comments are invaluable for helping other operators and programmers understand what the program is doing without having to decode every line of code.
Canned cycles deserve particular attention. These are fixed G-code sequences that compress common multi-step operations — like drilling, boring, or tapping — into a single command. For example, G81 is the basic drilling canned cycle. Instead of writing separate lines to rapid to a position, feed down to drill depth, and rapid back up — repeated for every hole — the programmer simply specifies G81 with the drill depth and feed rate, then lists the X and Y coordinates of each hole. The machine automatically performs the full sequence at each location. This can reduce a 50-line drilling program to 10 lines.
G-code is standardized under ISO 6983-1, but in practice, different CNC equipment manufacturers implement the standard in slightly different ways. The major controller brands — Fanuc, Siemens, Heidenhain, Mitsubishi, and Mazak's Mazatrol — all follow the core G-code standards but add proprietary extensions and variations.
Fanuc controllers dominate the global market and are found on a wide range of CNC equipment from Japanese, Korean, and Taiwanese manufacturers. Siemens controllers (running Sinumerik software) are prevalent in Europe, particularly in Germany. Heidenhain controllers are common on high-precision European machining centers, especially those made by DMG Mori and Hermle. Mazatrol is a conversational programming system unique to Yamazaki Mazak machines that can generate G-code from plain-language inputs.
The variations are usually in areas like:
The practical implication is that a G-code program written for a Fanuc-controlled machining center cannot always be loaded directly onto a Siemens-controlled machine without modification. This is why selecting the correct post-processor in CAM software is so important, and why operators working on unfamiliar CNC equipment should always review a program before running it for the first time — ideally using a simulation or dry run at reduced feed rate.

Even experienced CNC programmers make errors in G-code that can result in damaged parts, broken tools, or — in serious cases — machine crashes. Knowing the most common mistakes helps both beginners and experienced machinists avoid expensive problems on their CNC equipment.
G01 requires an active feed rate (F value). If no F value has been set in the program before the first G01 command, the controller will either throw an error or default to zero — meaning the machine either stops or moves so slowly it effectively stalls. Always ensure an F value is active before any cutting move.
G00 moves at maximum machine speed with no regard for cutting forces or chip load. Using G00 while the tool is engaged with the material will almost certainly break the cutting tool and may damage the workpiece or spindle. G00 is strictly for non-cutting rapid travel.
If the work offset (G54–G59) is not set correctly on the machine, the programmed coordinates will not correspond to the actual workpiece position. This is one of the most common causes of first-part crashes when setting up new jobs on CNC equipment. Always verify the work offset using a dial indicator or tool probe before running the first part.
Because G-codes are modal, a command from one operation can carry over into the next if the programmer forgets to cancel it. A common example is leaving an active canned cycle code (G81–G89) in effect when the programmer intends to make a simple positioning move. The machine will attempt to execute the canned cycle at every position, potentially drilling holes where none are intended. Starting every program with a safety block that explicitly cancels common modal codes (G80, G40, G49) is standard practice in professional shops.
Every axis on a CNC machine has physical travel limits. If a G-code program commands a move beyond those limits, the machine will trigger an overtravel alarm and stop — or in cases where software limits are incorrectly configured, physically crash into a hard stop. Always verify that programmed coordinates fall within the machine's actual travel range, particularly when using work offsets that shift the coordinate origin.
Despite the availability of CAM software that generates G-code automatically, learning to read and write G-code manually remains a valuable skill for anyone working with CNC equipment. Understanding the language helps machinists troubleshoot programs faster, optimize cycle times, and make quick edits at the machine without needing to regenerate code from CAM.
Here is a practical learning path for beginners:
The learning curve for basic G-code proficiency is relatively short — most motivated beginners can write and run simple programs within a few weeks of study and practice. Advanced techniques like macro programming, multi-axis transformations, and custom canned cycles take considerably longer but are rarely needed in everyday production machining.
For shops investing in new CNC equipment, having at least one team member with strong G-code knowledge — not just CAM proficiency — pays dividends every time a program needs to be debugged or modified quickly at the machine without waiting for an engineer to regenerate code in the office.
What Is G-Code in a CNC Machine — The Short Answer G-code, short for geometric code, is the primary programming language used to control CNC (Computer Numerical Control) machines. It tells the machine exactly where to move, how f...
READ MOREWhat Does CNC Mean? CNC stands for Computer Numerical Control. It refers to the automated control of machining tools and manufacturing equipment through pre-programmed computer software. Instead of a human operator manually guidi...
READ MOREWhat Is Custom Manufacturing: A Direct Answer Custom manufacturing is the process of designing, engineering, and producing components or complete products to meet a specific customer's exact requirements — including materials, di...
READ MOREThe Short Answer: What Software Does a CNC Router Actually Need? A CNC router requires at minimum three types of software working in sequence: CAD software to create or import a design, CAM software to convert that design into to...
READ MORE
Zhaxi Township Industrial Park, Nantong City, Jiangsu Province, China (west of Huaneng Power Plant)
+86-13615235768
+86-15950816906
+86-513-85632766
pan.director@sunwayer.com
lf you can't find the answer you're looking for, chat with our friendly team.
