Basics of G Code for CNC: What New Machinists Need to Know
This page explains how a CNC program is built, which G and M codes you will see most often, and how units and offsets change the outcome. It is written for new machinists, apprentices, and engineers who outsource parts and want to read a program before it runs.

How to Read a CNC Program Without Panicking
A G code program is a list of short instructions. Once you know the pattern, the list stops looking random.
What a Block of G Code Actually Contains
Every line in a CNC program is called a block. A block is a set of words, and each word is a letter plus a number. The letter tells the control what kind of information follows: G for a preparatory function, M for a machine function, X Y Z for linear position, F for feed rate, S for spindle speed, T for tool number. The control reads the block left to right and executes it as one instruction.
A typical block looks like N40 G01 X25.4 Y12.7 Z-3.0 F250. The N number is a sequence number, useful when you need to search or restart mid-program. G01 means linear interpolation at the programmed feed rate. The X, Y and Z words give the end point of the move. F250 sets the feed at 250 mm per minute. Change the F word and the same geometry cuts at a different speed.
Order inside the block matters less than people think, but consistency does. Most shops write the modal G code first, then coordinates, then feed. Modal means the code stays active until another code in the same group replaces it. G01 stays active for every block after it, which is why you rarely see it repeated on every single line.
A single wrong decimal is not a cosmetic problem. On a titanium part held to ±0.005 mm, an X word of 25.4 instead of 2.54 sends the tool 22.86 mm past where you intended. That is the difference between a finished part and scrap. Read the coordinate words before you press cycle start.
- 1NSequence number. Helps you find and restart a block.
- 2GPreparatory function: motion mode, units, offsets, canned cycles.
- 3MMachine function: spindle, coolant, tool change, program stop.
- 4XYZ / ABCAxis end points. A, B and C are rotary axes.
The G and M Codes You Will Use Every Day
You do not need to memorize the full code list. A working set of about twenty codes covers most milling and turning work. G00 is rapid positioning, used to move the tool between cuts with no material contact. G01 is linear feed, G02 and G03 are clockwise and counterclockwise arcs. G17, G18 and G19 select the plane for arc interpolation, so an arc written in the XY plane behaves differently from the same words in the XZ plane.
On the setup side, G54 through G59 are work coordinate systems. They tell the control where the part origin sits relative to machine home. G43 applies tool length compensation from the offset table, and G41 or G42 apply cutter radius compensation left or right of the programmed path. If your part is consistently off by the radius of the cutter, you are probably missing G41 or G42.
M codes handle the machine itself. M03 starts the spindle clockwise, M05 stops it, M06 changes the tool, M08 turns coolant on, M09 turns it off. M30 ends the program and rewinds. M00 is a program stop that waits for the operator, which is how many shops insert an inspection pause before a finishing pass on a tight-tolerance feature.
Controllers are not identical. Fanuc, Haas, Siemens and Mitsubishi all use the same core words, but canned cycles, macro syntax and some M codes differ. A program posted for one control may run on another after edits, and it may also alarm. Always check the machine manual before you transfer a program between two brands.
- 1G00 / G01Rapid move and linear feed move.
- 2G02 / G03Arc interpolation, clockwise and counterclockwise.
- 3G54–G59Work coordinate systems, where the part origin sits.
- 4M03 / M05 / M08Spindle on, spindle off, coolant on.
Common G Codes and What They Change
A short list for the shop floor. Values and behavior depend on the controller in front of you.
| Code | Function | When it matters |
|---|---|---|
| G00 | Rapid positioning | Tool moves between cuts, no cutting |
| G01 | Linear interpolation | Straight cuts at the programmed feed |
| G02 / G03 | Arc interpolation | Radii, fillets, circular pockets |
| G17 / G18 / G19 | Plane selection | Arc direction and canned cycle plane |
| G20 / G21 | Inch / metric units | Wrong mode scales the whole part |
| G28 | Return to machine home | Tool change and safe retract positions |
| G41 / G42 | Cutter radius compensation | Keeps the path on the part edge |
| G43 | Tool length compensation | Applies the tool offset from the table |
| G54–G59 | Work coordinate systems | Sets the part origin for the setup |
| G81 / G83 | Drilling and peck drilling | Hole depth, chip clearing, dwell |
| G90 / G91 | Absolute / incremental | Position reference for every move |
| M03 / M05 | Spindle on / off | Direction and stop before a tool change |
Units, Offsets and the Setup Mistakes That Scrap Parts
G20 selects inch mode and G21 selects metric. This is the first thing to check when a program comes from an outside source. A part programmed in millimeters and run in inch mode will try to move 25.4 times further than intended, and most machines will hit a travel limit before the tool reaches the material. That is a lucky outcome. On a short move inside the envelope, the tool simply cuts in the wrong place.
Work offsets and tool offsets are separate systems, and both have to be right. G54 stores the part origin for the current setup. The tool offset table stores the length and radius of each tool. If you touch off a tool and load the wrong offset number, every Z move in the program is wrong by the difference. This is why shops verify the first article on a scrap block before running a production part.
Feed and speed words are where the program meets the material. F is feed in units per minute for milling, or feed per revolution for turning when G99 is active. S is spindle speed in revolutions per minute. Neither word knows what material you loaded. A feed and speed pair that works in aluminium 6061 will burn a 4 mm end mill in 316 stainless. The program is only as good as the cutting data behind it.
At GreatLight we run 127 high-precision CNC machines across three wholly-owned plants, and every program is proven on the setup before the run starts. Parts are held to ±0.005 mm and inspected 100% before shipment, with reports on request. A clean program is the first step in that chain, not the last one.
How 5-Axis Programs Differ From 3-Axis Work
A 3-axis program moves the tool in X, Y and Z. The part stays still. That covers a large share of prismatic work: plates, brackets, housings with features on one or two faces. If the geometry can be reached from a single setup direction, 3-axis milling is usually the faster and cheaper route.
A 5-axis program adds two rotary axes, usually A and B or B and C, depending on the machine. The control coordinates linear and rotary motion in the same block. That lets the tool reach undercuts, blend fillets across a curved surface, and drill holes at compound angles without a second setup. On an impeller or a heat exchanger core, the alternative is several fixtures and several re-zeroing steps, each one adding error.
The programming changes more than the code list. Tool axis vectors, post-processor output, and collision checking all become part of the job. A 5-axis program also needs a machine simulation before it runs, because a rotary move that looks short on screen can swing the tool holder into the table. Our 16 simultaneous 5-axis machining centers, including DMG Mori mills, handle rotary work on a Ø400 mm table and travel up to 4,000 × 400 × 150 mm on the large machines.
Where a 5-axis program pays off: complex contours that need one continuous pass, deep pockets with drafted walls, and parts where tolerance stacks from multiple setups would eat the budget. Where it does not: simple plates, flat covers, and any part a 3-axis machine can finish in one setup. Match the program to the geometry, not to the machine's spec sheet.
What a Posted Program Tells You About the Job
When you open a posted program, read the header first. The tool list, the stock size, the work offset and the units mode are usually at the top in comment lines. If the header names a tool that is not in the machine, stop there. A missing T word in the tool change line is a common posting error and a fast way to break a cutter.
Next, scan the approach and retract moves. G00 blocks that pass close to the fixture are worth checking by hand. Most crashes happen in rapid moves, not in cutting moves, because the operator watches the cut and trusts the rapid. Set a safe Z height in the program and verify it against the tallest point of the part and the clamps.
Finally, look at the feed rates against the tool diameters. A 3 mm cutter running at a feed meant for a 12 mm cutter will snap. A 12 mm cutter running at a feed meant for a 3 mm cutter will rub, work-harden the surface, and wear out fast. The numbers in the program are a plan, and the machine does exactly what the plan says.
- 1Header commentsTool list, stock size, work offset, units mode.
- 2Rapid movesCheck clearance to clamps and fixture before cycle start.
- 3Feed vs tool sizeFeed written for one diameter is wrong for another.
Questions New Machinists Ask About G Code
Do I need to write G code by hand to run a CNC machine?
For most production work, no. CAM software posts the program from a 3D model, and the operator verifies and runs it. Hand writing is still useful for simple facing, spot drilling, and quick edits at the control.
What is the difference between G code and M code?
G codes control motion and how the control interprets the program: feed moves, arcs, units, offsets, plane selection. M codes switch machine functions: spindle, coolant, tool change, program stop. A block can contain both.
Why did my part come out the wrong size in one direction?
Check the units mode first, then the work offset and the tool radius offset. A part that is off by exactly the cutter radius usually means G41 or G42 was missing or applied on the wrong side.
Can the same program run on a Fanuc and a Haas control?
The core words are the same, but canned cycles, macro syntax and some M codes differ. A program posted for one control may need edits for another. Test on a safe block before running the part.
How do I know if a part should be programmed for 5 axes?
If the geometry needs undercuts, compound-angle holes, or one continuous pass across a curved surface, and the alternative is three or four setups, 5-axis is usually worth it. Simple prismatic parts are faster on a 3-axis machine.
What does G28 do, and why is it in almost every program?
G28 sends the machine to its home position, often before a tool change or at the end of the program. It clears the work area so the tool and holder do not collide with the part or fixture.
Send Us Your Part and We Will Review the Program
Upload a STEP file and we return a quotation with free DFM analysis within 12 hours. Uploads are secure and confidential, and an NDA is available on request.
12-hour quoteFree DFM analysis±0.005 mm100% inspection