📐 Free Gage Block Calculator — no login. Open it →

← AskMachinist · Guides · Calculator · 🔎 Search

CNC G-Code and M-Code Basics

Guide · Updated October 28, 2026

G-code tells a CNC machine where to move and how. G-codes are preparatory codes for motion and modes, and M-codes are miscellaneous machine functions like spindle, coolant and tool change. Most machines follow the Fanuc style described in ISO 6983, but builders differ in details, especially canned cycles, tool change and macro features. Always check your control manual before running a program, and prove it out with single block and a reduced rapid override.

Need an exact reference size?

Build any gage block stack with the free Gage Block Calculator. Free, no login.

Step by step

  1. Start safe: Cancel offsets and cycles with a safety line such as G17 G20 G40 G49 G80 G90.
  2. Call the tool: T1 M06 on a mill, then set spindle speed and direction with S and M03.
  3. Pick up the work offset: G54 to G59 select the part zero set at the machine.
  4. Pick up tool length: G43 H1 Z1.0 applies tool length offset 1 while moving to a clearance height.
  5. Cut: Use G00 rapid to position, G01, G02 and G03 to cut at the programmed F feed rate.
  6. End safely: Retract, turn off spindle and coolant, return home and end with M30.

Common G-codes (mill, Fanuc style)

On lathes, G96 is constant surface speed, G97 is constant rpm, G50 or G92 sets a maximum spindle speed (control dependent) and G71 or G76 are common roughing and threading cycles.

Common M-codes

M-codes above about M30 are often builder specific, for example through-spindle coolant or chip conveyor.

Example: drill four holes

Each hole uses G83 peck drilling 0.100 in at a time to Z−0.600, retracting to the R plane at Z0.1 between holes. Because the codes are modal, only the changing coordinate is needed on each new hole line.

Worked example: depth for a through hole

To drill through 0.500 in plate with a 1/4 in 118° drill, add the drill point length, 0.300 × 0.250 = 0.075 in, plus about 0.025 in breakthrough: Z = −(0.500 + 0.075 + 0.025) = −0.600, which is the depth in the example.

Worked example: feed and speed lines

For the 1/4 drill at 160 SFM in mild steel, rpm = 3.82 × 160 ÷ 0.25 = 2445, so S2500 is close. At 0.003 in per revolution, F = 2500 × 0.003 = 7.5 ipm, so F8.0 is about right. In G94 (feed per minute, the mill default) F is in inches per minute; in G95 it is per revolution.

Proving out a program

Run with the work offset shifted up in Z or in graphics first, then single block with rapid at 25% and a hand on feed hold. Check the distance-to-go on the first approach of every tool. Confirm tool lengths and diameters in the offset page match the tools actually in the magazine.

Common problems

Forgetting G43 so the tool rapids at the wrong height, leaving G91 active after a G28 line, mixing G20 and G21, missing a decimal point (on many controls X1 means 0.0001 in, not 1 in), and wrong cutter compensation side. Verify the first part with a mic, height gage or CMM before running more.

FAQ

What is the difference between G00 and G01?

G00 is rapid positioning; G01 is a straight line at the programmed feed.

What does G54 do?

Selects work coordinate system 1, the part zero you set on the machine.

What does M30 do?

Ends the program and rewinds to the start.

How do I check a CNC setup?

Check the gage itself before trusting a measurement: gage blocks set mics, calipers and indicators; build any stack free at gageblockcalc.com.

Related guides and calculators

Build your stack in seconds

The free Gage Block Calculator finds the fewest-block combination for inch and metric sets, with wear blocks. No login.

Questions? Ask a machinist.

General shop information only — follow your drawing, tool maker's data and your quality system.