CNC Milling Program Structure: Blocks, Addresses and Basic Commands

10 July 2026

Mentor CNC Editorial Team

To machine a part in CNC milling, giving the machine only the coordinates is not enough; which program runs, which tool is used, at what speed and direction the spindle turns, which workpiece zero is selected, at what feed the tool moves, whether coolant turns on/off and where the program ends must all be stated. When this information is written in a certain order, a CNC part program is formed. In this lesson we learn the program structure; the G00–G03 moves are covered in detail in the next lesson.

1. Aim of the lesson

By the end of this lesson the student will be able to explain a CNC part program; distinguish the program/block/word/address concepts; write the FANUC program number correctly; understand the roles of the N, G, X, Y, Z, F, S, T, H, D and M addresses; explain the EOB (end of block) concept; distinguish modal and single-block commands; recognize the comment line and the block-skip mark; and grasp the importance of a safe program start/end structure.

2. What is a CNC program?

A CNC program is the set of commands that describe, in a certain order, the operations the machine must perform to produce a part. It contains the program number, coordinate/measurement system, programming method, plane selection, tool number and change, spindle speed and direction, feed, tool length/radius compensation, X/Y/Z coordinates, coolant and program stop/end commands. A group of commands written side by side up to the end of a line is called a block.

3. Program, block, word and address concepts

We can compare a CNC program to a text: the program is the whole text, a block is a sentence/command line, a word is a command piece within the line, an address is the letter at the start of the word, and the numeric value is the number next to the address. Example block:

N20 G90 G00 X50. Y25. Z10. ;

Here, in X50., X is the address, 50. the numeric value, and X50. the word. FANUC defines a word as “address + number”; a block consists of one or more words, and blocks are separated by EOB (end of block).

4. The FANUC program number – the O address

On FANUC, programs are defined with the letter O followed by a number: O1001; has program number 1001. The program number lets the program be saved, searched, called and kept separate from other jobs. A comment can be added next to it: O1001 (VISE BODY);. Note: on some FANUC systems the O8000–O9999 range may be reserved for the builder/special macros; the usable range must be checked from the builder manual.

5. The line/sequence number – the N address

Program lines can optionally be numbered with N (N10, N20, N30). N numbers ease reading, indicate the faulty line, allow searching and separate tool sections. Writing N on every line is not mandatory; the program runs without them. Numbers are usually increased by 10 so a new line can be inserted (e.g. N25 M08; between N20 and N30). On FANUC, sequence numbers can be increasing, some can be skipped, and they need not be on every block.

6. End of block – EOB

Where each block ends must be told to the control; this is done with EOB (End of Block), shown in manuals as a semicolon ; (e.g. N10 G90 G54;). On the panel, after the line is written the EOB key is pressed.

7. Basic structure of a CNC block

Commands in a block are mostly ordered as N... G... X... Y... Z... F... S... T... M... ;, but not every address need be present on every line. E.g. N40 G01 X80. Y30. F250.; (N40 seq no, G01 linear cut, X80/Y30 target, F250 feed). Another example N20 T03 M06; (T03 tool, M06 tool change). What matters is that the commands in the same block are compatible.

8. Common addresses in CNC milling

AddressRole
GPreparatory function (e.g. G00 rapid, G01 linear cut, G02/G03 circular, G17 XY plane, G21 metric, G40 radius comp cancel, G43 length comp, G54 work zero, G80 cycle cancel, G90 absolute, G94 feed per minute).
X / Y / ZAxis positions (move amounts if G91 is active).
FFeed rate. With G94 active, e.g. F250 = 250 mm/min. An F written on a G00 rapid line does not set the rapid speed.
SSpindle speed, e.g. S1500 = 1500 rpm. It does not turn the spindle alone; direction needs an M (S1500 M03;).
TTool number, e.g. T01 M06;. The T number and magazine pocket may not be the same on every machine.
HTool length compensation memory number (e.g. G43 H01 Z50.;). Good practice: T01→H01, T02→H02.
DTool radius/diameter compensation number (e.g. G41 D01;). Detail in a later lesson.
MAuxiliary function (spindle, coolant, tool change, program stop/end).

