IsoCNC logo — precision CNC machining from China

G Code vs M Code: CNC Programming Basics for Engineers and Buyers

G codes move the tool, M codes run the machine. A plain-language tour of the commands behind every CNC program — with quick-reference tables and a real program explained line by line.

CNC milling machine cutting a part from G code and M code program instructions

Quick Answer

G code and M code are the two command families in the programming language every CNC machine runs (standardized as RS-274 / ISO 6983). G codes are geometric, preparatory commands: they control motion and cutting logic — rapid positioning (G00), linear cuts (G01), arcs (G02/G03), units, planes, work offsets, and compensation. M codes are miscellaneous machine functions: they run the hardware around the cut — spindle on/off and direction (M03/M04/M05), tool changes (M06), coolant (M08/M09), and program stops (M00/M01/M02/M30). A part program is a numbered sequence of these instructions executed line by line, and most codes are modal, meaning they stay active until another code in the same group replaces them.

What Is G Code? What Is M Code?

Every CNC machined part starts life as a CAD model, gets turned into toolpaths in CAM software, and arrives at the machine as a text file of G code and M code commands. The control reads that file one line (called a block) at a time and executes exactly what it says — no more, no less. Understanding how CNC machining works starts with understanding this language, because it is the literal bridge between the drawing and the cut.

G codes are the geometry and motion commands. The G stands for preparatory — they prepare the control for a type of operation. G00 says move fast and do not cut, G01 says cut in a straight line at feed rate F, G02 and G03 say cut arcs. Other G codes do not move anything at all: they set the context in which motion happens — inch or metric units (G20/G21), the plane arcs live in (G17–G19), where part zero is (G54–G59), and whether coordinates are absolute or incremental (G90/G91).

M codes are the machine-function commands. The M stands for miscellaneous. They switch the machine’s auxiliary hardware: spin the spindle up clockwise (M03) or counterclockwise (M04) at the speed given by the S word, stop it (M05), swap tools (M06), flood the cut with coolant (M08), shut coolant off (M09), and control program flow with stops and program-end commands. In short: G codes cut the part, M codes run the machine. A typical block mixes both — a line might rapid to position while the next line starts the spindle and coolant together.

The core vocabulary is standardized, so the commands below behave the same on the Fanuc-style controls that run most of the world’s mills and lathes. Machine builders do add proprietary codes for probes, pallet changers, and special cycles, so a program written for one control still needs a post-processor check before it runs on another.

The Most Common CNC Machine G Codes

There are well over a hundred G codes in a modern control, but a working subset of about twenty appears in nearly every program. If you can read the codes in this table, you can follow what most of a CNC program is doing. These cover the three jobs G codes perform: moving the tool, defining where part zero is, and setting the modes the machine operates in.

The most common G codes used in CNC milling and turning programs
G CodeNameWhat It Does
G00Rapid positioningMoves at maximum machine speed to a target point without cutting; used only for positioning moves
G01Linear interpolationCuts in a straight line at the programmed feed rate F
G02 / G03Circular interpolationCuts an arc — G02 clockwise, G03 counterclockwise — defined by an end point plus a radius R or arc center I/J/K
G17 / G18 / G19Plane selectionChooses the working plane for arcs and cutter compensation: G17 XY, G18 ZX, G19 YZ
G20 / G21Unit selectionG20 programs in inches, G21 in millimeters
G28Return to reference pointSends the axes home to the machine reference position, usually via an intermediate point for safety
G40 / G41 / G42Cutter radius compensationG41 offsets the tool left of the programmed path, G42 right, G40 cancels compensation
G43Tool length compensationApplies the tool length offset stored under the matching H number so Z positions are measured from the tool tip
G54–G59Work coordinate systemsSelects one of six stored work offsets that define where part zero sits on the machine
G90 / G91Positioning modeG90 reads coordinates as absolute positions from part zero; G91 reads them as incremental distances from the current point
G94 / G95Feed modeG94 feeds per minute (mm/min or in/min); G95 feeds per spindle revolution — common on lathes for threading and turning

Two entries deserve special respect. G00 rapids at full machine speed — it is for positioning only, never for cutting, and an incorrect G00 line is the classic way to crash a tool. And the G90/G91 pair decides what your coordinates mean; a program that expects absolute mode (G90) but runs in incremental (G91) will drive the tool somewhere you did not intend. That is why disciplined programmers state these modes explicitly on the first line of every program.

The Most Common CNC M Codes

M codes are fewer and simpler — most programs use fewer than a dozen — but forgetting one has immediate consequences: a tool change without M05 spins a live tool into the changer, and a long program without M30 leaves the control sitting at the end instead of rewound for the next part. Here is the working set.

