21 Unit 4: CNC Language and Structure


After completing this unit, you should be able to:

  • Identify the programs list instructions.
  • Understand the Program Format
  • Describe Letter Address Commands codes
  • Describe Special Character Code Definitions.
  • Understand the G & M Codes.

CNC programs list instructions to be performed in the order they are written. They read like a book, left to right and top-down. Each sentence in a CNC program is written on a separate line, called a Block. Blocks are arranged in a specific sequence that promotes safety, predictability and readability, so it is important to adhere to a standard program structure.

The blocks are arranged in the following order:

  • Program Start
  • Load Tool
  • Spindle On
  • Coolant On
  • Rapid to position above part
  • Machining operation
  • Coolant Off
  • Spindle Off
  • Move to safe position
  • End program

The steps listed above represent the simplest type of CNC program, where only one tool is used and one operation performed. Programs that use multiple tools repeat steps two through nine for each.

Table 3 and Table 4 in section G & M Codes show the most common G and M codes that should be memorized if possible.

Like any language, the G-code language has rules. For example, some codes are modal, meaning they do not have to be repeated if they do not change between blocks. Some codes have different meanings depending on how and where there are used.

While these rules are covered in this chapter, do not concern yourself with learning every nuance of the language. It is the job of the job of the CAD/CAM software Post Processor to properly format and write the CNC program.

Program Format

The program in Table 1: below machines a square contour and drills a hole.






(T1 0.25 END MILL)

 G17 G20 G40 G49 G80 G90

Start of program.

Program number (Program Name).

Tool description for operator.

Safety block to ensure machine is in safe mode.

Start Program

 T1 M6

 S9200 M3

Load Tool #1.

Spindle Speed 9200 RPM, On CW.

Change Tool



 G00 X-0.025 Y-0.275

 G43 Z1.H1


 G01 Z-0.1 F18.

Use Fixture Offset #1.

Coolant On.

Rapid above part.

Rapid to safe plane, use Tool Length Offset #1.

Rapid to feed plane.

Line move to cutting depth at 18 IPM.

Move to Position

 G41 Y0.1 D1 F36.





 G40 X-0.4

 G00 Z1.

CDC Left, Lead in line, Dia. Offset #1, 36 IPM.

Line move.

Line move.

Line move.

Line move.

Turn CDC off with lead-out move.

Rapid to safe plane.

Machine Contour



(T2 0.25 DRILL)

 T2 M6

 S3820 M3

Spindle Off.

Coolant Off.

Tool description for operator.

Load Tool #2.

Spindle Speed 3820 RPM, On CW.

Change Tool


 X1. Y1.

 G43 Z1.H2


Coolant On.

Rapid above hole.

Rapid to safe plane, use Tool Length Offset 2.

Rapid to feed plane.

Move to Position

 G98 G81 Z-0.325 R0.1 F12.



Drill hole (canned) cycle, Depth Z-.325, F12.

Cancel drill cycle.

Rapid to safe plane.

Drill Hole



G91 G28 Z0

G91 G28 X0 Y0




Spindle Off.

Coolant Off.

Return to machine Home position in Z.

Return to machine Home position in XY.

Reset to absolute positioning mode (for safety).

Reset program to beginning.

End Program.

End Program

Letter Address Commands codes

The command block controls the machine tool through the use of letter address commands.  Some are used more than once, and their meaning changes based on which G-code appears in the same block.

Codes are either modal, which means they remain in effect until cancelled or changed, or non-modal, which means they are effective only in the current block.  As you can see, many of the letter addresses are chosen in a logical manner (T for tool, S for spindle, F for feed rate, etc.).

The table below lists the most common Letter Address Commands codes.

Table 2: Letter Address Commands Codes





Absolute or incremental position ofAaxis (rotational axis around X axis)

A,B,C – 4th/5th Axis Rotary Motion

Rotation about the X, Y or Z-axis respectively.The angle is in degrees and up to three decimal places precision.
G01 A45.325B90.


Absolute or incremental position of B axis (rotational axis around Y axis)

Same as A


Absolute or incremental position of C axis (rotational axis around Z axis)

Same as B


Defines diameter or radial offset used for cutter compensation

Used to compensate for tool diameter wear and deflection.D is accompanied by an integer that isthe same as the tool number (T5 uses D5,etc). No decimal point is used. Itis always usedin conjunction with G41 or G42 and a XY move (never an arc). When called, the control reads the register and offsets the tool path left (G41) or right (G42) by the value in the register.
G01 G41 X2.D1


