G-Code Basics: The CNC Programming Language Explained

Every CNC machine, whatever its make or control, is ultimately told what to do in the same kind of language. The CAD model describes the part, the CAM software works out the toolpaths, and then — at the last step, in the machine’s own tongue — the whole job is translated into a long list of short text commands called G-code. It is the layer where design becomes motion, the common denominator every machinist eventually has to read. Yet because modern CAM software writes most G-code automatically, the language is now taught backwards: people learn to generate it years before they learn to read it. This guide closes that gap from the reading side, because the machinist who can read G-code can troubleshoot at the machine, catch what the software got wrong, and edit a program on the spot — and the machinist who cannot is trusting a black box with their setup.
This guide is the entry point of the programming topic. It explains what G-code is and how a program is structured, the two code families and the handful of motion commands that do almost all real cutting, the coordinate thinking every program depends on, and — the skill that matters most — how to read a real program line by line. It deliberately teaches the pattern rather than a code list, because G-code is not one language but many dialects: what a code means on a Fanuc may differ on a Siemens, a Heidenhain or a Haas, so the durable skill is understanding how the language works and verifying the details against your machine’s manual. It sits on top of the fundamentals covered elsewhere in this library: what a CNC machine is explains the machine G-code drives, the axes guide names the coordinates it moves, the machine-types guide shows the mill-versus-lathe worlds with their different conventions, and the parameters and speeds-and-feeds guides explain what the feed and speed words actually mean. Terms like spindle, axis, lathe and tool magazine are in the glossary.
What G-code actually is
G-code — short for geometric code, because it describes the geometry of the tool’s motion — is a programming language whose lines are executed one after another by the machine’s controller. The full chain is CAD to CAM to code to cut: the CAD model defines the part’s shape, the CAM software computes the toolpaths that will machine it and writes them as G-code, and the controller on the machine reads that code line by line and drives the axes, spindle and coolant accordingly. A G-code program is nothing more than a numbered sequence of instructions, each one a command about position, motion, speed or a machine function.
What a beginner needs first is the right mental scale. G-code looks intimidating because the code lists printed in manuals run to hundreds of entries, but the language a machinist actually meets has a small active vocabulary — the same twenty or thirty commands do the overwhelming majority of everyday work, and they divide into a structure that is easy to hold in your head once you see the pattern.
The grammar: blocks and words
A G-code program is made of lines, each called a block. Every block is a sequence of words, and each word is a letter followed by a number. The letters are the vocabulary; the numbers are the values. A typical block reads like a sentence:
G01 X40.0 Y15.0 F250.
- G01 is the word that says “cut a straight line.”
- X40.0 Y15.0 are the words that say where: “move to X = 40, Y = 15.”
- F250. is the word that says how: “at a feed rate of 250.”
Letters divide into jobs. N numbers the line (optional, mostly for readability). G commands the type of motion or operation. X, Y, Z give coordinates. F sets the feed rate, S the spindle speed, T the tool to select. M commands machine functions — spindle on, coolant on, tool change. A whole program is these same few letters, repeated in millions of combinations, and once a word is understood it means the same thing wherever it appears.
Two families carry the language, and the rule of thumb is the single most useful sentence in programming: G codes control the tool’s motion and the geometry of the cut; M codes control the machine’s on-off functions. G is the path the tool takes; M is the switch that turns things on and off along the way.
The motion commands that do almost all the cutting
Within the G family, four commands do nearly every move a machine makes, and they are worth understanding completely because everything else is detail:
| Command | What it does |
|---|---|
| G00 | Rapid positioning — moves the tool quickly to a position without cutting. Used to get to and away from the work fast. |
| G01 | Linear interpolation — cuts a straight line at the programmed feed rate. The workhorse of machining. |
| G02 / G03 | Circular interpolation — cuts an arc, clockwise (G02) or counter-clockwise (G03) as viewed from the spindle looking down in the default XY plane. |
| G04 | Dwell — pauses the machine for a programmed time. |
Everything else a machine does with its axes is a variation or a convenience built on these. Two details of the arc commands matter because they trip up beginners. First, an arc needs its centre as well as its endpoint: G02/G03 can be given the centre as I, J, K offsets (the distance from the arc’s start point to its centre, in each axis) or as an R radius — and an R arc cannot program a full circle (a circle needs the centre form), and the sign of R chooses which of the two possible arcs is meant. Second, arcs only make sense in a chosen plane — the default G17 is the XY plane, with G18 (XZ) and G19 (YZ) for the other faces a machine can work in. The plane selection is usually set once at the top of the program and forgotten, until the day an arc appears in the wrong plane and the machine does something unexpected.
The real subtlety of the motion commands is not any single one but the way they cancel each other. G00, G01, G02 and G03 belong to the same modal group: the machine remembers which one is active, and issuing one replaces the previous. You do not need to repeat G01 on every line of a straight cut — the machine stays in G01 mode until another motion command displaces it. Modal behaviour is the language’s biggest source of beginner confusion and of hand-editing mistakes, and the rule to carry is simple: a G-code that sets a state (motion type, units, plane) stays in force until another code in the same group changes it, which is why a stray G00 buried in a program can silently turn a cutting pass into a rapid.
Coordinate thinking: absolute, incremental, and where zero lives
G-code is meaningless without a coordinate system, and the coordinate concepts are where machining judgement lives. Three ideas cover it:
Absolute versus incremental. Under G90 (absolute), every X, Y, Z is measured from the part’s fixed origin — X40 means forty units from zero, wherever the tool currently is. Under G91 (incremental), every coordinate is measured from the tool’s current position — X40 means “move forty units in X from here.” Absolute programming dominates modern work because it is easier to reason about and safer: each line describes where the tool should be, not how far it should jump. Incremental is used where a motion repeats relative to itself — a pattern of identical steps. Mixing the two without noticing is a classic source of mysterious bad parts.
Machine home versus part zero. A machine has a home position — the reference point it returns to, usually with G28, where its axes are known to be at a fixed physical spot. But the work is cut relative to the part’s zero — the origin the programmer chose when writing the program. The bridge between the two is the work offset: a stored coordinate (G54 through G59 give several) that tells the machine where the part’s zero lives in the machine’s world. Setting the work offset — finding where the part actually sits and entering it — is the setup step that probing automates, and it is the reason two identical programs run identically on two different days: the program never changes, only the offset that locates the part for it. The part zero is the machining datum: everything in the program is measured from it, which is why its location is chosen with care — usually a corner or a centre that the drawing’s own dimensions reference.
Mill conventions versus lathe conventions. The coordinates mean the same thing on every machine but are oriented differently, and reading a program means knowing which world you are in. On a machining centre the tool moves in X, Y and Z with Z along the spindle axis. On a lathe the convention is different: Z runs along the spindle — the length of the part — and X is radial, toward or away from the centreline, so a two-axis turning program uses only X and Z, and the planes its arcs use default differently. If a program looks wrong, check which machine world it was written for before suspecting the code.
Everything else is modal state and convenience
Once the four motion commands and the coordinate ideas are clear, the rest of the language falls into two buckets: codes that set a state the machine remembers, and codes that collapse repetitive work into a single line.
State codes. Units are set once near the top — G20 (inch) or G21 (metric) on most milling controls. Tool length is handled by G43, which activates the length offset stored for the current tool so the control knows where the tool’s tip actually is; the offset itself comes from the tool table — the same numbers a tool setter writes automatically — which is how a machine cuts accurately with tools of different lengths. Cutter radius compensation — G41 (left) and G42 (right), cancelled by G40 — lets the program cut the part’s true outline while the control offsets the tool by its actual radius, so a worn or reground tool of a different size still cuts the drawing’s geometry. And on lathes, G96 sets constant surface speed (the spindle slowing as the tool moves outward so the cutting speed stays even) and G97 returns to direct RPM. None of these moves the tool by itself; each changes how subsequent moves are interpreted, which is exactly why modal codes are easy to forget and worth respecting.
Canned cycles. The great convenience of the language is that a long, repetitive operation can be requested in one line. A drilling cycle is the classic example: instead of writing the many blocks that rapid to a hole position, feed to depth, retract, and repeat, the programmer writes G81 and the control performs the whole standard sequence. G83 adds pecking for deep holes, G84 taps, and the family extends to boring. The cycle is cancelled with G80 (or by a new motion command), and G98/G99 choose where the tool returns between holes. Canned cycles are the reason a program for a part with forty holes is shorter than a program for one hole done longhand — and reading them correctly means knowing that one line stands for a whole hidden sequence.
The dialect warning: one code, many meanings
The warning that belongs in every honest G-code guide is that G-code is not a single standard. It is a family of dialects, each owned by a control builder, and the same letters can mean different things on different machines. The word “G-code” works everywhere; the meaning of a given G number has to be checked against the machine’s manual. The conflicts are real and sometimes inside a single builder: on many milling controls G20/G21 select inches and millimetres, while on some turning controls those same numbers mean something else entirely, and G70/G71 — inch and metric on one dialect — are finishing and stock-removal cycles on a Fanuc turning control. Codes also differ between a builder’s mill and its lathe, because the machines’ geometries differ.
None of this makes learning the language futile; it makes how you learn it matter. The pattern is universal — blocks, words, G-for-geometry, M-for-machinery, modal state, part zero, canned cycles — and any machinist fluent in the pattern can read a program on an unfamiliar control and verify the details against the manual. The discipline is never to assume a code means what it meant on the last machine. This is also why the machine’s control type is a specification worth reading before you buy — the spec-sheet guide treats it as a decision, not a footnote.
Why learn it in the CAM era
The honest question every beginner asks — and the industry keeps answering — is whether G-code is still worth learning when software writes it. The answer is yes, for reasons that become obvious the first time a CAM-generated program misbehaves. G-code literacy is what lets a machinist read the output the software produced: check that a tool change and an offset are in the right place, catch the move the postprocessor put in the wrong direction, and understand why a program that looked fine on screen crashed on the machine. It is the skill that lets a machinist edit at the machine — change a depth, adjust a feed, fix a clearance — and return to cutting in minutes instead of walking back to a workstation. And it is the foundation for the work CAM does not do: writing a simple program by hand for a quick job is often faster than building it in software, and the in-machine logic of probing routines, macros and automation is expressed in this same language.
The boundary between hand and CAM programming is not a contest but a division of labour. Hand-written G-code is fast and precise for simple work — facing, drilling, turning a shaft, a straightforward pocket — the jobs where the geometry is plain and a capable machinist can write the program in the time the software would take to boot. CAM earns its keep exactly where hand programming hits its ceiling: sculpted 3D surfaces and mould cavities, complex multi-axis work, and anywhere graphical simulation is needed to prove the toolpath before it touches the machine. The CAM and simulation side of that boundary is covered elsewhere in this library’s programming topic. What belongs here is the ground truth underneath it all: whether a program was written by hand or generated by software, it is G-code, and the person who can read it is the person in control.
How to read a real program
The test of understanding is reading a real program, and a typical short milling program reads like a story. Strip it to its bones and the logic of a whole job is visible:
- Set the stage. The program begins by establishing the state the whole job will use — units (G20/G21), the machining plane (G17), absolute mode (G90), and the work offset that locates the part (G54). These early lines carry no motion; they tell the machine what world it is working in.
- Bring up the first tool.
T1 M06selects tool 1 and performs the tool change;M03 S8000starts the spindle clockwise at 8000 rpm. G43 with the tool’s length offset number tells the control where the new tool’s tip is. - Rapid to the work.
G00 X... Y...moves the tool quickly to the first position above the part — a non-cutting move, close but not yet touching. - Cut.
G01 Z... F...feeds the tool down into the work at a cutting feed rate, and the following G01 lines cut the feature’s profile, the machine staying in G01 mode until told otherwise. - Retract, repeat. The tool rises (G00), the next feature’s position is called, and the pattern repeats. When a new tool is needed, the sequence begins again at step 2 — and on a machine with an automatic tool changer the whole job runs through its tools unattended.
- Finish. The tool retracts to a safe height, the spindle stops (M05), the machine returns toward home (G28), and
M30ends the program and resets it for the next part.
Read once this way, every program is the same story with different numbers — which is exactly the point. G-code looks like a code to be memorised and instead is a language to be understood: a small vocabulary, a fixed grammar, and a sequence that always tells the same tale of staging, cutting and finishing.
Frequently asked questions
What is the difference between G-code and M-code? G codes (geometric codes) control the tool’s motion and the geometry of the cut — rapid moves, straight and circular cutting, plane and unit selection. M codes (miscellaneous codes) control the machine’s on-off functions — spindle on and off, coolant on and off, tool change, program stop and end. The rule of thumb: G controls the path the tool takes; M controls the machine functions along the way.
Do I still need to learn G-code if I use CAM software? Yes. CAM writes most production G-code, but the machinist who can read it troubleshoots at the machine, verifies what the software produced, catches bad postprocessed output, and edits a program on the spot instead of returning to a workstation. Hand-written G-code is also still the fastest way to do simple jobs. Learn to read the language even if you rarely write it — code literacy is what keeps you in control of the software’s output.
What does G00 G01 G02 and G03 mean? They are the four core motion commands. G00 is rapid positioning — a fast non-cutting move to get the tool to and from the work. G01 is linear interpolation — cutting a straight line at the programmed feed rate, the workhorse of machining. G02 and G03 are circular interpolation — cutting an arc clockwise (G02) or counter-clockwise (G03). G00, G01, G02 and G03 belong to the same modal group, so each one cancels the previous.
What is the difference between absolute and incremental programming? Under absolute programming (G90), every coordinate is measured from the part’s fixed origin — X40 means forty units from zero. Under incremental (G91), every coordinate is measured from the tool’s current position — X40 means move forty units from here. Absolute dominates modern programming because it is safer and easier to reason about; incremental is used where a motion repeats relative to itself. Mixing the two unnoticed is a classic source of bad parts.
Why do the same G-codes mean different things on different machines? Because G-code is a family of dialects, not a single standard. Each control builder — Fanuc, Siemens, Heidenhain, Haas, Mazak and others — owns its own version, and the same numbers can differ between builders and even between a builder’s mills and lathes. The pattern is universal; the details are not. Always verify a code’s meaning against your machine’s control manual rather than assuming it matches the last machine you ran.
Bottom line
G-code is the language every CNC machine executes, and it is far simpler than its reputation: a program is a sequence of blocks, each a set of letter-and-number words, and the whole language rests on a pattern you can learn once and apply anywhere. Two families carry it — G codes for the tool’s motion and geometry, M codes for the machine’s on-off functions — and four motion commands, G00, G01, G02 and G03, do nearly all real cutting. Coordinate thinking supplies the meaning: absolute versus incremental, and above all the difference between the machine’s home and the part’s zero, bridged by the work offsets that probing and setup establish. The rest is modal state and convenience — codes that the machine remembers, and canned cycles that collapse whole operations into a single line. Because G-code is many dialects, the durable skill is reading the pattern and verifying the details against the manual rather than trusting memory. And because CAM writes most of it now, the machinist’s edge is literacy: the person who can read the code the software produced, catch what it got wrong, and edit it at the machine is the person who stays in control — of the setup, of the process, and of the part.
This guide is part of the CNC Media guides library — the programming entry reference of the CAM topic, deliberately free of prices and of any single control builder’s dialect to promote.