9. An important rule about M codes

As a safe habit, one M code per block is preferred (S1500 M03;, then M08; on a separate line). Some machines accept S1500 M03 M08;, but not every control handles several M codes in one block the same way. On FANUC one M code is usually stated; some parameters can support up to three; codes such as M00, M01, M02, M30, M98, M99 must not be used in the same block as other M codes. This training uses the clear/safe structure (separate lines).

10. Program comments – parentheses

Comments use parentheses: O1001 (SLOTTING PROGRAM);, N10 (T1 D10 END MILL);. Comments ease reading and separate operations. Special local characters, very long comments or unsupported marks can cause data-transfer problems; industry prefers plain notation ((T1 D10 END MILL)).

11. Block-skip mark – /

A / at the line start makes that line skipped if Block Delete is on (/N80 M01;). If the switch is off, the line runs normally. It is used for optional measurement, trial moves or temporary operations. A move needed for safety must not be left only on a block-skip line.

12. Modal and single-block commands

A modal command stays active once written until another command from the same group appears. E.g. after G01 X50. F200.;, the lines Y30.; and X10.; do not repeat G01. Modal examples: G00–G03, G17–G19, G40–G42, G43/G49, G54–G59, G90/G91, G94/G95, F and S. Some codes (reference, dwell, one-shot) are effective only on their block (non-modal). Danger: if G91 stays active, X100. is read not as a target but as a 100 mm move from the current point; so the required modal states must be rewritten explicitly at each tool start.

13. The safety line at the program start

Programs use a line at the start that puts the control in a known state:

G17 G21 G40 G49 G80 G90 G94;

Meaning: G17 XY plane, G21 metric, G40 radius comp cancel, G49 length comp cancel, G80 drilling-cycle cancel, G90 absolute, G94 feed per minute. This is called the “safety/safe start line.” It cancels modal states left from a previous program such as G91, G41, G81 or G18; otherwise the first moves can be unexpected.

14. Is the safety line the same on every machine?

It need not be identical; the supported functions, factory standards, measurement system and length-comp management can differ. So the safety line is prepared per the control and builder manual. This training uses G17 G21 G40 G49 G80 G90 G94; for a three-axis FANUC VMC.

15. Basic sections of a CNC milling program

A tidy program consists of: program number and comment → safety start line → tool call/change → workpiece coordinate system → spindle speed/direction → tool length compensation → safe approach → cutting moves → safe tool retract → coolant/spindle stop → return to reference/tool-change position → program end.

16. A simple program template

%
O1001 (SIMPLE MILLING PROGRAM);
N10 G17 G21 G40 G49 G80 G90 G94;
N20 T01 M06;
N30 G54;
N40 S1500 M03;
N50 G00 X10. Y10.;
N60 G43 H01 Z50.;
N70 M08;
N80 G00 Z5.;
N90 G01 Z-2. F100.;
N100 G01 X70. F250.;
N110 Y40.;
N120 X10.;
N130 Y10.;
N140 G00 Z50.;
N150 M09;
N160 M05;
N170 G91 G28 Z0.;
N180 G90;
N190 M30;
%

This is not a production-ready part program; its purpose is to teach the program sections.

