CNC G-Code and M-Code List with Examples (Milling and Lathe)

Engineers R World – CNC G-Code and M-Code List  |  engineersrworld.com  |  engineersrworld@gmail.com  |  WhatsApp: +91 89197 41799

Quick answer: In CNC programming, G-codes (preparatory codes) tell the machine how to move – rapid move G00, straight cut G01, arcs G02/G03, units G21, absolute mode G90 – and M-codes (miscellaneous codes) switch machine functions – spindle on M03, coolant on M08, tool change M06, end of program M30. Below is the complete list of commonly used codes for milling and turning, with two full example programs.

 Keep this list next to the machine or save it for exams.

Structure of a CNC program

A CNC program is a list of blocks (lines). Each block contains words made of a letter and a number. The codes below follow the ISO (RS-274) standard used by most CNC controllers; canned cycles can differ slightly between machines, so always check the machine manual.

LetterMeaning
NBlock (line) number
GPreparatory function – type of motion or mode
MMiscellaneous function – machine on/off actions
X, Y, ZCoordinates of the end point (on a lathe X is usually a diameter)
I, J, KArc centre offsets from the start point (or R for radius)
FFeed rate (mm/min or mm/rev)
SSpindle speed (rpm or m/min)
TTool number (and offset on a lathe, e.g. T0101)
D / HCutter radius offset number / tool length offset number

Example block: N60 G01 X50.0 Y30.0 F200 – block 60, straight cut to X50 Y30 at 200 mm/min.

G-code list

CodeFunctionUsed on
G00Rapid positioning (non-cutting move)Mill / Lathe
G01Linear interpolation (straight cut at feed F)Mill / Lathe
G02Circular interpolation, clockwiseMill / Lathe
G03Circular interpolation, counter-clockwiseMill / Lathe
G04Dwell (pause for a set time)Mill / Lathe
G17 / G18 / G19Select XY / XZ / YZ planeMill (G18 default on lathe)
G20 / G21Inch / millimetre unitsMill / Lathe
G28Return to machine reference (home) positionMill / Lathe
G40Cancel cutter radius compensationMill / Lathe
G41 / G42Cutter compensation left / right of the pathMill / Lathe
G43 / G49Tool length offset on / cancelMill
G54 – G59Work coordinate systems (work offsets)Mill / Lathe
G70Finishing cycleLathe
G71Stock removal (rough turning) cycleLathe
G76Multi-pass threading cycleLathe
G80Cancel canned cycleMill
G81Simple drilling cycleMill
G82Drilling with dwell at bottom (spot face, counterbore)Mill
G83Peck drilling cycle (deep holes)Mill
G84Tapping cycleMill
G90 / G91Absolute / incremental programmingMill
G94 / G95Feed per minute / feed per revolutionMill
G96 / G97Constant surface speed / constant rpmLathe
G98 / G99Feed per minute / feed per revolutionLathe

M-code list

CodeFunction
M00Program stop (operator must restart)
M01Optional stop (only if the optional-stop switch is on)
M02End of program
M03Spindle on, clockwise
M04Spindle on, counter-clockwise
M05Spindle stop
M06Tool change
M08Coolant on
M09Coolant off
M30End of program and rewind to start
M98Call a subprogram
M99End of subprogram / return to main program

Example 1: Milling program – rectangular profile

This program cuts the outside of a 50 × 30 mm rectangle, 2 mm deep, with a 10 mm end mill. The work zero (G54) is at the lower-left corner of the part.

O1001 (PROFILE 50 X 30 RECTANGLE, 10 MM END MILL)
N10 G21 G17 G90 G40 G80      (mm, XY plane, absolute, cancel comp and cycles)
N20 T01 M06                  (tool 1)
N30 G54 G00 X-10.0 Y-10.0 S1200 M03
N40 G43 H01 Z5.0 M08         (tool length offset, coolant on)
N50 G01 Z-2.0 F100           (plunge 2 mm deep)
N60 G41 D01 X0 Y0 F200       (start cutter compensation, left)
N70 Y30.0
N80 X50.0
N90 Y0
N100 X-5.0
N110 G40 X-10.0 Y-10.0       (cancel compensation)
N120 G00 Z50.0 M09
N130 M05
N140 M30

Example 2: Lathe program – step turning

This program turns a 40 mm bar down to 30 mm diameter over a length of 25 mm in two passes. On a lathe, X values are diameters and Z = 0 is the front face of the part.

