GreatLight CNC Machining Factory logo
CNC Machining
Rapid Prototyping
Materials
Industries
News
About GL

Get Instant Quote

CNC Milling Fundamentals

Basic Knowledge of CNC Milling Code

This page explains how a CNC milling program is built: block structure, the G-codes and M-codes that move the tool, offsets, work coordinates, and how CAM output reaches the machine. It is written for design engineers, manufacturing engineers, and buyers who need to read a program, judge a setup, and ask better questions at the shop floor level.

G-code & M-codeBlock structureWork offsetsCAM output
CNC G code basics explain
Doc

How to read a milling program

A CNC milling program is a list of instructions executed in order. Each line tells the machine where to go, how fast, and which functions to turn on.

Structure

Anatomy of a block

A milling program is written in blocks. One block is one line of code, and each block is made of words. A word is a letter address plus a number: G01, X50.0, F800, S6000, T05, M08. The letter says what kind of information follows; the number gives the value. Word order inside a block is flexible on most controls, but the conventional order (N, G, X/Y/Z, F, S, T, M) makes programs easier to read and easier to debug.

Letter addresses do not overlap. G is a preparatory function that sets a mode, such as linear interpolation or a drilling cycle. M is a miscellaneous function that switches machine behavior on or off, such as spindle rotation or coolant. X, Y, and Z are axis positions. I, J, and K define arc centers or cycle parameters. F is feed rate, S is spindle speed, and T calls a tool.

Block numbers (N) are optional on modern controls but useful. They let an operator jump to a line and they appear in alarm messages, so keeping them in the posted output saves time when something stops mid-cycle.

One more habit worth keeping: group related words so each block does one thing. A block that changes the tool, sets the speed, turns on coolant, and moves the axis is legal, but when it alarms you have four suspects instead of one. Programs from our CAM posts separate these into short blocks for that reason.

  • 1
    WordLetter address plus value, e.g. G01 or F800
  • 2
    BlockOne line of code, executed as a unit
  • 3
    ModalA G-code that stays active until cancelled
  • 4
    Non-modalA code that applies to one block only, e.g. G04
G-codes

The G-codes that do most of the work

Most of a milling program is motion. G00 moves the tool at rapid traverse to a position, usually above the part. G01 feeds in a straight line at the programmed feed rate and is the code that actually cuts. G02 and G03 cut clockwise and counterclockwise arcs; the arc is defined by the end point plus either the center offset (I, J, K) or the radius (R).

G17, G18, and G19 select the plane for arcs and cutter compensation. On a vertical mill, G17 (XY plane) is nearly always active. G20 and G21 set the unit system to inches or millimeters, and getting this wrong is one of the fastest ways to scrap a part or crash a machine. Check the units line before you run anything.

Fixed cycles handle repeating operations. G81 is a simple drill cycle, G83 is peck drilling, G73 is high-speed peck, and G84 is tapping. Each cycle has parameters for retract height, depth, and peck increment, usually set with G98 or G99 for the return plane. Once a cycle is active it repeats at each new position until G80 cancels it.

Cutter compensation is where a lot of confusion starts. G41 offsets the tool to the left of the path, G42 to the right, and G40 cancels it. On a finish pass the operator may dial a wear offset of a few thousandths instead of editing the program. That is normal practice, and it is why the coordinate values in the program are not always the values the tool actually cuts to.

  • 1
    G00 / G01Rapid move and linear feed cut
  • 2
    G02 / G03Clockwise and counterclockwise arc
  • 3
    G81 / G83Drill cycle and peck drill cycle
  • 4
    G41 / G42Cutter compensation left and right
M-codes & offsets

M-codes, tools, and work offsets

M-codes control the machine rather than the path. M03 starts the spindle clockwise, M04 counterclockwise, and M05 stops it. M08 turns coolant on and M09 turns it off. M06 changes the tool, usually together with a T word that selects the pocket. M30 ends the program and rewinds it; M00 is a planned stop for the operator.

Tool length and diameter live in the offset table, not in the program. The program calls T05, then H05 applies the length and D05 applies the diameter when compensation is active. If a tool is replaced, the operator measures it and updates H05. The program itself does not change. This separation is what lets the same file run after a tool swap.

Work offsets define where the part sits in the machine. G54 through G59 are the standard set, and G54 is the default on most jobs. The operator touches off a corner or a datum and stores the position in the work offset page. A program that runs on one vise with G54 may run on a second vise with G55 if the CAM post has that offset built in.

Subprograms and loops are worth knowing about even if you never write them. M98 calls a subprogram and M99 returns from it. For a part with 40 identical holes, a loop is shorter and easier to verify than 40 repeated blocks. If a CAM post is generating very long output, ask whether a subprogram would be cleaner.

  • 1
    M03 / M05Spindle on clockwise and spindle stop
  • 2
    M06Tool change
  • 3
    M08 / M09Coolant on and off
  • 4
    M30Program end and rewind