The most common M codes controlling CNC machine auxiliary functions
M CodeNameWhat It Does
M00Program stopHalts the program unconditionally — spindle, coolant, and motion stop — until the operator presses cycle start
M01Optional stopStops the program only if the operator has enabled the optional-stop switch on the control
M02End of programMarks the end of the program; the older convention, without the automatic rewind M30 provides
M03 / M04Spindle onStarts the spindle — M03 clockwise, M04 counterclockwise — at the speed set by the preceding S word
M05Spindle stopStops spindle rotation; required before tool changes and at the end of a program
M06Tool changeCommands an automatic tool change to the tool called by the T word
M08 / M09Coolant controlM08 turns flood coolant on, M09 turns all coolant off
M30End of program + rewindEnds the program, stops spindle and coolant, and rewinds the control to the first line, ready for the next cycle

Note the M02/M30 distinction, because it confuses people: both end the program, but M30 also rewinds the control back to the first line and resets it for the next cycle, which is what production running needs. M02 is the older convention and still appears in legacy programs. Beyond this list, machine builders define their own M codes — M60-series pallet changes, probe enable codes, chip-conveyor commands — so the edges of the M-code map are always control-specific.

Reading a Real CNC Program, Line by Line

Here is a short but genuine milling program — the kind a programmer might write to face the top of a small aluminum bracket with a 10 mm endmill. It is deliberately simple, but every structural element of a production program is present: safety line, work offset, tool change, spindle start, cutting moves, and a clean shutdown. Text in parentheses is a comment; the control ignores it.

Line-by-line walkthrough of a short CNC milling program
Program LineWhat It Does
%Program start delimiter.
O0001 (BRACKET TOP FACE)Program number O0001; text in parentheses is a comment the control ignores.
G21 G17 G90 G94Safety line: metric units, XY plane, absolute positioning, feed per minute — a known, sane starting state.
G54Select work offset 1, telling the machine where part zero is.
T1 M06 (10MM FLAT ENDMILL)Load tool 1 with an automatic tool change; the comment reminds the operator what tool 1 is.
S6000 M03Start the spindle clockwise at 6,000 RPM.
G00 G43 H01 Z25.0 M08Rapid to Z 25 mm while applying tool length offset H01, then turn coolant on.
G00 X-15.0 Y-15.0Rapid in X and Y to the start of the cut, off the part corner.
G01 Z0.0 F300Feed down to Z zero at 300 mm/min.
G01 X65.0 F600Cut a straight pass along X at 600 mm/min.
G01 Y15.0Step over 30 mm in Y; the next lines cut back along X and return to the start, facing the top of the bracket.
G00 Z25.0 M09Rapid clear of the part and turn coolant off.
M05Stop the spindle.
G28 G91 Z0Send Z home to the machine reference point (incremental mode keeps this move safe).
G90Restore absolute positioning.
M30End of program: stop everything and rewind to the first line for the next part.
%Program end delimiter.

Read it once through and the structure becomes clear: state the modes, establish part zero, load the tool, start the spindle, cut, then shut everything down and go home. Production programs are this skeleton scaled up — thousands of motion lines between the same opening and closing acts. Notice also how few distinct codes it takes: G00, G01, G21, G17, G90, G94, G54, G43, G28, G91, plus M03, M05, M06, M08, M09, M30. Sixteen commands cover the whole job.

Close-up of a CNC machined part whose toolpath was defined by modal G code motion commands

Most G codes and many M codes are modal: once issued, they stay in force until another code from the same group replaces them. G01 is modal — after one G01 line, every subsequent motion line keeps feeding in a straight line until a G00, G02, or G03 appears, so programmers write only the coordinates on following lines. The modal groups are organized so that codes within a group are mutually exclusive: G00/G01/G02/G03 form the motion group, G90/G91 the positioning group, G20/G21 the units group, G54–G59 the offset group. One code per group is always active, whether the program mentions it or not.

Non-modal codes act only on the line where they appear. G04 dwell — pause for a programmed time — is the textbook example: it delays that one block and is then gone. Some machine-specific commands behave this way too.

The practical consequence is that a CNC control has a hidden state, and that state survives between programs. If a previous job left the machine in incremental mode (G91) or with an unusual work offset active, and the next program assumes absolute mode, the first motion line drives the tool somewhere dangerous. This is why every well-written program opens with a safety line — the G21 G17 G90 G94 block in the example above — that forces all critical modal groups to known values before anything moves. When you see a programmer insist on this line, they are not being fussy; they have paid for the lesson already.

Why Buyers Should Know a Little G Code

