G Code in CNC Machining: What the Controller Actually Reads
G code in CNC machining is the instruction set that turns a CAM toolpath into axis motion, spindle speed and tool changes. This page explains the coordinate model, modal states and compensation logic behind the language, and where a program stops being safe to run untouched.

In this article
- 1
- 2
- 3
- 4
- 5
- 6
How G code in CNC machining becomes axis motion
A controller does not read a drawing. It reads a stream of blocks, each block a line of words. A word is a letter plus a number: G for preparatory action, M for machine function, X Y Z for linear position, I J K for arc centers, F for feed, S for spindle speed, T for tool number. The controller parses the block left to right, applies the modal state, then moves.
Modal means the command stays active until another command in the same group replaces it. Issue G01 once and every following block with X Y Z coordinates feeds at the programmed rate. G00 rapid, G01 feed, G02 clockwise arc, G03 counter-clockwise arc. This is why a missing G01 near the top of a program can send a tool across the part at rapid speed.
Absolute and incremental positioning follow the same idea. G90 sets absolute mode, where every coordinate is measured from the active work origin. G91 sets incremental mode, where each coordinate is a distance from the last position. Most production programs run in G90. Incremental blocks appear in subprograms, drilling patterns and bar-pull routines on lathes.
Units are a group too. G20 selects inches, G21 selects millimeters. A program that mixes the two will not alarm out on every control. Some controllers simply scale the motion, and a 25 mm move becomes 25 inches of travel. Post-processor output must match the control setting, not the drawing units.
- 1G90 / G91Absolute or incremental coordinate interpretation.
- 2G20 / G21Inch or metric units for all axis words.
- 3G17 / G18 / G19Plane selection for arc interpolation: XY, XZ, YZ.
- 4G94 / G95Feed per minute or feed per revolution.
Cutter compensation and offsets in G code in CNC machining
The CAM system knows the nominal tool diameter. The shop floor knows the real one. G41 and G42 tell the control to shift the toolpath left or right of the programmed contour by the value stored in the offset register. G40 cancels it. That register is where an operator enters the measured radius after touching off a tool.
Cutter compensation lets you adjust a feature without reposting the program. A slot that measures 0.03 mm undersize can be brought in by editing the wear offset, not by rewriting 4,000 blocks of CAM output. On a production run this is the difference between a two-minute correction and a two-hour one.
Length offsets work the same way. G43 applies the tool length offset from the register named by the H word, and G49 cancels it. If the H number points at the wrong register, the tool will rapid to the wrong Z height. That single mismatch is behind a large share of crashes on vertical mills.
Work offsets define where the part sits. G54 through G59 each hold a stored origin, so a fixture with several stations can run the same program at different locations. G10 lets a program write values into those registers, which is useful on pallet systems but risky on a manual setup.
- 1G41 / G42 / G40Left, right and cancel cutter radius compensation.
- 2G43 / G49Apply or cancel tool length offset via the H register.
- 3G54–G59Select a stored work coordinate system.
- 4Wear vs geometryKeep cutting-size tweaks in the wear column.
Why feed and speed numbers are not transferable
F and S are not universal constants. Surface speed depends on material, cutter coating, rigidity and coolant. A 12 mm carbide end mill in 6061 aluminium runs far faster than the same cutter in 17-4PH stainless. Copying a feed rate from one job to another without adjusting for material is how tools break.
Chip load is the more useful number. Feed per tooth multiplied by tooth count and spindle speed gives the table feed. Too low a chip load rubs the edge and work-hardens stainless. Too high a chip load deflects a small cutter and pushes a wall out of tolerance. Aim for the middle of the cutter supplier's range first.
On a five-axis machine the programmed feed is the tip feed, not the feed of every axis. When the rotary table swings, the linear axes can move faster than the commanded value to keep the tip on path. Controls handle this with feed rate limits, but tight radii at high feed still show up as chatter or gouges.
Rigidity sets the ceiling. A 4,000 mm long part on a large travel machine will chatter long before the same alloy on a compact machine. Keep the toolholder as short as the feature allows, and reduce overhang before you reduce feed.
- 1Start from surface speedThen convert to rpm for the actual cutter diameter.
- 2Check chip loadFeed per tooth should stay inside the supplier range.
- 3Watch tool overhangLong reach cuts the achievable feed rate.
When CAM output needs manual editing
Most programs come off CAM and run. Some do not. Editing on the floor is normal, but it should be deliberate and recorded. If you change a feed rate, a Z clearance plane or a tool number, the next setup needs to know.
Safe start blocks matter. A retract plane set too low can scrape a vise or a clamp. A rapid move through a fixture zone can crash a holder. Setting a clearance plane above the tallest obstruction, and checking the first approach in single block, catches most of these before the spindle turns.
Restart points change the risk. M00, M01 and M30 control program flow, while M03 and M05 start and stop the spindle and M08 and M09 control coolant. Jumping to a mid-program restart without re-establishing modal state leaves the control in whatever mode the last block used. Re-enter the modal set before letting the cycle run.
Subprograms and macros reduce repetition on families of parts. M98 calls a subprogram, M99 returns. A macro can calculate a pocket depth from a variable instead of hard-coding it. Both are powerful and both hide errors from a reader who only scans the main program. Document what each call does.
- 1Clearance planeSet it above the tallest fixture element.
- 2Single block the first runVerify approach moves before full feed.
- 3Log every editFeed, offset and tool changes belong in the setup sheet.
Where G code stops being the right tool
G code describes motion. It does not describe intent. A program cannot tell you whether a 0.05 mm wall deflection is acceptable for the part's function, whether a surface finish meets a sealing requirement, or whether the tolerance is realistic for the geometry.
Complex free-form surfaces push the format hard. A dense set of tiny linear segments approximates a curve well enough, but the block count grows and the control spends time reading rather than cutting. High-speed machining options and spline interpolation exist for this, and they are not available on every control.
Tolerance drives the decision. Features held to ±0.005 mm need the compensation, probing and in-process checks that a bare G code program does not include. If your part carries those tolerances, the programming conversation is about setup and metrology, not about G words.
For simple prismatic parts, hand-written or lightly edited G code is fast and predictable. For contoured, multi-setup or five-axis work, CAM output with a verified post-processor is the practical route. Both are legitimate. Pick based on geometry and tolerance, not habit.
- 1Prismatic and simpleShort programs are easy to verify by reading.
- 2Contoured and multi-axisCAM output wins on block count and verification.
- 3Tight toleranceSetup and probing matter more than the code itself.
Common G code groups and what they change
Groups are exclusive. Only one command from a group can be active at a time.
| Group | Command | Effect | Typical use |
|---|---|---|---|
| Motion | G00 | Rapid traverse, no cutting | Approach and retract |
| Motion | G01 | Linear feed at programmed F | Facing, walls, roughing |
| Motion | G02 / G03 | Clockwise / counter-clockwise arc | Fillets, bores, radii |
| Comp | G41 / G42 | Shift path left / right of contour | Finish passes, size control |
| Offset | G43 | Apply tool length from H register | Every tool change |
| Plane | G17 | Arc interpolation in XY | Milling default |
The verdict on G code in CNC machining
If the part is prismatic with open tolerances, edit and verify a short program yourself. If it is contoured, five-axis or held to ±0.005 mm, run verified CAM output and control size with offsets and probing instead of rewriting blocks.
Questions engineers ask about G code
Can I edit a CAM program directly on the machine?
Yes, for feed rates, offset numbers and clearance values. Keep the edits small and write them into the setup sheet so the next run starts from the same state.
Avoid changing geometry by hand. A contour edit made at the control is hard to trace later and easy to leave out of the CAM file.
Why does my program alarm on the arc block?
Most arc alarms come from a mismatch between the selected plane and the I J K values, or from an arc radius that the endpoints cannot satisfy. Check G17, G18 or G19 against the axis words in the block.
A missing G02 or G03 is another cause. The control keeps the last modal motion command, so an arc block with no G word may be read as a straight feed.
Do I need cutter compensation if CAM already offsets the path?
CAM can output the path with the nominal tool radius already applied, and that runs fine. What you lose is the ability to adjust size without reposting.
For parts with a size tolerance on a wall or bore, G41 and G42 with a wear offset give the operator a fast correction path.
How do I check a program before the first cut?
Run the simulation in CAM, then check the posted program for units, plane selection, work offset and the first Z approach. Dry run with the tool above the stock where the control supports it.
On the first run, use single block and reduced rapid override through the approach moves. Most crashes happen in the first thirty seconds.
Is hand-written G code still worth learning?
Yes. Being able to read a posted program makes you faster at finding the cause of a gouge, a chatter mark or a wrong-size feature.
It also helps when a CAM post is not available for a legacy control and a short program has to be written from scratch.
What tolerance can a well-run program hold?
At GreatLight, production parts are held to ±0.005 mm (±0.0002 in) with 100% inspection before shipment. Whether a specific feature reaches that depends on geometry, material and fixturing.
Send the drawing and we return a DFM analysis and quotation within 12 hours.
Send the drawing, not just the program
Upload your files for a DFM review and quotation within 12 hours. Uploads are secure and confidential, and an NDA is available on request.
12-hour quote100% inspection±0.005 mmNo minimum order quantity