A Complete Collection of CNC Maching Center Program Codes
This page lists the G-codes, M-codes and program structure that run a CNC machining center, and explains which ones matter for a given part. It is written for programmers, setup machinists and process engineers who need to read or edit a program at the machine, not just press cycle start.

What a program code actually does
Codes fall into two families. One moves the tool, the other changes the state of the machine.
G-codes: the motion and geometry family
G-codes tell the control where to go and how to get there. G00 is rapid positioning, G01 is linear feed, G02 and G03 are clockwise and counterclockwise arcs. Everything else in the G family either sets up the coordinate frame, selects a plane, or calls a canned cycle. On a Fanuc-style control the active G-code stays active until it is cancelled or replaced, which is why a G01 written at the top of a long cut does not need to be repeated on every line.
Modal behavior is the main source of trouble for new programmers. If G41 cutter compensation is left active after a contour, the next operation will offset by the same tool radius and scrap the part. Get in the habit of cancelling at the end of each operation: G40 for cutter comp, G80 for canned cycles, G49 for tool length offset. A clean cancel block costs one line and saves a setup.
Coordinate systems sit above all of this. G54 through G59 are the standard work offsets, and G54.1 P1 through P48 extend that range on controls that support them. G53 moves in machine coordinates, ignoring the active offset. Use G53 for safe retract moves and tool changes when the fixture is tall. G28 and G30 return through an intermediate point, which is useful when a straight move to home would clip a clamp.
M-codes: spindle, coolant and program control
M-codes do not move an axis. M03 starts the spindle clockwise, M04 counterclockwise, M05 stops it. M08 floods coolant, M09 stops it, and M07 usually selects mist. M06 changes the tool. The exact ordering matters: on most machines the control will not accept an M06 while the spindle is turning, so the program needs M05 before the tool change block, or the machine will alarm or wait.
Program control codes decide how the file is read. M30 ends the program and resets the control. M02 ends the program without a reset. M00 is an unconditional stop, M01 is an optional stop that only works when the operator panel switch is on. M98 calls a subprogram and M99 returns from one, or loops the main program when it appears at the end. For a part with six identical pockets, a subprogram with M98 and a set of coordinate offsets is shorter and easier to verify than six copied blocks.
Some M-codes are machine-builder specific. Pallet change, chip conveyor, probe arm, bar feeder and high-pressure coolant through the tool are not standardized across builders. The same M-code number can mean different things on a Haas and a Mazak. Read the machine's own M-code list before running an unfamiliar program. We keep a printed code sheet taped inside each machine door for this reason.
Common codes and what they change
A short list of the codes that come up most often on a 3-axis or 5-axis machining center.
| Code | Function | When to use it |
|---|---|---|
| G00 | Rapid positioning | Non-cutting moves between features |
| G01 | Linear feed | Straight cuts at a set feed rate |
| G02 / G03 | Circular interpolation | Bores, fillets, and arc contours |
| G17 / G18 / G19 | Plane selection | XY, XZ, or YZ arc plane |
| G40 / G41 / G42 | Cutter compensation | Cancel, left, or right offset |
| G43 / G49 | Tool length offset on / off | Set Z height, then cancel |
| G54–G59 | Work coordinate systems | One offset per vise or fixture |
| G73 / G83 | Peck drilling cycles | Deep holes needing chip clearing |
| G81 / G82 | Drill / spot and countersink | Standard holes, dwell at bottom |
| G84 | Tapping cycle | Rigid tapping with an encoder |
| G90 / G91 | Absolute / incremental | Absolute for most work; incremental for patterns |
| G94 / G95 | Feed per minute / per rev | Per rev when tapping or turning |
| G96 / G97 | Constant surface speed / RPM | Turning operations on a lathe or mill-turn |
| M03 / M04 / M05 | Spindle on CW / CCW / stop | Start and stop the spindle |
| M06 | Tool change | Between operations, spindle stopped |
| M08 / M09 | Coolant on / off | Flood or mist control |
| M98 / M99 | Subprogram call / return | Repeated pockets or hole patterns |
| M30 | Program end and reset | Last block of the main program |
Program block layout and safe starts
A block is one line of the program. Word address format is standard: a letter, then a number. N is the sequence number, G is the preparation function, X Y Z are coordinates, I J K are arc centers, F is feed, S is spindle speed, T is the tool number and M is the miscellaneous function. Most controls accept the words in any order within a block, but convention puts N, then G, then coordinates, then F and M. Consistency makes a program easier to scan when something goes wrong at 2 a.m.
A safe start block at the top of every program prevents most crashes. Cancel compensation, cancel the canned cycle, cancel tool length offset, select absolute mode, select the XY plane, and select the work offset. Written out, that is G40 G49 G80 G90 G17 G54. Add G21 or G20 for metric or inch units. If the control was left in incremental mode by the previous job, this line catches it before the first rapid move.
Comments help more than most programmers admit. Parentheses hold comments on Fanuc controls; some builders use a semicolon. Mark every tool change with the tool number and the operation. Mark every fixture stop. When a program comes back to the machine six months later for a repeat order, the comments are the setup sheet.
Macro B, conditions and when not to use them
Macro B adds variables, arithmetic and conditional branches to the program. A local variable like #1 holds a value for the duration of the call. Common variables #100 and above survive a power cycle. IF, GOTO and WHILE let the program decide what to do next. A simple example: use #100 as the hole depth, and the same program drills a 12 mm plate and a 25 mm plate without editing coordinates.
The IF block and GOTO branch are useful for probing and for error trapping. If a probe result falls outside a tolerance, the program can jump to a message or stop rather than cut. The WHILE loop repeats a section as long as a condition holds, which is how a family of parts with a variable number of pockets gets handled without rewriting the file.
Macros are not always the right answer. They are hard to read on the shop floor, and a wrong variable number can send the tool into a fixture. For a one-off part, write the longhand program. Use macros when the same geometry repeats across a family and the setup time saved is real. We also keep a written variable map next to any macro program, because six months later nobody remembers what #103 was for.
Choosing codes for the part in front of you
The part decides the code list, not the other way around. A flat bracket with a few holes needs G81 and G83 and little else. A contoured pocket with a 3 mm corner radius needs G41 with a lead-in arc, and the lead-in length has to be longer than the tool radius or the control will alarm. A part with a true 3D surface needs a CAM post, and the G-code output is mostly G01 moves at a fine stepover. Reading that file by hand is not practical.
Canned cycles save time but hide motion. G83 pecks and retracts to clear chips; set the peck depth to about one tool diameter in aluminum and less in stainless. G73 is faster because it retracts a short distance, but it only works when the chips break cleanly. In gummy materials like 304 stainless or 6061 with a deep hole, a full retract cycle is safer than a speed gain that packs the flutes.
Tapping is where code choice shows up on the inspection report. Rigid tapping with G84 requires the feed rate to match the pitch exactly: feed equals RPM times pitch, in the same units. Get that wrong and the thread is torn or the tap breaks. On a part with a lot of small threads, a floating tap holder and G84 with a slightly conservative RPM is cheaper than a broken tap in a finished bore.
What happens after the program runs
A correct program is only part of the result. Tool wear, thermal growth and fixture deflection all move the cut. On tight work we hold ±0.005 mm (±0.0002 in) and inspect 100% before shipment, which means the program has to leave room for a finish pass that can be adjusted at the machine. If the roughing pass runs to final size, there is nothing left to dial in.
Surface finish is set by the combination of feed, speed, tool geometry and stepover. A Ra 0.8–1.6 μm finish is normal for a well-run finishing pass on aluminum. Ra 0.2–0.8 μm takes a lighter stepover, a sharp tool and often a second finish pass. These are process choices, not code choices, but the code has to leave the stock for them to happen.
For runs of one prototype up to 10,000+ parts, we keep the same program and change the workholding, not the code. That is the point of a clean, commented file: it moves from a single part on a vise to a fixture with six stations without a rewrite. If a job needs a 5-axis simultaneous toolpath, 4,000 mm of travel or a mill-turn operation, the code is generated and verified against the machine's own post, then proven on the first article.
Questions engineers ask about program codes
Do all CNC controls use the same G-code and M-code meanings?
The core set is close, but not identical. G00, G01, G02, G03, G54 and M03 are consistent across Fanuc, Haas, Mitsubishi and Siemens controllers. The differences appear in canned cycle details, high-speed look-ahead modes, and machine-builder M-codes for pallet change, probing or through-tool coolant.
Always check the machine's own code list before running an unfamiliar file. A code that is safe on one builder can trigger a different action on another.
When should I use G41 cutter compensation instead of programming the tool path offset directly?
Use G41 or G42 when you want to adjust the finished size at the machine without editing coordinates. The operator changes the tool radius in the offset table and the contour shifts by that amount.
Program the offset directly when the geometry is simple and the tool will not change. Cutter compensation adds a lead-in and lead-out move, and a lead-in shorter than the tool radius will alarm on most controls.
Can I run a macro program on any machining center?
No. Macro B is an option on many controls and is not always enabled. Check the machine parameter or the builder's option list before writing a program that depends on variables and IF statements.
If macros are not available, the same result can often be reached with subprograms called by M98, using work offsets for each position.
What is the safe way to start a program after a setup change?
Run it in single block with the rapid override turned down, and keep a hand on the feed hold. Watch the first tool change and the first Z approach, because that is where a wrong offset shows up.
A safe start line of G40 G49 G80 G90 G17 G54 clears most leftover modal states. Verify the work offset and tool length offset on the control screen before the first cut.
Does the code change between a 3-axis and a 5-axis machining center?
The G-code family is the same, but 5-axis work adds rotary axis words, usually A, B or C, and often requires RTCP or a similar function to keep the tool tip on the programmed point as the table tilts.
Most 5-axis programs come from CAM and are verified with simulation before the first cut. Hand-writing simultaneous 5-axis code is not practical for real parts.
How do you keep programs consistent across a production run?
We keep one proven program per part number, with comments marking every tool and operation. Changes are logged, and the revised file is re-proven on a first article before the run continues.
On repeat orders the program goes back on the same machine class where possible, so the post and the control behavior stay the same.
Need a part machined from a proven program?
Send us your CAD file. We return a quotation and a free DFM analysis within 12 hours, and every part ships after 100% inspection.
12-hour quote100% inspectionNDA on requestNo minimum order quantity