Reference

Common G-codes and M-codes in milling

A short reference for reading posted output. Control dialects vary, so confirm against the machine manual before editing a program by hand.

CodeFunctionWhen it is used
G00Rapid positioningApproach and retract moves
G01Linear interpolationStraight cuts at feed rate
G02 / G03Circular interpolationFillets, radii, pockets
G17 / G18 / G19Plane selectionArc and compensation plane
G20 / G21Inch / metric unitsSet at program start
G40 / G41 / G42Cutter compensationFinish passes, wear offsets
G43Tool length compensationAfter every tool change
G54–G59Work coordinate systemsPart zero location
G81 / G83Drill and peck drill cyclesHole patterns
G84Tapping cycleRigid tapping
M03 / M04 / M05Spindle controlStart and stop spindle
M06Tool changeBetween operations
M08 / M09Coolant controlCutting and dry runs
M30Program endReset for next part
From CAD to machine

How CAM output becomes a milling program

Nobody hand-writes a five-axis program for a complex part. The CAM system takes the solid model, applies toolpath strategies, and posts the result as G-code for a specific control. The post processor is the piece that decides how the code looks: which G-codes are modal, how arcs are output, whether the program uses subprograms, and how work offsets and tool numbers are numbered.

Two shops can cut the same part with the same model and produce completely different-looking programs. That is normal. What matters is that the posted code matches the machine, the tooling, and the setup the operator will actually use. A program posted for a three-axis mill will not run correctly on a five-axis machine without the right post.

Simulation catches most geometry and collision problems before anything is cut. We verify toolpath and check for gouges, rapid collisions, and holder clearance in software, then make a first-article part and inspect it. For tight work we hold ±0.005 mm and a finish of Ra 0.8–1.6 μm, and the program has to be stable enough to repeat that across a run.

If you receive a program to review, start with three things: the units line, the work offset, and the tool list. Those three account for a large share of setup errors. After that, check the first rapid move and the retract height. A tool that rapids into a fixture at full speed does not give anyone a second chance.

Editing posted code by hand is sometimes necessary and always risky. Change a feed rate or a wear offset value at the control and you can undo it. Change a coordinate in the file and you may not remember why six months later. Keep a record of any manual edit.

  • 1
    Post processorTurns CAM toolpath into control-specific G-code
  • 2
    SimulationChecks gouges, collisions, and holder clearance
  • 3
    First articleVerifies the program before a full run
FAQs

CNC milling code questions

Is G-code the same on every CNC mill?

No. The core motion codes (G00, G01, G02, G03) are close to universal, but canned cycles, subprogram calls, high-speed modes, and macro syntax differ between Fanuc, Siemens, Heidenhain, and Mitsubishi controls.

A program posted for one control usually needs re-posting, not just a few edits, before it runs cleanly on another.

Can I edit the G-code myself after CAM posts it?

You can, and operators do it for small changes like feed rate or a wear offset. The risk is that the CAM model and the posted file drift apart.

If a change is permanent, make it in CAM and re-post. If it is a one-time adjustment, change it at the control and note it on the setup sheet.

What does G54 mean on a milling machine?

G54 is the first of the standard work coordinate systems. It tells the control where the part zero is relative to machine zero.

Most single-vise jobs run in G54. Fixtures with multiple stations may use G55, G56, and so on, so the same program can cut several parts in one cycle.

Why does the program use I and J instead of R for arcs?

I and J define the arc center as an incremental offset from the start point, which is precise for arcs close to a full circle. R defines the radius and is shorter but can be ambiguous for arcs over 180 degrees.

Some controls and posts default to I and J, others to R. Both produce the same geometry when written correctly.

How are tool offsets different from work offsets?

Tool offsets describe the tool: its length and diameter. Work offsets describe the part: where it sits in the machine.

Tool length is applied with G43 and an H number after each tool change. Work offset is applied once at the start, usually with G54.

Do I need to understand G-code to buy machined parts?

You do not need to write it, but reading the basics helps you review a setup sheet, understand why a quote includes an extra operation, and talk through a tolerance problem without guessing.

It also helps you spot when a drawing calls for something the process cannot hold, such as a sharp internal corner where the tool has a radius.

Send us your part and we will review the program side

Upload a drawing or a 3D model. We reply with a quotation and a free DFM analysis within 12 hours, and every part is inspected before shipment.

12-hour quoteFree DFM analysis100% inspection

Trusted by engineers and manufacturers worldwide

Tesla Ford Motor Company BYD Auto Denso Magna International Boeing Airbus Medtronic KUKA FANUC