Precision feed rate for threading on lathes



Defines feed rate

Sets the feed rate when machining lines, arcs or drill cycles.Feed rate can be in Inches per Minute (G94 mode) or Inverse Time (G93 mode). Feed rates can be up to three decimal placesaccuracy (for tap cycles) and require a decimal point.
G01 X2.Y0. F30.


Address for preparatory commands

G commands often tell the control what kind of motion is wanted (e.g., rapid positioning, linear feed, circular feed, fixed cycle) or what offset value to use.

G02 X2.Y2.I.50J0.


Defines tool length offset;

Incremental axis corresponding to C axis (e.g., on a turn-mill)

This code calls a tool length offset (TLO) register on the control. The control combines the TLO and Fixture Offset Z values to know where the tool is in relation to the part datum.It is always accompanied by an integer (H1, H2,etc), G43, and Z coordinate.
G43 H1 Z2.


Defines arc size inX axisfor G02 or G03 arc commands.

Also used as a parameter within some fixed cycles.

For arc moves (G2/G3), this is the incremental X-distance from the arc start point to the arc center. Certain drill cycles also use I as an optional parameter.
G02 X.5 Y2.500I0.J0.250



Defines arc size inY axisfor G02 or G03 arc commands.

Also used as a parameter within some fixed cycles.

For arc moves (G2/G3), this is the incremental Y-distance from the arc start point to the arc center. Certain drill cycles also use J as an optional parameter.
G02 X.5 Y2.500 I0.J0.250



Defines arc size inZ axisfor G02 or G03 arc commands.

Also used as a parameter within some fixed cycles, equal to L address.

For anarcmove (G2/G3) this is the incremental Z-distance from the arc start point to the arc center. In the G17 plane, this is the incremental Z-distance for helical moves. Certain drill cycles also use J as an optional parameter.
G18 G03 X.3 Z2.500 I0.K0.250



Fixed cycle loop count;

Specification of what register to edit using G10

Fixed cycle loop count: Defines number of repetitions (“loops”) of a fixed cycle at each position.Assumed to be 1 unless programmed with another integer.Sometimes the K addressis usedinstead of L. With incremental positioning (G91), a series of equally spaced holes can be programmed as a loop rather than as individual positions.G10 use:Specificationof what register to edit (work offsets, tool radius offsets, tool length offsets, etc.).


Miscellaneous function

Always accompanied by an integer that determines its meaning.Only one M-codeis allowedin each block of code. Expanded definitions of M-codes appear later in this chapter.


Line (block) number in program;

System parameter number to be changed using G10

Block numbers can make the CNC program easier to read. They are seldom required for CAD/CAM generated programs with no subprograms. Because they take up controlmemorymost 3D programs do not use block numbers. Block numbers are integers up to five characters long with no decimal point. They cannot appear before the tape start/end character (%) and usually do not appear before a comment only block.
N100 T02 M06


Program name

Programs are stored on the control by their program number. Thisis an integer thatis preceded by the letter O and has no decimal places.
O1234 (Exercise 1)



Serves as parameter address for various G and M codes

Dwell (delay) in seconds.Accompanied by G4 unless used within certain drill cycles.
G4 P.1



Peck increment in canned cycles

The incremental feed distance per pass in a peck drill cycle.
G83 X2.000 Y2.000 Z-.625 F20.R.2 Q.2 P9.



Defines size of arc radius or defines retract height in canned cycles

Arcs can be defined using the arc radius R or I,J,Kvectors. IJK’s are more reliable than R’s so itis recommendedto use them instead. Ris also usedby drill cycles as the return plane Z value.
G83 Z-.625 F20.R.2 Q.2 P9.



Defines speed, either spindle speed or surface speed depending on mode

Spindle speed in revolutions per minute (RPM). It is an integer value with no decimal, and always used in conjunction with M03 (Spindle on CW) or M04 (Spindle on CCW).



Tool selection

Selects tool. It is an integer value always accompanied by M6 (tool change code).
T01 M06


Incremental axis corresponding to X axis (typically only lathe group A controls)

Also defines dwell time on some machines.

In these controls, X and U obviate G90 and G91, respectively. On these lathes, G90 is instead a fixed cycle address for roughing.


Incremental axis corresponding to Y axis

Until the 2000s, the V address was very rarely used, because most lathes that used U andWdidn’thave a Y-axis, so they didn’t use V. (Green et al 1996 did not even list V in their table of addresses.) That is still often the case, although the proliferation of live lathe tooling and turn-mill machining has made V address usage less rare than it used to be (Smid2008 shows an example).