You will never be asked to write G code — send a STEP file and a drawing, and the shop’s CAM software and programmers handle the rest. But a basic reading knowledge makes you a sharper buyer in three concrete ways. First, programming is a real cost line in every quote. Before the first chip is cut, someone must build toolpaths, choose tools and offsets, post the program for the specific control, and prove it out. A part with 3D surfacing, dozens of features, or multiple setups takes proportionally more programming hours — which is why two similar-looking parts can price very differently, a dynamic we break down in our guide to CNC machining cost.

Second, it improves the conversations you have about design for manufacture. When a shop tells you a deep internal corner or a non-standard radius is adding programming and cycle time, you now know what is actually happening at the control — the programmer is fighting arc definitions, compensation, and rest-machining passes — and a small drawing concession can remove the problem entirely. Third, it helps you sanity-check lead times: complex parts carry a programming phase before machining starts, so a “we can start cutting tomorrow” promise on a five-axis surfacing part deserves a follow-up question.

The machining process you choose also shapes the program behind it — a Swiss lathe program with overlapping main and sub-spindle work reads very differently from a three-axis milling program — so it pairs naturally with our overview of the types of CNC machining. None of this requires you to program; it requires you to recognize what the machine is being asked to do.

FAQ: G Code and M Code

What is G code in CNC machining?

G code is the programming language that tells a CNC machine how to move. Each G command is a preparatory instruction: G00 rapids to a position, G01 cuts in a straight line at a set feed rate, G02 and G03 cut clockwise and counterclockwise arcs, and dozens of other codes set units, planes, offsets, and compensation modes. The language is standardized under RS-274 and ISO 6983, so the core vocabulary is consistent across Fanuc-style controls found on most mills and lathes. A part program is simply a numbered sequence of these instructions that the control executes line by line.

What is the difference between G code and M code?

G codes control geometry and motion — where the tool goes, how fast it feeds, and which coordinate system it works in. M codes control the machine's auxiliary functions — starting and stopping the spindle, changing tools, and switching coolant on and off. A useful way to remember it: G codes cut the part, M codes run the machine. Most program lines combine them: a line might rapid the tool to position with G00 while the next line starts the spindle with M03 and turns coolant on with M08. Both families are modal-aware, so the programmer must track which state is active at all times.

What are the most common M codes on a CNC machine?

The handful you will see in nearly every program are M03 and M04 to start the spindle clockwise or counterclockwise, M05 to stop it, M06 for an automatic tool change, and M08/M09 to switch coolant on and off. Program flow is handled by M00 (unconditional program stop), M01 (optional stop, active only when the operator enables it), and M30 (end of program with rewind, which resets the control to the first line for the next cycle). M02 also ends a program but is the older convention without automatic rewind. Machine builders add their own M codes for pallets, probes, and chip conveyors.

What does modal mean in G code programming?

Modal codes stay in effect until another code from the same group replaces them. If a program calls G01 once, every following motion line continues to feed in a straight line until a G00, G02, or G03 appears — you do not repeat G01 on every line. The same applies to G90/G91 positioning, G20/G21 units, and G54–G59 work offsets. Non-modal codes, by contrast, act only on the line where they are written; G04 dwell is the classic example. This is why experienced programmers open every program with a safety line that explicitly sets the modal state, so leftover modes from a previous run cannot cause a crash.

Is G code the same on every CNC machine?

The core vocabulary is. Fundamental ANSI/ISO codes — G00 through G03 for motion, G20/G21 for units, G54–G59 for offsets, G90/G91 for positioning, and the common M codes like M03, M06, and M30 — work the same on virtually every Fanuc-style control, which covers most of the industry. Beyond that core, controls diverge: Haas, Siemens, Heidenhain, and Mazak each add proprietary cycles and code maps, and fixed cycles like G81 drilling can differ in their parameters. A program written for one control usually needs a post-processor adjustment before it runs safely on another.

Do I need to know G code to order CNC machined parts?

No — you send a CAD model and a drawing, and the machine shop's CAM software and programmers generate the G code. You will never be asked to write it. But a little literacy pays off when you read quotes: programming and setup time is a real line item, and knowing that a part full of 3D surfacing, tight corner radii, or multiple setups means more programming hours helps you understand why two visually similar parts can price very differently. It also helps you ask sharper questions about lead time, since programming for a new part happens before the first chip is cut.

You Bring the Drawing. We Write the Program.

IsoCNC’s engineers program, set up, and machine your parts on 3-, 4-, and 5-axis mills, lathes, and Swiss-type turning centers — with DFM feedback on every RFQ and inspection reports on every lot. Upload a STEP file and drawing; an engineer typically responds within 1–2 business days.