What Is G Code and Mcode in CNC Machine Control
G code tells the machine where and how fast to move. M code switches non-motion functions on and off: spindle, coolant, tool change, program end. This page explains how the two work together, which codes matter on a 3-axis mill versus a mill-turn center, and where programming choices start to affect tolerance and cycle time.

In this article
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
Key takeaways
How G Code and Mcode in CNC Machine Work Together
A CNC program is a list of blocks. Each block is one line of text, and most controllers read it word by word: a letter address plus a number. G words handle motion and geometry. M words handle machine state. X, Y, Z, I, J, K, F, S and T carry the values those words need.
The split is not arbitrary. Motion is something the controller can calculate and verify in advance. Machine state is a physical action: a spindle ramping to 12,000 rpm, a coolant pump starting, an automatic tool changer swinging an arm. The controller has to sequence those actions around the motion so nothing collides.
That is why a typical block looks like N120 G01 X45.0 Y12.5 F250 M08. The tool moves in a straight line at 250 mm/min while coolant turns on. The M08 does not wait for the move to finish in most controls. It fires in parallel, because flooding the cut early is better than flooding it late.
The two families are also standardized differently. G and M codes follow ISO 6983, but every builder adds its own codes on top. A Fanuc M code list is not the same as a Siemens list. When a shop moves a program between machines, the post-processor has to be rebuilt, not just copied.
- 1Word address formatLetter plus number, one block per line, no separators required.
- 2Two code familiesG for motion and offsets, M for machine functions and program flow.
- 3Parallel executionMany M codes run while the axis is still moving.
- 4Vendor extensionsProbing, high-speed look-ahead and pallet change codes vary by builder.
The G Codes That Cover Most Milling and Turning Work
G00 is rapid positioning. The machine moves at its fastest traverse rate to a point, usually above the part. G01 is a straight feed move at the programmed feed rate. G02 and G03 are circular interpolation, clockwise and counterclockwise. Between them, these four codes describe nearly every path a machinist cuts.
Offsets come next. G54 through G59 set the work coordinate system, so the program's zero point can sit at a corner of the stock instead of machine zero. G41 and G42 apply cutter radius compensation, letting the programmer define the part geometry and let the control shift the path by the tool radius. G43 applies tool length offset from the tool table.
Canned cycles compress repetitive drilling and tapping. G81 is a simple drill cycle. G83 pecks in steps, which clears chips in deep holes. G84 taps. On a lathe, G71 and G70 handle rough and finish turning passes. One G83 block with a Q value can replace 40 lines of G01 and G00.
High-speed machining options change how these codes execute. Look-ahead buffers thousands of blocks and smooths corners so the machine does not stop at every junction. Without it, a complex 3D surface program runs at a fraction of the programmed feed. With it, the same program holds tolerance and cuts cycle time.
- 1G00 / G01Rapid traverse and linear feed, the two most common motion codes.
- 2G02 / G03Circular interpolation with I, J, K center offsets or an R radius.
- 3G41 / G42Cutter compensation left or right of the path direction.
- 4G81 / G83 / G84Drill, deep-hole peck drill and tap cycles.
M Codes: Spindle, Coolant, Tool Change and Program Control
M03 starts the spindle clockwise. M04 runs it counterclockwise. M05 stops it. The speed comes from the S word in the same block, so M03 S8000 spins at 8,000 rpm. On a lathe, M03 usually means the spindle turns so the top of the insert cuts, which is why the direction matters more than on a mill.
M06 is a tool change. On machines with an automatic tool changer, M06 T07 sends the carousel to pocket 7 and swaps it into the spindle. The controller also reads the tool length offset from the table at that point, so the next G43 does not need the length typed out.
Coolant is M07 for mist, M08 for flood and M09 to stop. The choice is not cosmetic. Flood coolant removes heat from deep pockets and flushes chips. Mist is common in aluminum at high spindle speeds where flood would splash and where chip evacuation is already good. On titanium and stainless, flood is the default because heat stays in the cut.
Program flow uses M00 for a stop, M01 for an optional stop, M02 for program end without reset and M30 for program end with reset. M30 is the one most shops put at the bottom of a file. It rewinds to the top and clears modal state, ready for the next part.
- 1M03 / M04 / M05Spindle forward, reverse and stop.
- 2M06Automatic tool change, usually paired with a T word.
- 3M07 / M08 / M09Mist coolant, flood coolant, coolant off.
- 4M30End of program with rewind and modal reset.
Why Modal State Causes Most Programming Errors
Modal means the code stays in effect after the block that called it. If line 50 says G01 and line 51 only has X and Y, the control still feeds at the same rate along a straight line. This saves thousands of characters in a long program, but it also means a single stale G code can ruin a part.
The classic case is a missed G00. A programmer leaves out the rapid move after a cut and the next block feeds at cutting speed into the stock. Or a G41 stays active after a contour and the next pocket is offset by the tool radius. Both errors trace back to modal state that nobody cancelled.
Safety blocks exist for this reason. Many shops end a toolpath with G40 to cancel cutter compensation, G49 to cancel tool length offset and G80 to cancel canned cycles. It costs three short lines. It prevents a scrap part that costs far more than the programming time saved.
On a 5-axis machine, the problem compounds. G43.4 and G43.5 for tool center point control change how the rotary axes are calculated. If one of those modes stays active into the next operation, the post-processor output no longer matches the machine's interpretation and the tool can drive into the fixture.
- 1Modal G codesG00 to G03, G40, G41, G43, G80 all persist across blocks.
- 2Non-modal codesG04 dwell and G53 machine coordinate move apply once.
- 3Reset habitEnd each operation with G40, G49 and G80.
- 45-axis cautionCancel TCP modes before switching to a new operation.
What Code Choice Changes on the Machine
Programming decisions show up in three places: tolerance, cycle time and tool life. A trochoidal path with a small radial depth of cut keeps the tool engaged at a steady angle, which holds ±0.005 mm on a deep pocket and extends carbide life. A conventional full-width pass may finish faster in theory and fail in practice on the same feature.
Cycle time moves with feed rate and rapid distance. Raising feed from 250 to 400 mm/min helps only if the tool and the setup can take it. On a thin-wall aluminum part, the same change causes chatter and forces a second finishing pass. The code is correct. The physics is not.
Tool life is a code question too. G83 peck drilling with a Q value of 3 mm clears chips and avoids re-cutting on deep holes. A G81 straight drill on the same hole loads the flutes with chips and burns the tip. The material and depth decide which cycle is right, not habit.
At GreatLight, 127 high-precision CNC machines run programs across 3-axis, 4-axis, 5-axis and mill-turn centers. The largest travel is 4,000 × 400 × 150 mm. Programming is matched to the machine that will cut the part, because a program that runs clean on one control can behave differently on another.
- 1Toolpath shapeTrochoidal paths hold tolerance in deep pockets.
- 2Feed and speedHigher feed helps only when the setup is rigid enough.
- 3Canned cycle choicePeck drilling protects tool life in deep holes.
- 4Machine matchingPrograms are posted for the specific control and machine.
Reading a CNC Program Block by Block
A practical way to check a program before it runs.
- 1Find the setup blockLook for G54 and the first G43. Confirm the work offset and tool length match the setup sheet.
- 2Trace the first motionCheck that the first G00 clears the fixture. Rapid height of 5 to 10 mm above stock is typical.
- 3Identify the cutting movesG01 with an F value is a feed move. Note the feed rate and compare it to the tool's recommended range.
- 4Check compensationG41 or G42 should appear before the contour starts and G40 after it ends. Both matter equally.
- 5Verify spindle and coolantM03 with an S value before the first cut. M08 on for steel and titanium, M07 for aluminum at high rpm.
- 6Scan the end of each operationG40, G49 and G80 cancel modal state. M09 stops coolant before the retract.
- 7Confirm program endM30 rewinds and resets. M02 stops without rewind and leaves modal state active.
G Code vs M Code: What Each One Controls
Same block, two jobs. G words move the tool, M words change machine state.
| Item | G code | M code |
|---|---|---|
| Primary job | Position and path geometry | Machine state and program flow |
| Typical examples | G00, G01, G02, G41, G43 | M03, M06, M08, M30 |
| Uses numeric values | X, Y, Z, I, J, K, F, R, Q | S and T, or no value at all |
| Modal behavior | Stays active until another G replaces it | Most are momentary actions |
| Execution timing | Synchronized with the motion block | Often starts in parallel with motion |
| Vendor variation | Low, ISO 6983 covers the core set | Higher, builder-specific M codes are common |
| Failure symptom | Wrong position, gouge, scrap part | Spindle fault, crash, tool breakage |
When to trust the code and when to check it
If the part is simple and the material is aluminum, a verified post and a dry run are usually enough. If the part has deep pockets, thin walls or 5-axis features, trace the modal state and cancel codes line by line before the first cut.
Frequently asked questions
Do I need to send G code with my CAD files?
No. Send the 3D model and a 2D drawing with tolerances and material. Programming is part of the machining service.
If you already have a proven program for a specific machine, send it. We will check whether it matches the control and the setup before using it.
Can the same G code run on any CNC machine?
Not directly. ISO 6983 covers the core motion codes, but tool change, probing and high-speed options are vendor-specific.
A program posted for a Fanuc control may need changes for a Siemens or Haas control. The geometry stays the same. The M codes and some G codes do not.
What causes a CNC machine to cut the wrong depth?
Most often the tool length offset is wrong or G43 was called with the wrong H number. The machine then uses a stale length from the previous tool.
A second cause is a missing G49 between operations. The old offset stays active and shifts the Z position on the next toolpath.
Is M30 better than M02 at the end of a program?
M30 is the safer default. It ends the program, rewinds to the top and clears modal state so the next cycle starts clean.
M02 ends the program without rewinding. It is used in subprograms and in some bar feeder setups where the control handles the reset differently.
How do G codes affect surface finish?
Feed rate, stepover and toolpath shape set the theoretical finish. A constant-engagement path at 250 to 400 mm/min with a 0.1 to 0.3 mm stepover gives a more even finish than a full-width pass.
The machine still has to hold it. Look-ahead, rigid fixturing and a sharp tool decide whether the programmed finish appears on the part. On our machines, finishes run from Ra 1.6–3.2 μm as machined down to Ra 0.2–0.8 μm when fine finishing is specified.
Can you machine a part from a hand sketch?
We can start from a sketch if it carries enough dimensions to build a model. A sketch with key dimensions, tolerances and material is usually enough for a quote.
For anything with mating features, a 3D model is faster and removes interpretation risk. DFM feedback comes back with the quote within 12 hours.
Send a part file and get a manufacturability check
Upload a model and a drawing. We review the geometry, material and tolerances, then quote with DFM notes within 12 hours.
12-hour quoteFree DFM analysisNo minimum order quantity100% inspection before shipment