17. Line-by-line explanation of the program

  • % marks the program start/end in some data-transfer systems. O1001 (...) program number and comment.
  • N10 safety line — the control is put in a known start state.
  • N20 T01 M06; selects and changes tool 1; N30 G54; selects the workpiece zero.
  • N40 S1500 M03; spindle 1500 rpm clockwise. N50 G00 X10. Y10.; rapid X-Y positioning.
  • N60 G43 H01 Z50.; tool length compensation active (H01), move to Z50. Critical: when using T01, if a different H is called by mistake the tool can go to an unexpected Z; the T01→H01 match must be checked.
  • N70 M08; coolant on. N80 G00 Z5.; 5 mm above the part (safety checked against clamping/part height).
  • N90 G01 Z-2. F100.; enter 2 mm into the part at 100 mm/min. N100–N130 linear machining (G01 and F250 are modal, so not repeated).
  • N140 G00 Z50.; retract to safe Z first. N150 M09; N160 M05; coolant/spindle stop. N170 G91 G28 Z0.; N180 G90; Z reference and G90 again. N190 M30; program end.

18. Program structure with several tools

Vertical machining centers can use several tools in one program (e.g. T01 face mill, T02 center drill, T03 drill, T04 end mill). Each tool section should be written as independently and safely as possible:

O1200 (MULTI-TOOL PROGRAM);
G17 G21 G40 G49 G80 G90 G94;
(T1 FACE MILL)
T01 M06;
G54 S1200 M03;
G00 X... Y...;
G43 H01 Z...;
M08;
...
G00 Z...;
M09;
M05;
G49;
(T2 CENTER DRILL)
T02 M06;
G54 S1800 M03;
G00 X... Y...;
G43 H02 Z...;
M08;
...
G80;
G00 Z...;
M09;
M05;
G49;
M30;

19. Why write each tool section independently?

Assuming which modal codes remained active from the previous section is dangerous. If G90 is not written in the second tool section and the previous one stayed in G91, all coordinates may be interpreted as incremental. So at each tool start the basics (G17 G40 G49 G80 G90; G54;) and the tool number, H number, speed, feed, coolant and cycle cancel should be repeated. Independent tool sections make it easy to safely restart the program from a given tool.

20. Features of a good program format

A good program is not just one that runs; it must also be readable, checkable, editable, understandable by another operator and safely restartable. Good example:

N10 G17 G21 G40 G49 G80 G90 G94;
N20 T01 M06;
N30 G54 S1500 M03;
N40 G00 X10. Y10.;
N50 G43 H01 Z50.;
N60 M08;

Poor example: G90G0X10Y10T1M6S1500M3G43H1Z50M8; — even if technically readable by some controls, it is hard for a person to read, difficult to debug, and packs too many operations in one block. Shorter is not always better.

21. Order of writing the codes

Although many addresses can be written in different orders, a standard habit is useful. Suggested general order: N → G → X/Y/Z → I/J/K/R → F → S → T → H/D → M. E.g. a move line N80 G01 X50. Y30. F250.;, a tool line N20 T01 M06;, a comp line N50 G43 H01 Z50.;. The aim is programs close to the same format across the whole shop.

22. Decimal point, sign and spacing

Safe notation uses a decimal point (X10., Y25., Z5.): X10. instead of X10 clearly shows the value is 10 mm. The decimal separator must be a point (correct X25.5, wrong X25,5). The + on positive values is usually omitted (X+50. = X50.); the minus is mandatory on negatives (X-50.) and forgetting it means moving the wrong way. Many FANUC controls read without spaces (G90G54G00X50.), but spaced notation is preferred for readability.

23. The difference between G codes and M codes

G codes mostly set the type of motion, the coordinate system, the plane, the measurement method, the compensation behavior and the drilling cycles (G00, G01, G17, G43, G54, G81, G90). M codes mostly control the spindle, coolant, tool change, program stop/end and auxiliary operations (M03, M05, M06, M08, M09, M30). In short: G codes set how the tool path/state will be, M codes set the machine’s auxiliary functions.

24. Basic M codes

M codeFunction
M00Unconditional program stop; Cycle Start needed to continue (turn part, measure, manual operation).
M01Optional stop; stops if the panel Optional Stop is on.
M03 / M04Turn spindle clockwise / counter-clockwise.
M05Stop the spindle.
M06Tool change.
M08 / M09Coolant on / off.
M30Program end and, on most FANUC, rewind to the start.
M98 / M99Subprogram call / return from subprogram.