Incremental axis corresponding to Z axis (typically only lathe group A controls)

In these controls, Z and W obviate G90 and G91, respectively. On these lathes, G90 is instead a fixed cycle address for roughing.


Absolute or incremental position ofX axis.

Coordinate data for the X-axis. Up to four places after the decimalare allowedand trailing zeros are not used. Coordinates are modal, so there is no need to repeat them in subsequent blocks if they do not change.
G01 X2.250F20.


Absolute or incremental position of Y axis

Coordinate data for the Y-axis.

G01 Y2.250 F20.


Absolute or incremental position of Z axis

Coordinate data for the Z-axis.

Special Character Code Definitions

The following is a list of commonly used special characters, their meaning, use, and restrictions.

% – Program Start or End

All programs begin and end with % on a block by itself. This code is called tape rewind character (a holdover from the days when programs were loaded using paper tapes).

( ) – Comments

Comments to the operator must be all caps and enclosed within brackets. The maximum length of a comment is 40 characters and all characters are capitalized.
(T02: 5/8 END MILL)

/ – Block Delete

Codes after this character are ignored if the Block Delete switch on the control is on.
/ M00

; – End of Block

This character is not visible when the CNC program is read in a text editor (carriage return), but does appear at the end of every block of code when the program is displayed on the machine control.
N8 Z0.750 ;

G & M Codes

G&M Codes make up the most of the contents of the CNC program. The definition of each class of code and specific meanings of the most important codes are covered next.


Codes that begin with G are called preparatory words because they prepare the machine for a certain type of motion.

Table 3: G-Code




Rapid motion.Used to position the machine for non-milling moves.


Line motion at a specified feed rate.


Clockwise arc.


Counterclockwise arc.




Return to machine home position.


Cutter Diameter Compensation (CDC) off.


Cutter Diameter Compensation (CDC) left.


Cutter Diameter Compensation (CDC) right.


Tool length offset (TLO).


Fixture Offset #1.


Fixture Offset #2.


Fixture Offset #3.


Fixture Offset #4.


Fixture Offset #5.


Fixture Offset #6.


Cancel drill cycle.


Simple drill cycle.


Simple drill cycle with dwell.


Peck drill cycle.


Tap cycle.


Absolute coordinate programming mode.


Incremental coordinate programming mode.


Drill cycle return to Initial point (R).


Drill cycle return to Reference plane (last Z Height)


Codes that begin with M are called miscellaneous words. They control machine auxiliary options like coolant and spindle direction. Only one M-code can appear in each block of code.

Table 4: M-Codes




Program stop.Press Cycle Start button to continue.


Optional stop.


End of program.


Spindle on Clockwise.


Spindle on Counterclockwise.


Spindle stop.


Change tool.


Coolant on.


Coolant off.


End program and press Cycle Start to run it again.

Select G-Code Definitions (Expanded)

G00 – Rapid Move 

This code commands the machine to move as fast as it can to a specified point. It is always used with a coordinate position and is modal. Unlike G01, G00 does not coordinate the axes to move in a straight line. Rather, each axis moves at its maximum speed until it is satisfied. This results in  motion as shown in Figure 18, below.

G00 X0. Y0.

Figure 1. G00 Motion
Caution: The rapid speed of some machines can exceed 1. An incorrect offset or coordinate move can crash the machine faster than the operator can hit the emergency stop. Use the rapid feed override on the machine when running a program for the first time.
Linear Motion is Straight Line Motion: 

G-Code is about motion, and the most common kind of motion found in part programs is straight line or linear motion. Motion is another one of those things in G-Code that is modal. You tell the controller what kind of motion you’d like with a G-Code and it remembers to always make that kind of motion until you tell it to change using another G-Code.

G00 for Fast Positioning ; Rapids Motion as fast as your machine will go. Used to move the cutter through air to the next position it will be cutting.

G01 for Slower Cutting Motion; Feed Motion slower, for cutting.  Feedrate set by “F” G-Code.

F-word = “F” as in “Feedrate”.

S-word = “S” as in “Spindle Speed”, address is rpm.

Specifying Linear Motion With X, Y, and Z:

Specifying G00 or G01 does not cause any motion to happen–they merely tell the controller what type of motion is expected when you finally tell it where to move to. For actual motion you need to specify a destination using the X, Y, and Z words.  To move to the part zero, we might issue a command like this:

G00 X0 Y0 Z0       Or  use G01 if you want to go slower   G01 X0 Y0 Z0 F40.

