What G Code for CNC Machine? The 7 Codes That Matter
G code is the instruction set a CNC control reads to move axes, spin a spindle, and change tools. This page explains which codes do what, where the same number means something different on a lathe, and how that changes the way a part gets quoted and cut.

In this article
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
Key takeaways
What G Code for CNC Machine Actually Controls
G code is a list of numbered commands that a machine control reads one block at a time. Each block is a single line, and each line can hold several words: a G code for motion mode, X/Y/Z for position, F for feed, S for spindle speed. On a mill, G0 rapids to a point at full speed. G1 feeds in a straight line at the programmed feed rate. G2 and G3 swing an arc clockwise or counterclockwise.
The control does not decide anything about the part. It executes exactly what the CAM post-processor handed it. That is why two shops cutting the same model can produce different cycle times and different surface finishes from the same nominal geometry: the toolpaths, stepovers, and entry moves behind the G code are different.
A block is not a sentence in a human language. G1 X50.0 Y20.0 F800 means feed to that coordinate at 800 mm/min on most metric controls, or 800 in/min on an inch-programmed machine. Units are set by G20 and G21, and mixing them is one of the fastest ways to scrap a part.
- 1Modal codesStay in effect until changed, such as G1, G54, or G21.
- 2One-shot codesExecute once in their own block, such as G4 dwell or G28 return.
- 3Word orderFanuc-style controls run the block in a fixed sequence regardless of how you write it.
The Motion Codes You Will See in Every Program
Most of a program is built from four motion codes. G0 moves at rapid traverse and is never used for cutting. G1 interpolates a straight line at the commanded feed. G2 and G3 cut arcs and need either an R word or I, J, K offsets to define the center. A full circle written with only R is ambiguous, so most post-processors output I and J for anything past 180°.
G17, G18, and G19 select the plane for arc interpolation and cutter compensation. G17 is XY and covers nearly all 3-axis milling. G18 and G19 appear on lathes and on parts where you machine in the XZ or YZ plane. Pick the wrong plane and the arc will still run, just in a direction you did not intend.
Cutter compensation is the other group worth knowing. G41 offsets the tool to the left of the path, G42 to the right, and G40 cancels it. On older controls this let an operator adjust size at the machine by typing a wear value instead of editing the program. Many modern shops leave comp in the CAM output and control size through tool radius offsets in the machine's tool table.
- 1G0Rapid positioning. Clear the part before using it.
- 2G1Linear feed move. The workhorse of any cut.
- 3G2 / G3Clockwise and counterclockwise arcs in the active plane.
- 4G40 / G41 / G42Cancel, left, and right cutter compensation.
Work Offsets, Tool Length, and Why G54 Shows Up Everywhere
G54 through G59 are work coordinate systems. They tell the control where the part origin sits relative to machine home. A vise on a 3-axis mill might be G54; a second vise on the same table might be G55. The same program can then run on either position without a single edit. G10 lets a program write those offsets from inside the code, which is common on production fixtures.
Tool length compensation is separate. G43 applies the length offset stored for the active tool, usually with an H number. G49 cancels it. If a tool change happens without a G43 H word, the next Z move will drive to the wrong height. On a machine cutting at ±0.005 mm, a missing length offset is not a small error.
G28 sends an axis back to the machine reference point, and G30 goes to a secondary reference. Both are used for safe retracts between operations. A G28 on Z before a tool change is standard practice on many controls, because it moves the spindle clear of the fixture before the carousel rotates.
G92 sets a temporary coordinate shift inside the program. It is useful for a quick manual setup and dangerous in a production file, because the shift persists and stacks with later offsets if nobody clears it.
- 1G54–G59Part origins stored in the control, one per fixture station.
- 2G43 / G49Apply and cancel tool length compensation.
- 3G28 / G30Return axes to reference positions for safe retracts.
- 4G92Temporary shift. Keep it out of production programs.
Canned Cycles and the Codes That Save Real Cycle Time
Canned cycles bundle a repeated motion into one block. G81 is a simple drill cycle. G83 pecks in steps so chips clear, which matters in deep holes and in gummy materials like 6061 or 316L. G73 is a high-speed peck that breaks the chip without fully retracting. G84 taps, and it must be synchronized with the spindle or the tap will strip the thread.
On a mill, G84 rigid tapping needs the spindle encoder and a feed rate that matches pitch times RPM. Get the arithmetic wrong by a few percent and the thread will still look fine on the gauge but fail a go/no-go check. G85 reams with a feed-out, which produces a straighter hole than feeding back out at rapid.
Lathes use the same numbers for different cycles. G71 is a roughing cycle, G70 finishes, G76 cuts threads. On a mill, G76 is a fine boring cycle. A programmer moving between the two machine types without checking the control manual will write a program that runs and cuts the wrong shape.
Cycles are modal in a limited sense. Once G83 is active, each new X/Y position drills another hole until G80 cancels the cycle. Forgetting G80 before a G0 move is a classic crash, because the control will try to drill at the new location.
- 1G80Cancel any active canned cycle. Always call it before moving.
- 2G81 / G83Plain drilling and peck drilling with chip clearance.
- 3G84Tapping. Feed must equal pitch × spindle RPM.
- 4G85Reaming with a controlled feed out of the hole.
M Codes: Spindle, Coolant, and Tool Change
M codes change machine state rather than position. M3 starts the spindle clockwise, M4 counterclockwise, M5 stops it. M8 floods coolant and M9 shuts it off. M6 calls a tool change and normally needs a T word with the tool number. On a lathe, M3 and M4 set spindle direction the same way, but the G codes around them change meaning.
Some M codes are machine-specific. A pallet changer, a bar feeder, or a probe macro will each use M codes that are not part of any standard. Those numbers come from the machine builder, and the post-processor has to be configured for that exact machine. Copying them between two machines from different builders will not work.
Program structure ends with M30, which stops the program and rewinds it to the top. M0 is a planned stop that waits for the operator. Use M0 before a manual check on a first article, and leave it out of the production version of the same program.
The safe-start block is where all of this is declared. A typical one cancels compensation, cancels cycles, selects absolute positioning with G90, sets units with G21, and calls the first work offset before any motion happens.
- 1M3 / M4 / M5Spindle clockwise, counterclockwise, and stop.
- 2M6Tool change. Pairs with a T word.
- 3M8 / M9Coolant on and off.
- 4M0 / M30Planned stop and program end with rewind.
How G Code Choices Show Up in the Finished Part
Two programs can produce the same nominal dimensions and still differ in ways that matter. A G1 approach into a pocket wall leaves a witness mark. A G2 lead-in arc blends into the wall and leaves almost nothing. On a visible cosmetic face, that single block changes whether the part needs hand polishing afterward.
Feed and speed come from the material and the tool, not the control. Cutting 7075 aluminum at 12,000 rpm with a 6 mm carbide end mill is normal. Running the same tool in Inconel at that speed will destroy it in seconds, so the G code will carry a much lower S value and a lighter chipload. The control does not know the difference.
Thermal growth is the part people forget. A machine cutting for six hours straight will drift as the spindle and ballscrews warm up. G code can include a warm-up cycle and periodic re-probing of a reference feature. On work held to ±0.005 mm, skipping that step shows up as a slow size trend across a production run.
Surface finish targets drive toolpath density more than any single code. Roughing at Ra 1.6–3.2 μm is normal for a roughing pass. A finishing pass with a small stepover and a constant-engagement path can reach Ra 0.8–1.6 μm, and a fine finishing strategy can push toward Ra 0.2–0.8 μm. The codes are the same; the parameters behind them are not.
- 1Lead-in styleArc entry hides witness marks; straight entry does not.
- 2ChiploadSet by tool and material, not by the control.
- 3Thermal driftWarm-up and re-probing keep size stable on long runs.
- 4StepoverSmaller stepover, better finish, longer cycle.
When G Code Alone Is Not the Right Answer
Hand-written G code works well for simple geometry: a face, a slot, a drilled hole pattern. It is a bad choice for a contoured 5-axis surface or a part with 40 pockets. The math for a smoothly blended toolpath is not something a person types by hand at the control. CAM handles it, and the post-processor turns the result into G code.
The post-processor is where most errors hide. A post written for a 3-axis machine will output a valid-looking program with no rotary moves. Load it on a 5-axis center and the part will be cut in the wrong orientation. Every machine model, and often every machine, needs its own verified post.
Some features cannot be produced by toolpath alone, no matter how good the G code is. A sharp internal corner needs a tool with a radius, and the radius stays in the corner. Undercuts need a different tool axis or a second setup. Deep, narrow pockets need a long tool that will deflect unless the stepdown and stepover are reduced.
That is the point where the question shifts from programming to process. If the geometry needs five simultaneous axes, or the tolerance is tight on a thin wall, the choice of machine and fixturing matters more than the code. We run 16 simultaneous 5-axis centers alongside 27 three-axis machines, so the same part can be quoted on the process that actually fits it.
- 1Simple geometryHand-written code is fast and easy to verify.
- 2Complex surfacesCAM with a verified post is the only practical route.
- 3Internal cornersTool radius sets the smallest corner you can cut.
- 4Thin wallsDeflection, not code, drives the achievable tolerance.
Same Code, Different Machine: Mill vs Lathe
Numbers that behave differently depending on the control type.
| Code | On a mill | On a lathe |
|---|---|---|
| G70 | Finishing cycle, some controls | Finishing cycle for a rough profile |
| G71 | Rarely used | Roughing cycle with depth of cut |
| G76 | Fine boring cycle | Thread cutting cycle |
| G84 | Rigid tapping cycle | Spindle direction command |
| G96 | Constant surface speed on some lathes | Constant surface speed control |
| G98 / G99 | Canned cycle return plane | Feed per minute or per rev |
| G28 | Return to machine reference | Return to machine reference |
| G2 / G3 | Arc in the active plane | Arc on the part profile |
The Short Version
If your part is simple and the shop writes code at the control, ask which codes drive the finish and the size. If it has contoured surfaces or tight tolerances, ask about the post-processor and the machine instead. That is where the outcome is decided.
Questions Engineers Ask Next
Do I need to send G code with my RFQ?
No. Send the 3D model and the 2D drawing with tolerances. We generate the toolpaths and the G code in-house from the STEP file, and we run a DFM review before cutting.
If you already have a proven program for a specific machine, mention it. It is useful context, but we still verify it against our own machine geometry and post.
What file formats work best for generating G code?
STEP and IGES handle 3D geometry cleanly. DXF works for flat parts and sheet metal profiles. Native CAD files are fine too, and PDF drawings are useful for tolerances, datums, and notes.
The model alone rarely carries everything. Tolerances, surface finish callouts, and datum schemes belong on the drawing.
Can the same part run on a 3-axis and a 5-axis machine?
Often yes, with more setups. A 3-axis machine needs the part repositioned for each face, and each reposition adds a location error. A 5-axis center reaches the same features in one setup, which usually holds position between faces better.
For a part with features on four sides and a tolerance that stacks across them, the 5-axis route is the safer one. For a simple plate with drilled holes, 3-axis is faster and cheaper.
How do you verify a program before it cuts metal?
We simulate the toolpath against the stock model and the fixture, then run a dry pass or a first article on scrap where the geometry is risky. The first article gets inspected before the rest of the run starts.
Every part is inspected before shipment, with raw material checks, in-process monitoring, and a final inspection. Reports are available on request.
Does the material change the G code?
The structure stays the same. The values change. Aluminum 6061 and 7075 run at high spindle speeds and light chiploads. Stainless 316L and 17-4PH run slower with more coolant. Titanium TC4 and Inconel run slower still, with reduced stepover and careful heat control.
We machine all of these in-house, along with brass, copper, tool steel, and engineering plastics such as PEEK and carbon fiber.
What if the part needs a finish the toolpath cannot produce?
Then it goes to a secondary process. Bead blasting, tumbling, brushing, polishing, anodizing, plating, and laser marking are all handled after machining. Laser marking has a minimum character height of 1.5 mm.
The machining program still matters, because a rougher surface takes more work to bring to the final finish.
Send the Model, Get a Program Plan
Upload your STEP file and drawing. We return a quotation with a free DFM analysis within 12 hours, and production can start within 24 hours.
12-hour quote100% inspectionNo minimum order quantity