How Does a CNC Machine Use G Codes?
G codes are the motion language a CNC controller reads line by line. This page explains what happens between the CAM file and the cutting tool: modal states, work offsets, feed rates, and the checks we run before a spindle turns. It is written for engineers and buyers who need to judge how a CNC machine use g codes, not just order a part.

In this article
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
Key takeaways
What G codes actually are, and what the controller does with them
A G code is an address word that starts with the letter G followed by a two-digit number. G00 is rapid positioning, G01 is linear feed, G02 and G03 are clockwise and counter-clockwise arcs. The number is not a step count or a distance; it selects a behavior that the controller already knows how to execute.
The controller reads the program one block at a time. Each block is a single line, and it can hold several words: a G code for motion, X Y Z coordinates, an F feed rate, an S spindle speed, and an M code for an auxiliary function. The controller parses that line, updates its internal state, then sends position commands to the servo drives.
That internal state is what makes G code compact. Positions, feed, spindle speed and the active plane all persist from block to block. A program only writes what changes. This is why a 30,000-line file for a complex 5-axis part can still be read and verified by a human in sections.
Machine builders follow ISO 6983 for the core set, but the edges differ. A canned cycle, a probing routine, or a high-speed look-ahead mode may carry the same G number on two different controls and behave differently. Never assume a program is portable between a Fanuc, a Siemens and a Heidenhain control without a dry run.
- 1G00Rapid move at machine maximum, no cutting.
- 2G01Straight feed at the programmed F rate.
- 3G02 / G03Circular interpolation, with I J K or R defining the arc.
- 4G54–G59Work coordinate systems, set from the fixture.
How a CNC machine use g codes to turn a CAM file into cutter motion
The chain starts in CAM. The post-processor converts toolpaths into G code using a machine definition: axis travel limits, spindle orientation, coolant type, and the exact syntax your control expects. If the machine definition is wrong, the code is wrong, no matter how good the toolpath looked on screen.
The controller then runs its own interpreter. It checks for syntax errors, looks ahead a set number of blocks, and plans acceleration so the tool does not overshoot at corners. Look-ahead depth is a machine parameter. On a 4,000 mm gantry with heavy steel, a short look-ahead shows up as rounded corners and chatter marks.
Servo drives close the loop. The controller issues a position demand, the encoder reports actual position, and the drive corrects the error thousands of times per second. Backlash, thermal growth and ball-screw wear all live in that loop. A machine that holds ±0.005 mm one week and drifts the next usually has a mechanical problem, not a code problem.
Cutting data sits on top of all this. Feed and speed come from the material, the tool and the depth of cut. On 6061-T6 aluminium we might run a 12 mm carbide end mill at 8,000 rpm and 3,000 mm/min, while 17-4PH stainless at the same diameter runs far slower. The G code carries those numbers, but the machinist chooses them.
How to read a G code block without running the machine
Start at the header. You should see a program number, a safety block that cancels cutter compensation and active cycles, and a G90 or G91 to set absolute or incremental mode. A missing safety block is the single most common reason a proven program crashes on a different machine.
Next, find the work offset call. G54 should appear before the first cutting move, along with a G43 tool length compensation and an H number that matches the tool. If the H number does not match the tool in the carousel, the tool will plunge to the wrong depth. This is a fast, cheap thing to check and it catches real mistakes.
Then walk the first cutting block. Confirm the feed rate is present, that the spindle direction is correct with M03 or M04, and that coolant is called before the tool enters the material. On titanium and magnesium, coolant timing matters more than almost any other line in the program.
Finally, check the retract and the end of the program. The tool should clear the part and the fixture before any rapid move, and the program should end with a spindle stop and a return to a safe position. If the last block leaves the tool at depth, the next operator inherits a hazard.
- 1HeaderSafety block, units, absolute mode.
- 2OffsetsG54 and G43 H numbers must match the tool.
- 3First cutFeed, spindle direction and coolant before entry.
- 4EndClear the fixture, stop the spindle.
When G code matters less than people assume
For a simple 2.5D bracket, the code is almost a formality. A CAM package produces a clean program, the operator proves it once, and the part runs for thousands of cycles. The real risk here is fixturing and tool wear, not the code itself.
On simultaneous 5-axis work the balance flips. Tool axis control, singularity handling and post-processor accuracy decide whether the surface finish holds at Ra 0.8–1.6 μm or turns into visible scallops. Two shops can run the same CAM software and get very different results because their post-processors were validated differently.
Hard materials add another layer. Inconel and Ti-6Al-4V generate high cutting forces and heat. Feed rates need to respect tool deflection, and a program that works in aluminium will destroy a cutter in titanium within a few seconds. The code is identical in structure; the numbers are not.
Where G code genuinely stops mattering is at the level of metallurgy, heat treat and residual stress. A perfect program cannot fix a warped casting or a part that moves after stress relief. Those problems get solved upstream of the controller.
How we keep G code under control at GreatLight
We run 127 high-precision CNC machines across three plants in Dongguan, including 16 simultaneous 5-axis machining centers, 12 four-axis mills and 16 mill-turn centers. Each machine has its own validated post-processor. That is a maintenance job, not a one-time setup, because control software gets updated and posts drift.
Every new program gets a first-article inspection before the run continues. We check the drawing, the material certificate and the critical dimensions, then release the job. If a tolerance sits inside ±0.005 mm, the program is proven on the specific machine that will run it, not on a sister machine.
For multi-axis parts, we plan the setup so that as much geometry as possible is cut in one fixturing. Fewer setups means fewer offset changes, and offset changes are where the majority of dimensional errors come from. On parts that need Ra 0.2–0.8 μm, we separate roughing and finishing into different programs so the finish pass runs with a fresh tool and a clean offset.
We work from ISO 9001:2015, IATF 16949:2016, ISO 13485:2016 and ISO 27001:2022 systems. Inspection reports are available on request, and we hold 100% inspection before shipment on production runs. Uploads stay confidential, and an NDA is available when a program or drawing needs that cover.
Step by step: how to prove a G code program before cutting metal
- 1Confirm the machine definition in CAMCheck axis travel, spindle taper, coolant type and control dialect. For a 750 × 1,150 × 550 mm machine, verify the post is not written for a 500 × 500 × 450 mm envelope.
- 2Check the header and safety blockLook for G17, G21 or G20, G90, G40, G49 and G80. A missing G40 leaves cutter compensation active and the first cut will be off by the tool radius.
- 3Verify offsets and tool numbersMatch every H and T number to the physical tool. Confirm the G54 origin against the drawing datum, not against the fixture edge.
- 4Run a dry cycle with the spindle offUse the single-block and rapid-override controls. Watch the Z clearance at every rapid. If a rapid passes within 5 mm of a clamp, change the program, not the fixture.
- 5Air-cut or cut wax for new geometryFor first-article 5-axis work, an air cut verifies the kinematics. Check the tool tip position at the extremes of the rotary table, around Ø400 mm of swing.
- 6Prove the first part with conservative feedsDrop feed to 50% and step up in 10% increments while watching spindle load and chip color. Record the final values in the setup sheet.
- 7Measure and lock the programInspect critical features, confirm the ±0.005 mm tolerance holds, then lock the program revision. Any later edit needs a new first-article check.
G code vs M code: what each one controls
Use this as a quick reference when reading a program block.
| Code | Controls | Typical use | Watch for |
|---|---|---|---|
| G00 | Rapid positioning | Move between features | Collisions with clamps |
| G01 | Linear feed | Straight cuts and ramps | Missing or stale F value |
| G02 / G03 | Circular interpolation | Bores, radii, arcs | Wrong I J K direction |
| G43 | Tool length compensation | Every tool change | H number not matching tool |
| G54–G59 | Work coordinate offset | Fixture datum setup | Offset left from last job |
| M03 / M04 | Spindle on, forward or reverse | Start of every cut | Wrong direction on left-hand tools |
| M08 / M09 | Coolant on and off | Before and after entry | Coolant late in titanium |
| M30 | Program end and reset | Last block of the file | Tool left at depth |
The code is only as good as the setup behind it
G codes tell the machine where to go. Offsets, tool data and fixturing decide whether it arrives at the right place. If your program is proven and your part still drifts, the answer is usually mechanical.
Frequently asked questions
Do all CNC machines use the same G codes?
The core motion codes follow ISO 6983, so G00, G01, G02 and G03 mean roughly the same thing on most controls. The differences appear in canned cycles, probing, high-speed modes and macro syntax.
A program written for one control family often needs post-processor edits before it runs on another. Always dry-run before cutting.
Can I write my own G code by hand?
For simple operations like facing, drilling a hole pattern or squaring a block, hand-written code is practical and fast. You need to know the offsets, the tool lengths and the safe Z heights on that specific machine.
For contoured 3D surfaces or simultaneous 5-axis motion, CAM output is the only realistic route. Manual trig for a few hundred arc segments is where mistakes multiply.
Why did my part come out the wrong size when the program looked correct?
Check the work offset first. A G54 value left over from the previous job shifts the entire part, and the controller reports no error because nothing is technically wrong.
Next, check tool length compensation. An H number that does not match the physical tool changes the Z depth without changing X or Y, so the part looks correct in plan view and wrong in section.
What is the difference between G codes and M codes?
G codes control geometry and motion modes. M codes control machine functions such as spindle rotation, coolant, tool changes and program end.
Both can appear in the same block. A typical first-cut line carries G01, coordinates, an F value and an M08 for coolant.
Does the G code determine the surface finish?
Partly. Stepover, feed rate and tool geometry set the theoretical finish. Machine rigidity, tool runout and workholding decide whether you actually reach Ra 0.8–1.6 μm or see chatter.
If the finish is poor, the fix is usually a combination of a lighter finish pass, a sharper tool and a stiffer setup, not a code rewrite alone.
How do you handle a program for a part with a ±0.005 mm tolerance?
We prove the program on the machine that will produce the run, with a first-article inspection before production continues. Critical dimensions are measured against the drawing, and reports are available on request.
The program revision is then locked. Any edit, including a feed change, triggers a new first-article check.
Send us your drawing and get a DFM review in 12 hours
We review the geometry, suggest the machining approach and quote from one prototype to 10,000+ part runs. No minimum order quantity.
12-hour quote100% inspectionNDA on request±0.005 mm tolerance