O2001 (TURN 40 MM BAR TO 30 MM FOR 25 MM LENGTH)
N10 G21 G40 G99              (mm, cancel comp, feed per rev)
N20 T0101                    (tool 1, offset 1)
N30 G97 S800 M03
N40 G00 X42.0 Z2.0 M08
N50 X35.0                    (first pass diameter)
N60 G01 Z-25.0 F0.2
N70 X42.0
N80 G00 Z2.0
N90 X30.0                    (final diameter)
N100 G01 Z-25.0
N110 X42.0
N120 G00 X100.0 Z100.0 M09
N130 M05
N140 M30

Tips for writing CNC programs

  • Start every program with a safety block (G21 G90 G40 G80 or the lathe equivalent) to cancel old modes.
  • Use G00 only in clear air; approach the part at least 2 mm away before switching to G01.
  • Turn on coolant (M08) before cutting and switch it off (M09) before the tool retracts home.
  • Check the program with a dry run or simulation before cutting metal.
Prepared by Engineers R World. Free to share for study. Website: engineersrworld.com · Email: engineersrworld@gmail.com · WhatsApp: +91 89197 41799

CNC quiz: 10 MCQs

Try it as a quiz: tap an option to answer. You will see right or wrong at once, and your score is shown at the bottom-left of the screen.

Q1. Which G-code is used for rapid positioning?

  1. G00
  2. G01
  3. G02
  4. G04
Show answer

A. G00 G00 moves at rapid traverse without cutting.

Q2. G01 is used for:

  1. Rapid move
  2. Straight-line cutting at feed rate F
  3. Clockwise arc
  4. Dwell
Show answer

B. Straight-line cutting at feed rate F G01 is linear interpolation at the programmed feed.

Q3. Which code produces a clockwise arc?

  1. G03
  2. G01
  3. G02
  4. G17
Show answer

C. G02 G02 is clockwise, G03 is counter-clockwise.

Q4. Which M-code starts the spindle clockwise?

  1. M05
  2. M04
  3. M08
  4. M03
Show answer

D. M03 M03 = spindle CW, M04 = CCW, M05 = stop.

Q5. G90 sets:

  1. Absolute programming
  2. Incremental programming
  3. Inch units
  4. Tool change
Show answer

A. Absolute programming In absolute mode all coordinates are from the work zero.

Q6. Which code selects millimetre units?

  1. G20
  2. G21
  3. G28
  4. G40
Show answer

B. G21 G21 = mm, G20 = inch.

Q7. M30 means:

  1. Coolant on
  2. Tool change
  3. End of program and rewind
  4. Optional stop
Show answer

C. End of program and rewind M30 ends the program and returns to the start.

Q8. Cutter radius compensation to the left of the tool path is:

  1. G40
  2. G42
  3. G43
  4. G41
Show answer

D. G41 G41 = left, G42 = right, G40 = cancel.

Q9. G83 is a:

  1. Peck drilling cycle
  2. Tapping cycle
  3. Threading cycle
  4. Boring cycle
Show answer

A. Peck drilling cycle Peck drilling retracts the drill to clear chips in deep holes.

Q10. In the block ‘G01 X50 F200’, F200 is the:

  1. Spindle speed
  2. Feed rate
  3. Tool number
  4. Coordinate
Show answer

B. Feed rate F sets the feed rate, here 200 mm/min in G94 mode.

Frequently asked questions

What is the difference between G-code and M-code?

G-codes control motion and modes such as straight cuts, arcs, units and coordinate systems. M-codes control machine functions such as spindle on/off, coolant and tool change.

Is G-code the same on every CNC machine?

The core codes (G00–G03, G90/G91, M03/M05, M30) follow the ISO standard and are the same on most machines, but canned cycles and some codes differ between controllers, so always check the machine manual.

What is G28 used for?

G28 sends the machine axes back to the machine reference (home) position, usually before a tool change or at the end of a program.

Why is cutter radius compensation used?

It lets you program the part outline directly; the controller offsets the tool by its radius (G41 or G42), so the same program works when tool size changes slightly.

Related notes and projects

Learn the design side with our machine design formula sheet, machine design notes and machine design MCQs, or browse mechanical design and analysis projects.

Want to learn CAD, CAM and analysis?

We teach CAD modelling, FEA, CFD and Python for engineers through live 1-to-1 screen-share classes or recorded videos – you do every step on your own computer. Message us on WhatsApp or email engineersrworld@gmail.com.

Scroll to Top