# CNC Programming and G-Code: A Complete Reference

https://cncoutlook.com/guides/cnc-programming-g-code-reference/

By CNC Outlook Editorial · 2026-10-28

How CNC programs are written, how to read a block of G-code, the G-codes and M-codes used most often on mills and lathes, an annotated drilling program, and how controller dialects differ.

CNC programming is the work of writing the instructions a CNC machine follows to make a part. Most of those instructions are written in G-code, a language of letter-and-number commands that tell the machine where to move, how fast, and when to switch the spindle, coolant and tools on and off. Today CAM software writes most programs, but machinists still read and edit G-code at the machine every day.

This reference explains how a program is put together, lists the G-codes and M-codes used most often, walks through a short drilling program line by line, and covers the differences between controllers.

**The short version**

- A program is a series of lines called blocks. Each block holds one or more words, such as G01, X50.0 or F300.
- G-codes set motion and modes; M-codes switch machine functions such as the spindle, coolant and tool changes.
- Many codes are modal: they stay active until another code in the same group replaces them.
- Code meanings vary between controllers, and between mills and lathes. Always check the programming manual for the specific control.
- Simulate every new or edited program before running it on a machine.

## Three ways to write a CNC program

| Method | How it works | Best suited to |
|---|---|---|
| Manual programming | The programmer writes G-code by hand, line by line | Simple parts, edits at the machine, learning |
| Conversational programming | The programmer answers prompts on the machine control, which generates the program | One-off and simple parts written at the machine |
| CAM programming | Software generates tool paths from a 3D model, and a post-processor converts them into G-code for one machine | Complex shapes, 3D surfaces, multi-axis work, production |

The methods overlap in practice. A shop may program a complex part in CAM, then adjust feeds or add a probing cycle by hand at the control. Controls such as HEIDENHAIN's TNC series offer their own conversational language, Klartext, alongside standard ISO G-code programming, each documented in a separate manual.

