Basic G-code comes in three groups you must know before you run a machine: motion commands (G00 rapid move without cutting, G01 straight-line cut, G02 clockwise arc, G03 counterclockwise arc), setup commands (G17 XY plane, G21 millimetres, G90 absolute coordinates, G54 work offset) and M-codes that control the machine (M03 spindle on, M06 tool change, M08 coolant on, M30 end of program). On top of these come offsets: the work zero, tool length compensation and cutter radius compensation. This lesson explains each group in Fanuc-style syntax (the most common in Vietnamese machine shops), then reads a sample engraving program line by line, with spindle speed, feed and cycle time worked out.
By the end of this lesson, you will be able to:
- Read one block (line) of G-code and say what each word N, G, X, Y, Z, F, S, T and M does.
- Tell G00, G01, G02, G03, G90 and G91 apart, and write straight moves and arcs using R or I, J.
- Explain the G54 work offset, G43 H tool length offset and G41, G42 D cutter compensation, and where each is measured on the machine.
- Read a 17-line sample milling program and work out spindle speed, feed and cutting time with figures that add up.
- Prove out a new program safely with simulation, a dry run above the part and single block.
Learning CNC machining? G-code and CAM programming are step 6 of the roadmap, after machine safety, reading drawings, measuring tools, tooling and cutting data, and setting up work and tools. See the 8-step CNC machinist roadmap to see where you are.
What is G-code, and what is in one line?
G-code is a letter-and-number command language that a CNC controller reads line by line to move the tool, turn the spindle and switch the coolant. Even when you later program with CAM software, the output is still G-code; an operator must be able to read it to make quick edits, find faults and know what the machine is about to do before pressing start.
Structure of a block
Each line is called a block and contains several "words", each a letter followed by a number:
- N: sequence number, for finding and jumping to a line; the machine does not require it.
- G: preparatory command, choosing a type of motion or a mode.
- X, Y, Z: target coordinates; I, J, K: arc centre position relative to the start point; R: arc radius.
- F: feed rate (mm/min when milling); S: spindle speed (rpm); T: tool number; M: miscellaneous machine function.
- Text in parentheses is a comment the machine ignores; write it in plain ASCII, since many controllers cannot display accented characters.
You type these letters on the controller keypad (Figure 1): there are keys for G, M, X and Z, a number pad, and an EOB key to end each block.
Photo: TurnOnTheNight / Wikimedia Commons, CC BY-SA 4.0
Modal commands stay active until replaced
Most G-codes are modal: declare G01 once and the following lines only need X and Y; the machine keeps cutting in straight lines until it meets G00, G02 or G03. F and S also stay the same until changed. That is why a line like X40. on its own still runs, and also a big source of mistakes when you forget an old mode is still active.
Coordinate systems on mills and lathes
On a vertical mill, X is left and right, Y is toward and away from you, Z is up and down, and positive Z moves the tool up away from the part. On a lathe, Z runs along the spindle axis and X is radial, and most lathes program X as a diameter: X30 means the part is turned to 30 mm diameter, not that the tool is 30 mm from the centre.
Motion commands G00, G01, G02, G03
These four commands make up most of any program. Master them and you can read 70–80% of a G-code file.
G00: rapid move, no cutting
The tool moves at the machine's top speed to the target, used to bring the tool near the part or retract it. No F is needed. On many machines G00 axes do not travel in a straight line; each axis runs at its own full speed, so the path can be a dog-leg. Always lift Z first, then move X and Y, to avoid hitting the fixture.
G01: straight-line cut at feed F
G01 X40. Y10. F300 means cut in a straight line to X40, Y10 at 300 mm/min. The first G01 line must include F, or many machines raise an alarm. Plunging down in Z also uses G01, with a lower F.
G02, G03: circular arcs
Looking down from positive Z onto the XY plane (G17), G02 is a clockwise arc and G03 a counterclockwise arc. There are two ways to define them:
- With R:
G03 X50. Y20. R10.moves to the end point along an arc of radius 10. Short, but it cannot describe a full 360-degree circle. - With I, J: I and J are the distances from the arc start point to the arc centre in X and Y. For example, at X40 Y10 with the centre at X40 Y20, that is I0 J10. This is exact and works for full circles too.
| Code | Meaning | Needs F? | Example |
|---|---|---|---|
| G00 | Rapid move, no cutting | No | G00 X10. Y10. |
| G01 | Straight-line cut | Yes | G01 Z-1. F100 |
| G02 | Clockwise arc | Yes | G02 X10. Y30. R10. |
| G03 | Counterclockwise arc | Yes | G03 X50. Y20. I0 J10. |
| G90 / G91 | Absolute / incremental coordinates | No | G90 X20. (go to X20) |
| G17 / G18 / G19 | Arc plane XY / ZX / YZ | No | G17 |
| G20 / G21 | Inch / mm units | No | G21 |
| G54–G59 | Select work coordinate system | No | G54 |
| G28 | Return to machine home | No | G91 G28 Z0 |
Setup commands: G90, G91, G54 and the safety line
G90 and G91: absolute or incremental
G90 (absolute): every coordinate is measured from the work zero. At X10, G90 G01 X40. takes the tool to X40, a 30 mm move. G91 (incremental): the coordinate is the distance to travel from the current position; from the same point, G91 G01 X40. takes the tool to X50. Beginners should write in G90, use G91 only for a few special commands, and switch back to G90 straight away.
G54: the work zero
The machine has a fixed machine home, but you program from the work zero, usually at a corner or the centre of the top face of the part. When setting up, the operator touches off the part with an edge finder or an electronic probe and enters the distance from machine home to work zero in the G54 table (Figure 2). When the program calls G54, every X, Y and Z is measured from that point. The same program run on two different vises uses G54 and G55.
Photo: Rob NREC / Wikimedia Commons, CC BY-SA 3.0
The safety line at the start of a program
The first line should reset any modes left over from the previous program: G21 G17 G90 G40 G49 G80 (mm, XY plane, absolute, cancel cutter compensation, cancel tool length offset, cancel drilling cycles). Without it, a leftover G91 or G41 can send the tool the wrong way on the very first move.
Common M-codes, S, F and T
M-codes are miscellaneous commands that switch machine functions on and off. The list below is standard on Fanuc-style machines; some numbers differ between machine builders, so always check the manual of the machine you run.
| Code | Function | Notes |
|---|---|---|
| M00 | Mandatory program stop | Used to flip the part or measure mid-program |
| M01 | Optional stop | Stops only when Optional Stop is switched on |
| M03 / M04 | Spindle clockwise / counterclockwise | Paired with S, e.g. S3000 M03 |
| M05 | Spindle stop | Before tool change and at the end |
| M06 | Tool change | Paired with T, e.g. T1 M06 |
| M08 / M09 | Coolant on / off | On before cutting, off when retracting |
| M30 | End of program, rewind | Last line of the program |
S and F come from cutting data
S (rpm) comes from the cutting speed Vc for the material and tool: S = Vc × 1000 ÷ (π × D). F (mm/min) comes from the feed per tooth fz: F = fz × number of teeth × S. Vc and fz come from the tool catalogue, which you covered in the tooling and cutting data step.
Differences when programming a lathe
Lathes commonly use G96 (constant surface speed, S in m/min) with G50 to cap the maximum rpm, G97 (constant rpm), feed in mm/rev (G99), and canned cycles for roughing and threading. The G00, G01, G02 and G03 rules stay the same; only the axes are X and Z.
Offsets: work zero, tool length and tool radius
A correct program with wrong offsets still crashes the tool or scraps the part. Offsets are numbers the operator measures on the machine and enters into tables; the program only calls them.
Tool length offset G43 H
Every tool sticks out of its holder by a different length. The operator measures each tool's length (with a tool setter, or by touching off on the part top or a gauge block) and enters it in the tool offset table (Figure 3). G43 H01 Z50. tells the machine to apply tool 1's length and bring the tool tip to Z50 above the part. Rule: the H number matches the T number. Forgetting G43 or calling the wrong H is the number one crash for beginners.
Photo: Master Sgt. Christopher Campbell / Wikimedia Commons, Public domain
Cutter radius compensation G41, G42 D
When milling a profile you want to program the drawing dimensions, not the tool centreline. G41 (left) and G42 (right) shift the tool to the left or right of the programmed path by the radius stored in register D. Looking in the direction of travel, if the tool is on the left of the profile, that is G41. Turn compensation on during a straight lead-in longer than the tool radius, and cancel it with G40 on the lead-out.
Why it helps when tools wear
A 10 mm cutter worn to 9.96 mm leaves the part oversize. Instead of editing the program, the operator changes the D radius from 5.00 to 4.98 and reruns. This is the everyday sizing skill used in the machining-to-tolerance step.
Worked example: reading a sample engraving program
Task: an aluminium block 60 × 50 × 15 mm, with the G54 work zero at the front left corner of the top face (X0 Y0 Z0). Use a 6 mm two-flute end mill to cut a groove 1 mm deep along the tool centreline (no radius compensation). The path: from X10 Y10 right to X40, a convex R10 arc up to X50 Y20, straight up to Y40, left to X20, a concave R10 arc down to X10 Y30, then back to X10 Y10.
%
O0001 (KHAC RANH MAU - NHOM 60X50X15)
N10 G21 G17 G90 G40 G49 G80
N20 T1 M06 (DAO PHAY NGON D6 2 ME)
N30 G54 G00 X10. Y10.
N40 S3000 M03
N50 G43 H01 Z50. M08
N60 Z2.
N70 G01 Z-1. F100
N80 X40. F300
N90 G03 X50. Y20. R10.
N100 G01 Y40.
N110 X20.
N120 G02 X10. Y30. R10.
N130 G01 Y10.
N140 G00 Z50. M09
N150 M05
N160 G91 G28 Z0.
N170 M30
%
The comments are in Vietnamese without accents, as written on the shop floor: "sample groove engraving, aluminium 60x50x15" and "6 mm two-flute end mill".
Line by line
- N10: safety line. N20: call tool 1. N30: select G54 and rapid to the start point X10 Y10 (Z still high).
- N40: spindle clockwise at 3000 rpm. N50: apply tool 1 length offset, go to Z50, coolant on. N60: rapid down to 2 mm above the part.
- N70: plunge to 1 mm deep at a slow F100. N80: cut straight to X40 at F300 (G01 and Y10 carry over).
- N90: counterclockwise arc centred at X40 Y20; in I, J form it is
G03 X50. Y20. I0 J10.N120: clockwise arc centred at X10 Y40; in I, J form it isG02 X10. Y30. I-10. J0. - N140–N170: retract, coolant off, spindle stop, send Z home, end.
Check the cutting data
- Actual cutting speed: Vc = π × D × S ÷ 1000 = 3.1416 × 6 × 3000 ÷ 1000 ≈ 56.5 m/min, moderate for aluminium on a small machine.
- Feed per tooth: fz = F ÷ (teeth × S) = 300 ÷ (2 × 3000) = 0.05 mm/tooth.
Cut length and time
| Segment | Command | Length (mm) |
|---|---|---|
| X10 → X40 | G01 | 30.00 |
| R10 arc, 90 degrees | G03 | 15.71 |
| Y20 → Y40 | G01 | 20.00 |
| X50 → X20 | G01 | 30.00 |
| R10 arc, 90 degrees | G02 | 15.71 |
| Y30 → Y10 | G01 | 20.00 |
| Total | 131.42 | |
A 90-degree arc of radius 10 is π × 10 ÷ 2 = 15.71 mm long. Check the total: 30 + 15.71 + 20 + 30 + 15.71 + 20 = 131.42 mm. Cutting time at F300: 131.42 ÷ 300 = 0.438 min ≈ 26.3 seconds. Add the 3 mm plunge (from Z2 to Z-1) at F100: 3 ÷ 100 = 0.03 min = 1.8 seconds. Total cutting time ≈ 28.1 seconds, excluding rapids and tool change. The cut stays within X7–X53 and Y7–Y43 (plus or minus the 3 mm tool radius), inside the 60 × 50 block.
Photo: Impressionmanufacturer / Wikimedia Commons, CC BY-SA 4.0
Proving out a new program safely
Every newly typed or edited program must go through these checks, including programs posted from CAM.
Safety warning: no cloth gloves, rings, watches or loose sleeves at the machine; tie back long hair; wear safety glasses; close the machine doors before running. Never reach into the cutting area while the spindle is turning, and never clear chips by hand. On the first run, keep your hand on the emergency stop (Figure 5), turn rapid override down to 25% or lower, and switch on single block. If the tool moves differently from what you expect, press Feed Hold or the emergency stop at once; do not guess. Do not change offsets or machine parameters unless you have been trained and assigned to.
Photo: Flygklubben / Wikimedia Commons, CC BY-SA 4.0
Order of checks
- Read it through on paper or on screen: safety line, correct G54, G43 with H matching T, decimal points everywhere.
- Graphic simulation on the machine or in software: the path has the right shape and does not cut through the fixture.
- Dry run above the part: shift the Z zero up (for example by 50 mm in the common shift register, following your shop's procedure) and run the whole program in the air.
- Single block on the first real cut, rapid at 25%, low feed override, watching the "distance to go" before each step.
- Measure the first part before running a batch.
Quick check before pressing Cycle Start
- The part is clamped tight and the fixture is clear of the tool path.
- The right tool number, correct H and D offsets, no chipped edges.
- Enough coolant, machine doors closed.
Common G-code mistakes and how to fix them
Missing decimal point
Sign: the tool barely moves or goes wildly wrong. Cause: on many Fanuc-style controls, X10 without a decimal point can be read as 10 least increments (0.010 mm), depending on machine parameters. Fix: always write X10. or X10.0, and check carefully when editing by hand.
Forgetting G43 or calling the wrong H
Sign: the tool dives lower than expected and hits the part or the vise. Cause: no length offset, or T2 with H01. Fix: every tool change gets a G43 Hxx Z.. line with xx matching the tool number; dry run to check.
Mixing up G02 and G03
Sign: an arc goes the wrong way and gouges the part. Cause: looking at the plane from the wrong side. Fix: always picture looking down from positive Z; sketch the path and mark the direction before writing.
A leftover G91
Sign: after a G28, the following coordinates go off. Cause: G91 G28 Z0. switches the machine to incremental and G90 was never restored. Fix: write G90 on the next line after any G91 command, or at the start of each new tool section.
Turning on radius compensation in the wrong place
Sign: an alarm, or a gouge at the start of the profile. Cause: G41 or G42 switched on during an arc, or on a lead-in shorter than the tool radius. Fix: add a straight lead-in long enough, outside the part, and switch compensation on there.
Frequently asked questions
How long does it take to learn basic G-code?
Reading the commands in this lesson usually takes about 1–2 weeks of steady study with handwritten exercises. Editing programs confidently on a real machine usually takes a few more months of running machines under supervision.
Is G-code the same on Fanuc, Siemens and Mitsubishi?
The motion commands G00–G03, G90, G91 and coordinates are almost the same. They differ in canned cycles, subprogram calls, some M-codes and special syntax (Siemens has its own cycle language, Heidenhain uses a conversational style). When you change machines, read the programming manual for that exact controller.
Do I still need G-code if I use CAM software?
Yes. CAM writes the program, but the operator still has to read it to check it before running, quickly adjust F, S and entry points, and understand why the machine raises an alarm. Skills tests at job interviews also often include reading or hand-writing G-code.
What is the difference between G00 and G01?
G00 is a rapid move without cutting, needs no F, and may not follow a straight path. G01 is a precise straight-line cut at the feed F you set. Never use G00 to move into the part.
Practice exercises and self-assessment
- Hand-write a program to engrave the letter L: from X10 Y40 down to X10 Y10 then across to X35 Y10, 0.5 mm deep, 6 mm cutter, with a full safety line and ending.
- Rewrite arc N90 and arc N120 in the example in I, J form and check them in simulation.
- Recalculate the example's cutting time and fz if F300 is changed to F450.
Self-assessment criteria
- Pass: the program has a safety line, G54, G43 with H matching T, and decimal points on every coordinate.
- Pass: the simulation has the right shape and arc directions, with no G00 move into the part.
- Pass: you get the new time 131.42 ÷ 450 ≈ 0.292 min ≈ 17.5 seconds of profile cutting, and fz = 0.075 mm/tooth.
- Not yet: running a new program without simulation or single block, or mixing up G02 and G03.
Once you can read and write basic G-code, move on to the next roadmap step: Machining a milled and turned part to tolerance, where you combine work setup, tool setup, the program and inspection to produce a real part. To see how G-code skills affect your role and pay, read CNC machinist pay in Vietnam 2026: operator, setter, programmer.
Keep learning CNC: after G-code come CAM programming, machining parts to tolerance, and then trade certificates and the skills test at job interviews. See the 8-step CNC machinist roadmap.