M02 vs M30: M02 marks the program end but its rewind behavior can vary by system; M30 is the program end and usually a rewind to the start. Daily machining-center programs mostly prefer M30. With M03, an S value should be given (S1500 M03;); otherwise it may turn at the previous S value.

25. Common mistakes when writing a program

  • Writing the program number wrongly: O1001; not 01001; (the leading character is the letter O, not zero).
  • Writing a number without an address: G01 50. Y20.; wrong → G01 X50. Y20.;. On FANUC an address-less number at block start gives an “Address Not Found” error.
  • Not writing a value after the address: G01 X Y20.; wrong → G01 X50. Y20.;. If an address is followed by another address/EOB instead of a number, a “No Data After Address” error.
  • Wrong H number: applying H01 to T02 by mistake can cause a serious Z crash depending on tool length.
  • Writing M03 without an S and forgetting to turn coolant off (M09).
  • Leaving G91 active at the program end: on restart the first coordinates may run incremental. Safe: G91 G28 Z0.; G90; M30; plus G90 at the program start.
  • An X-Y move before the tool retracts to safe Z: retract with G00 Z50.; first, then move X-Y.

26. Program check order

Program start: is the O number correct, G21 metric, G90 absolute, G17 plane, G40/G49/G80 cancel, and the right G54–G59 selected? Tool: are the T/H/D numbers correct and matched, is the tool in the right magazine pocket, are the diameter/length correct? Technology: are the S speed, M03/M04 direction, F feed and coolant correct? Motion: is the first X-Y safe, does length compensation activate at a safe Z, are the negative Z depths and clamping elements correct? Program end: safe Z first, M09, M05, a safe reference return, G90 again and M30?

27. An example safe program format (template)

%
O____ (PART NAME);
N10 G17 G21 G40 G49 G80 G90 G94;
N20 (T1 TOOL DESCRIPTION);
N30 T01 M06;
N40 G54 S____ M03;
N50 G00 X____ Y____;
N60 G43 H01 Z____;
N70 M08;
N80 G00 Z____;
N90 G01 Z____ F____;
N100 G01 X____ Y____ F____;
N110 ...;
N200 G00 Z____;
N210 M09;
N220 M05;
N230 G49;
N240 G91 G28 Z0.;
N250 G90;
N260 M30;
%

The blanks are not filled in randomly; each value is calculated from the drawing, tool diameter/length, clamping layout, material and machine features.

28. The difference between programming and the real machine

A program looking correct on paper does not mean it is safe on the real machine. The G54–G59 and tool length/radius offsets, the magazine tool order, the part clamping position and its real height, the vise/fixture position and machine-specific M codes all affect the result. FANUC safety rules require checking the entered data, feed values and the direction and amount of tool compensations before running.

29. Safety method before running the program

For a new/modified program: verify the program number; physically check the tools; examine the T–H–D match and the G54–G59 values; check the G90/G91 state and the Z values; display the program graphics/tool path; use Machine Lock or simulation; use Dry Run carefully; turn on Single Block; lower Rapid Override; keep a hand near Feed Hold on the first approach; use Emergency Stop instead of RESET in danger. Remember the Dry Run speed can differ from and sometimes exceed the programmed F.

30. Lesson summary

  • A CNC program is made of blocks, and blocks of words; a word is address + numeric value. The program number is O, the line number N, the block end EOB (;).
  • G codes set preparatory/motion states; X/Y/Z the axis values; F feed, S speed, T tool; H length, D radius comp number; M codes the auxiliary operations.
  • Modal commands stay active until the opposite command; the safety line cancels previous modal states.
  • In multi-tool programs each tool section should be independent; a tidy format eases debugging and safe restart.