CAM is also where software makers are adding automation. Autodesk's [System Modeler for Fusion](https://cncoutlook.com/news/autodesk-fusion-october-2026-system-modeler-turning-cam-update/) chains steps such as feature recognition, tool selection and toolpath creation into reusable rules. [GibbsCAM's 2027 Copilot](https://cncoutlook.com/news/gibbscam-2027-copilot-ai-limits-programming/) is more limited: it helps users find information but does not yet understand the job open in the CAM session.

## How to read a block of G-code

Each line of a program is a block. It may start with a line number and then contains one or more words: a letter followed by a number. In the LinuxCNC reference's words, many commands are modal, meaning "the mode stays active until some other command changes it."

Take this block:

```text
N40 G01 X50.0 Y20.0 F300
```

N40 is the line number. G01 selects a straight-line move at the feed rate. X50.0 Y20.0 is the end point, and F300 sets the feed, here 300 mm per minute in a metric program. Because G01 is modal, the next block can contain just `X80.0` and the machine will make another straight cut to X80.

The letters have fixed meanings on most controls:

| Letter | Meaning |
|---|---|
| N | Line (sequence) number |
| O | Program number on Fanuc-style controls |
| G | Preparatory function: motion type, plane, units, offsets, cycles |
| M | Miscellaneous function: spindle, coolant, tool change, program stop |
| X, Y, Z | Linear axis positions |
| A, B, C | Rotary axis positions |
| I, J, K | Arc center offsets from the start point |
| R | Arc radius, or the retract level in a canned cycle |
| F | Feed rate |
| S | Spindle speed |
| T | Tool number |
| H | Tool length offset number |
| D | Cutter radius compensation number |
| P | Dwell time or subprogram number, depending on the code |
| Q | Peck depth in drilling cycles |

## G-code list for CNC mills

These are the G-codes found on almost every milling program. The descriptions follow the common Fanuc-style convention, which most mill controls share.

| Code | Function | Notes |
|---|---|---|
| G00 | Rapid move | Positioning at maximum traverse speed; never for cutting |
| G01 | Linear move at feed rate | The standard cutting move |
| G02 | Clockwise arc | Defined by an end point and I/J/K offsets or R |
| G03 | Counterclockwise arc | Direction is viewed from the positive end of the axis |
| G04 | Dwell | Pauses axis motion for a set time, given with P |
| G17, G18, G19 | Plane selection | XY, ZX and YZ; arcs and cycles work in the active plane |
| G20, G21 | Units | Inch or millimeter |
| G28 | Return to reference position | Usually used to send Z home before a tool change |
| G40 | Cutter compensation off | Cancels G41 or G42 |
| G41, G42 | Cutter compensation left, right | Offsets the path by the tool radius, left or right of the programmed line |
| G43 | Tool length compensation | Applies the length offset named by the H word |
| G49 | Cancel tool length compensation | |
| G54 to G59 | Work coordinate systems | Select where part zero is; G54 is the most common |
| G80 | Cancel canned cycle | |
| G81 | Drilling cycle | Feed to depth, rapid out |
| G82 | Drilling with dwell | Pauses at the bottom, for spot drilling or counterboring |
| G83 | Peck drilling | Retracts between pecks to clear chips from deep holes |
| G84 | Tapping cycle | Right-hand tapping; feed must match spindle speed times thread pitch |
| G85 | Boring cycle | Feed in and feed out |
| G90, G91 | Absolute, incremental | Positions measured from part zero, or from the current point |
| G98, G99 | Canned cycle return level | Retract to the starting height, or to the R level |

Canned cycles such as G81 to G85 do the most work in the fewest lines. One block sets the cycle, and each following block containing only X and Y drills another hole until G80 cancels it.

## M-code list

| Code | Function |
|---|---|
| M00 | Program stop; the operator restarts the cycle |
| M01 | Optional stop; stops only when optional stop is switched on |
| M02 | End of program |
| M03 | Spindle on clockwise |
| M04 | Spindle on counterclockwise |
| M05 | Spindle stop |
| M06 | Tool change |
| M07, M08 | Mist coolant, flood coolant on |
| M09 | Coolant off |
| M19 | Spindle orientation |
| M30 | End of program and reset to the start |
| M98, M99 | Subprogram call and return |

M-codes vary more between machines than G-codes, because many switch functions that only some machines have, such as pallet changers, chip conveyors or through-spindle coolant.

## An annotated drilling program

The program below drills four holes 20 mm deep with a peck drilling cycle on a vertical mill. It shows the structure of a typical program on a Fanuc-style control. Speeds, feeds and offsets depend on the machine, tool and material, so check the codes against your control's manual and simulate the program before running it.

```text
%
O1001 (FOUR-HOLE PECK DRILL EXAMPLE)
G21 G17 G40 G49 G80 G90      (metric, XY plane, cancel offsets and cycles, absolute)
T2 M06                       (load tool 2, a 6.8 mm drill)
G54                          (use work offset 1)
S1800 M03                    (spindle on clockwise, 1,800 rpm)
G00 X10.0 Y10.0              (rapid to the first hole)
G43 Z25.0 H02 M08            (apply length offset 2, rapid to Z25, coolant on)
G99 G83 Z-20.0 R2.0 Q4.0 F150 (peck drill to Z-20 in 4 mm pecks, retract to R2)
X50.0                        (second hole)
Y40.0                        (third hole)
G98 X10.0                    (fourth hole, then retract to the starting height)
G80 M09                      (cancel the cycle, coolant off)
G28 G91 Z0                   (send Z to the machine reference position)
M05                          (spindle stop)
M30                          (end of program, reset)
%
```

Line three is a safety line. It cancels anything an earlier program may have left active, such as cutter compensation or a canned cycle, so the program starts from a known state.

The G43 line needs particular care. If the H number doesn't match the tool in the spindle, the machine works with the wrong tool length and can drive the drill into the part or the vise.

## Work offsets and tool offsets

A program describes the part, not the machine. Two sets of offsets connect them.

Work offsets (G54 to G59) tell the control where part zero is in the machine's travel. The machinist measures the part with a probe or an edge finder and stores the position. The same program can then run on a part clamped anywhere on the table, or on several parts held in different fixtures, each with its own offset.

Tool length offsets (G43 with H) store how far each tool sticks out of the spindle, so the program can be written as if every tool had the same length. Cutter radius compensation (G41 and G42 with D) does the same job for diameter. It lets a machinist adjust a finished size by changing an offset value instead of editing the program.

## Controller dialects: why a program rarely runs everywhere

G-code has a shared core, but controls do not all implement it the same way. The open-source LinuxCNC control, for example, documents its language as based on RS274/NGC. Every control builder adds its own extensions, and the same code can mean different things on different machines.

The differences show up in canned cycle parameters, macro and variable syntax, subprogram calls, and especially between mills and lathes. On a lathe, X is normally programmed as a diameter, G96 sets a constant surface speed, and cycles such as G71 (roughing), G70 (finishing) and G76 (threading) do much of the work. Some codes that exist on both machine types have different meanings on each.

CAM software handles this through post-processors, one for each machine and control. A program posted for one machine should be treated as untested on any other until it has been checked and simulated.

## Learning CNC programming

Most machinists learn G-code by reading programs before writing them: following a proven program block by block at the control and matching each line to what the machine does. Writing small programs by hand, such as a facing pass or a drilled pattern, builds the habit of thinking in coordinates and modes.

CAM skills come next, since most production programs are now generated in CAM. A programmer who can read the G-code that CAM produces can still spot a wrong offset or an unsafe rapid move before it reaches the machine. For the wider picture of how programming fits into making a part, see [What Is CNC Machining?](https://cncoutlook.com/guides/what-is-cnc-machining/).

## FAQ

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

G-codes are preparatory functions that set motion and modes, such as G01 for a straight cut or G90 for absolute positioning. M-codes are miscellaneous functions that switch machine hardware, such as M03 to start the spindle or M08 to turn on coolant.

### Do machinists still write G-code by hand?

For simple parts, edits and troubleshooting, yes. Complex parts are programmed in CAM software, but the output is still G-code, and machinists read and adjust it at the control.

### Can the same G-code program run on any CNC machine?

No. Basic moves are similar on most controls, but cycles, offsets, M-codes and syntax differ. Programs are posted for a specific machine and control, and anything moved to a different machine needs checking and simulation first.

### How long does it take to learn G-code?

The core codes in this reference are quick to learn with practice. Programming efficiently, choosing cycles and setting up offsets safely takes much longer, and most of that learning happens at the machine.

## Sources & References

- [G-Code Overview](https://linuxcnc.org/docs/html/gcode/overview.html) — LinuxCNC
- [G-Codes](https://linuxcnc.org/docs/html/gcode/g-code.html) — LinuxCNC
- [M-Codes](https://linuxcnc.org/docs/html/gcode/m-code.html) — LinuxCNC
- [TNC 640 User’s Manual: ISO Programming](https://content.heidenhain.de/doku/tnc_guide/pdf_files/TNC640/34059x-11/diniso/892909-29.pdf) — HEIDENHAIN

© CNC Outlook. See https://cncoutlook.com/terms/ for reuse terms.