Interpolated motion or an interpolated move, When we specify multiple coordinates on a line, means more than one axis of the machine is moving at the same time. In fact, the controller will move them all at exactly the right speed relative to one another so that the cutter follows a straight line to the destination and moves at the feedrate.

If we specify the same destination, but spread the coordinates over multiple lines, each line is a separate move:

G00 X0 Y0 (Move to X0 Y0 in one move, keeping Z constant)

Z0 (Move to Z0 in one move, keeping X and Y constant)

G00 and G01 are modal, so we only have to specify them when we want to change modes.

Z Axis: 

The concept of interpolated moves raises an interesting issue for the Z axis. It’s often a good idea to move the depth-of-cut-axis on its own, rather than as coordinated motion with other axes(X and Y). Whether you’re going to have a problem (collision) as the cutter gets close to the workpiece and fixturing. First moving in X and Y and then moving in Z , it’s much easier to judge whether an accidental collision is about to take place. You’re also much less likely to hit some random object sticking up, like a clamp, if you keep the cutter high until you’re directly over where you want to start cutting.

G02 and G03 Circular Motion is a Mode Initiated: 

G02 establishes a mode for clockwise circular arcs.

G03 establishes a mode for counter-clockwise circular arcs.

The G02 or G03 mode is established, arcs are defined in G-Code by identifying their 2 endpoints and the center which must be equi-distant from each endpoint. The endpoints are easy. The current control point, or location when the block is begun establishes one endpoint. The other may be established by XYZ coordinates. The center is most commonly identified by using I, J, or K to establish relative offsets from the starting point of the arc to the center.


Figure 2. An Arc’s center with IJK

This arc starts at X0Y2. and finishes at X2.Y0. It’s center is at X0Y0. We could specify it in G-code like this:

G02 (Set up the clockwise arc mode)

X2Y0 I0J-2.0

The Center Using Radius “R”. 

The center just by specifying the radius of the circle.  Circle has a radius of 2, so the G-Code might be simply:

G02 X2Y0 R2

G17/G18/G19 – Plane Designation

Arcs must exist on a plane designated by the command G17 (XY), G18 (XZ) or G19 (YZ). G17 is the machine default.

Figure 3. Plane Designation

G40/G41/G42 – Cutter Diameter Compensation (CDC)

CDC is a key to precision CNC machining, allowing the operator to compensate for tool wear and deflection by commanding the machine to veer left (G41) or right (G42) from the programmed path. G40 cancels cutter compensation. The amount of offset is entered in a CNC control D-register. The wear register can be thought of like a table that the control refers to with every move.

Table 5: Diameter Offset Register

Tool Diameter Offset














The value in the D-register is calculated by the machine tool operator, who monitors the finished size of part features, compares them with the print, and enters the difference in the register as needed to keep the part within specifications. If there is no deviation, the register is set to zero.

G01 G41 D1 X1.0 Y.25 F40. 

G43 – Tool Length Compensation

G43 activates tool length compensation. It is always accompanied by an H-code and Z-move, where H is the tool length offset (TLO) register to read, and Z is the height to go to in reference to the part datum.

The (TLO) can be thought of like a table on the control:

Table 6: Work Offsets

Tool Length Resister














The TLO is combined with the active fixture offset on the control so the machine knows where the tip of the tool is in relation to the part datum.

G43 H1 Z1.

G54 – Work Offset 

Work offsets are data registers in the CNC control that hold the distance from the machine home X, Y, Z position to the part datum. These offsets can be thought of like a table on the control:

Table 7: Work Offset

Work Offset




























Tip: G54 is usually usedfor the first machining setup. Additional offsets are used to machine other sides of the part.

The X and Y values represent the distance from the machine home to part datum XY. The Z value is the distance from the tool reference point (for example, the top of a 1-2-3 block) and the part Z-datum.

G54 X0. Y0.


1. Please describe the CNC program list instruction.

2. All CNC program start and end with what?

3. Describe letter address Commands codes.

4. Please lists three special character codes.

5. Describe G and M codes.

6. Please describe G00 G90 G54 X0 Y0.

7. Please describe G00 G90 G43 H1 Z1.

8. What is the different between G00 and G01?

9. Explain the different between G02 and G03.

10. Please Describe the F and S word.


Icon for the Creative Commons Attribution 4.0 International License

Manufacturing Processes 4-5 Copyright © by LamNgeun Virasak is licensed under a Creative Commons Attribution 4.0 International License, except where otherwise noted.

Share This Book