G-Code Fundamentals: Program Structure, Coordinates, and Modal Commands
G-code is the ISO 6983 standardized language that controls CNC machine tools — translating machining intent into axis motion, spindle speed, feed rate, and auxiliary commands. UTEC Industrial provides precision CNC machining services for large and oversized industrial components in the Pacific Northwest, with in-house heat treatment and induction hardening integrated into the machining workflow. Every movement a CNC lathe or machining center makes is driven by a G-code program, whether written by hand, generated by CAM, or produced by a conversational interface. This article covers the structure of a G-code program, coordinate system conventions, modal and non-modal commands, and the preparatory and miscellaneous codes that govern production machining.
What is G-code and how does a CNC machine use it?
G-code is a numerical control (NC) programming language standardized under ISO 6983-1 (internationally) and EIA RS-274D (in the US), though in practice most CNC controls — including Fanuc, Siemens, Mazatrol, and their compatibles — implement a superset of the standard with manufacturer-specific extensions and variations. A G-code program is a sequential list of blocks (lines), where each block contains one or more words (letter-address codes) that define a single machine action: move axis X to this position at this feed rate, turn on the spindle at this speed and direction, activate coolant, call a subprogram, or end the program. The CNC control reads each block in sequence, interprets the words, and sends commands to the servo drives, spindle motor, and auxiliary systems. The machine does exactly what the program says — no more, no less. This is both the power and the risk of G-code programming: an error in the program (wrong sign on a dimension, wrong feed rate, wrong tool call) produces exactly the wrong result, with no automatic correction unless the control has active collision detection. The distinction between manual G-code programming and CAM-generated G-code is workflow, not language: a CAM system generates the same G-code words that a machinist would write manually, but produces them automatically from a 3D model and toolpath strategy. For simple turning operations — facing, roughing OD, finish boring — many experienced machinists write G-code directly at the machine or in a text editor; for complex 3D milling paths, CAM generation is effectively mandatory. UTEC Industrial's machinists program straightforward turned and bored components directly in G-code, leveraging decades of shop-floor programming experience (Smid, CNC Programming Handbook, 3rd ed., Industrial Press, 2008).
What is the structure of a G-code block and what does each address letter mean?
A G-code block is a single line of text containing one or more words. Each word is an address letter followed immediately by a numerical value. The address letters and their meanings: N — block number (optional sequence number for reference; the machine executes blocks in file order, not N-number order); G — preparatory function code (defines the type of motion or mode); X, Y, Z — axis coordinates (absolute or incremental position of the controlled axes); A, B, C — rotary axis coordinates (degrees of rotation around X, Y, Z respectively); I, J, K — incremental arc center offsets from current position (used in circular interpolation); R — arc radius (alternative to I/J/K for circular moves on many controls); F — feed rate (inches per minute for milling, inches per revolution for turning); S — spindle speed (RPM, or surface feet per minute when G96 constant surface speed is active); T — tool number (selects the tool from the turret or tool magazine); M — miscellaneous function code (controls auxiliary functions: spindle on/off, coolant, program end, tool change); D — tool diameter offset register number (for cutter radius compensation in milling); H — tool length offset register number (for tool length compensation in milling). A typical turning program block: N0050 G01 X2.8750 Z-3.500 F0.008 — this instructs the control to execute a linear interpolation move (G01) to X coordinate 2.8750 and Z coordinate -3.500 at a feed rate of 0.008 inches per revolution. Every address in the block changes a machine parameter for that block and, if the code is modal, for all subsequent blocks until changed (Smid, CNC Programming Handbook, 3rd ed., Industrial Press, 2008; Kief et al., The CNC Handbook, Industrial Press, 2020).
What is the difference between modal and non-modal G-codes?
Modal G-codes remain active until canceled or replaced by another code in the same group — they set a mode that persists across multiple blocks without being re-stated. Non-modal G-codes are one-shot: they apply only to the block in which they appear and do not persist. The majority of the most commonly used G-codes are modal, which allows programs to be written concisely — a machinist roughing a turning profile at G01 (linear feed) does not need to re-write G01 on every block; the code stays active until G00 (rapid traverse) or G02/G03 (circular interpolation) replaces it. Modal G-codes are organized into groups where only one code from a group can be active at a time. Group 1 (the motion group) is the most important: G00 (rapid traverse), G01 (linear feed), G02 (clockwise circular), G03 (counterclockwise circular). Only one can be active — switching from G01 to G02 in a block automatically cancels the G01 mode. Group 3 is the plane selection group: G17 (XY plane for circular interpolation), G18 (XZ plane, the standard for CNC turning), G19 (YZ plane). Group 6 is the units group: G20 (inch mode), G21 (metric mode) — critical to specify at the beginning of every program. Group 7 is the cutter radius compensation group: G40 (cancel compensation), G41 (compensate left of path), G42 (compensate right of path). Non-modal examples: G04 (dwell — pauses execution for a specified time in a single block), G28 (return to reference position — executes in that block only), G92 (coordinate system setting on some controls). Understanding modality is essential for reading and editing programs: a G02 buried in the middle of a long program will continue to execute circular moves on every subsequent block until a G01 or G00 explicitly cancels it — a common source of crashes when editing programs without understanding the modal state of the program at the edit point (Smid, CNC Programming Handbook, 3rd ed., Industrial Press, 2008).
How are coordinate systems and work offsets used in G-code programming?
A CNC machine operates in its machine coordinate system (MCS) — the absolute coordinate frame fixed to the machine structure, with the origin defined by the home or reference position of each axis. Machine coordinates are used for safe positions, tool change positions, and home moves, but they are rarely used for part programming because the part is loaded in a different location every time. Work offsets (also called work coordinate systems or fixture offsets, designated G54 through G59 on most Fanuc-compatible controls, with G54 the most common default) shift the programming coordinate system origin to a defined point on the part — typically the part datum used on the drawing. When G54 is active, the programmer programs all coordinates relative to the part datum (X0, Y0, Z0 = the part datum), and the control automatically adds the G54 offset values to convert to machine coordinates. This means the program coordinates match the drawing coordinates, which reduces the chance of sign errors and simplifies program writing. Setting the work offset: the machine operator or setup person probes or touches off a datum surface on the part, reads the machine coordinate of that point, and enters it as the G54 offset. When the program calls G54, the control uses that offset value as the origin. For CNC turning, the Z-zero is almost universally set at the finished face of the part (the Z-datum on the drawing), and X-zero is the centerline of the spindle (radius = 0). A facing pass to Z0 machines the part face to the datum; turning to a diameter of X3.500 produces a 3.500-inch diameter. Separate programs for different parts simply use different G54 offset values — the program coordinates remain relative to the part datum, and the offset handles the physical location of the part in the machine (Smid, CNC Programming Handbook, 3rd ed., Industrial Press, 2008; Madison, CNC Machining Handbook, Industrial Press, 1996).
What are the most important G-codes for CNC turning programs?
A CNC turning program for a straightforward steel shaft or bore uses a small subset of the available G-code vocabulary. The essential turning codes: G20/G21 — inch or metric mode; always stated explicitly at the top of the program. G96/G97 — constant surface speed (CSS) mode vs. constant RPM mode. G96 maintains a programmed surface speed (e.g., G96 S400 = 400 SFM) by automatically adjusting RPM as the tool moves to different X positions — essential for facing operations where the effective diameter changes from the OD to the center. G97 is used for threading (where a fixed RPM is required) and for very small diameters where G96 would drive RPM above the spindle maximum. G00 — rapid traverse; used to position the tool from the tool change position to the start of cut, and between cuts. G00 moves are at maximum axis velocity with no feed rate control — the tool must not contact the workpiece during G00 moves. G01 — linear feed at a programmed F rate; used for facing, OD turning, boring, chamfering, and any straight-line cutting move. G02/G03 — clockwise and counterclockwise circular interpolation; used for radii and contour blends at the programmed feed rate. G71 — stock removal in turning (a rough turning canned cycle on Fanuc controls that automatically calculates multiple roughing passes based on a defined profile, leaving a finishing stock allowance); reduces program length dramatically compared to manually programming every roughing pass. G70 — finishing cycle (used after G71 to execute the final finishing pass at the programmed profile). G76 — threading cycle (automatically calculates multiple passes for a thread form defined by pitch, depth, and form angle). M03/M04/M05 — spindle on clockwise, counterclockwise, stop. M08/M09 — coolant on, coolant off. M30 — program end and rewind. A complete program for turning a steel shaft consists almost entirely of these codes plus the coordinate values (Smid, CNC Programming Handbook, 3rd ed., Industrial Press, 2008).
What are the most important G-codes for CNC milling programs?
CNC milling programs use the same address letter structure as turning programs but with different motion conventions and additional codes for 3-axis control. The essential milling codes: G17/G18/G19 — plane selection for circular interpolation (G17 = XY plane, the standard for vertical machining centers; G18 = XZ plane; G19 = YZ plane). G43 — tool length offset compensation; activates the stored tool length offset for the called tool (H word), compensating for the physical length of each tool. Without G43, the Z-zero defined in the work offset would need to account for each tool's length individually — G43 allows the programmer to use a consistent Z-zero (the part datum) regardless of which tool is in the spindle. G41/G42 — cutter radius compensation (left and right); offsets the tool path by the stored tool radius (D word), allowing the programmer to program the part contour directly rather than the centerline of the tool. G81 — drilling canned cycle; executes a drill-to-depth-and-retract cycle at each called location without programming the individual Z-approach, feed, and retract moves. G84 — tapping canned cycle (rigid tapping with synchronized spindle and feed). G80 — cancel all canned cycles. G91 — incremental mode (all axis coordinates are incremental distances from the current position, rather than absolute coordinates from the work offset origin); G90 returns to absolute mode. G54–G59 — work offset selection; allows rapid switching between different fixture positions or parts set up in the same machine. Understanding the interaction between G43 tool length compensation, G54 work offset, and G41/G42 cutter radius compensation is the foundation of accurate milling programming — errors in any of these offset inputs directly translate to dimensional errors in the machined part (Smid, CNC Programming Handbook, 3rd ed., Industrial Press, 2008; Kief et al., The CNC Handbook, Industrial Press, 2020).
How are spindle speed and feed rate specified in G-code for different operations?
Spindle speed and feed rate interact with the G96/G97 mode in turning and with the S and F address words in both turning and milling. For CNC turning: in G96 (constant surface speed) mode, the S word specifies surface feet per minute (SFM) or meters per minute — the control calculates and continuously adjusts RPM to maintain this surface speed as the cutting diameter changes. For a finish turn of AISI 4140 at S450 (450 SFM), the RPM at a 10-inch diameter = (450 × 12) / (π × 10) = 172 RPM; at a 2-inch diameter on the same pass, RPM = (450 × 12) / (π × 2) = 859 RPM. The F word in turning typically specifies inches per revolution (ipr) — the distance the tool advances per spindle revolution, which directly controls chip thickness and surface finish. F0.008 = 0.008 inch per revolution feed; at 450 SFM on a 10-inch diameter this produces a linear feed rate of 172 × 0.008 = 1.38 inches per minute. For CNC milling: the S word specifies RPM directly (G97 mode is standard for milling because the cutting diameter — the tool diameter — is constant). The F word specifies inches per minute (ipm) — F20 = 20 inches per minute of table travel. To convert between chip load per tooth and table feed rate: F = RPM × number of teeth × chip load per tooth. A 4-tooth end mill at 1,200 RPM with 0.004 inch/tooth chip load: F = 1,200 × 4 × 0.004 = 19.2 ipm. Always verify that the programmed F value is consistent with the intended chip load and the material being machined — a common error in hand-edited programs is leaving a roughing feed rate active during a finishing pass, over-feeding the finishing insert and producing poor surface finish or insert chipping (Smid, CNC Programming Handbook, 3rd ed., Industrial Press, 2008; Machinery's Handbook, 31st ed., Industrial Press, 2020).
What program structure elements appear at the beginning and end of every G-code program?
Every well-structured G-code program follows a consistent header-body-footer pattern that ensures safe, predictable machine behavior. The header — the first several blocks of the program — establishes the initial machine state: program number (O word, e.g., O1234); safety block (G40 G49 G80 — cancels cutter radius compensation, tool length offset, and any active canned cycles from a previous program); units declaration (G20 or G21); absolute vs. incremental mode declaration (G90 for absolute, the standard); work offset selection (G54 or the appropriate offset); plane selection (G17 for milling, G18 for turning); tool call (T word for turning, T and M06 for milling machining centers); spindle start (M03 Sxxx); coolant on (M08). The body contains the actual machining moves — approach, roughing, semi-finishing, finishing, retract — as described in the preceding sections. The footer — the final blocks — safely returns the machine to a known state: retract to a safe Z position; cancel cutter compensation (G40); cancel tool length offset (G49); return to reference position or tool change position (G28 or G30); spindle stop (M05); coolant off (M09); program end (M30 — on most controls, M30 also rewinds the program to the beginning so it can be run again without manual reset). The critical safety rules in the header: always cancel G40, G49, and G80 explicitly at the start of every program, regardless of what the previous program left active. A machining center that ended its previous program mid-cycle with G41 still active will carry that cutter compensation into the new program's rapid moves — with potentially catastrophic results. The discipline of always starting with an explicit cancel block is one of the most important habits in CNC shop-floor programming (Smid, CNC Programming Handbook, 3rd ed., Industrial Press, 2008).
- CNC Controller Types: Fanuc, Siemens, Mazatrol, and Other Families — the control systems that interpret G-code programs
- CAD/CAM Workflow for CNC Machining: From Model to Finished Part — how CAM systems generate G-code automatically from 3D models
- Work Offsets and Tool Compensation in CNC Programming — deeper coverage of offset management and compensation codes
- Roughing vs. Finishing Strategies in CNC Machining — how G-code canned cycles implement roughing and finishing strategies
References
- Smid, P. (2008). CNC Programming Handbook, 3rd ed. Industrial Press.
- Kief, H.B., Roschiwal, H.A., and Schwarz, K. (2020). The CNC Handbook. Industrial Press.
- Madison, J. (1996). CNC Machining Handbook. Industrial Press.
- Machinery's Handbook, 31st ed. Industrial Press, 2020.
- ISO 6983-1:2009: Automation Systems and Integration — Numerical Control of Machines — Part 1: Frame Structure for Data, Program Data Format, and Address Word Definitions. ISO.
Need Precision CNC Machining?
UTEC Industrial provides large-scale CNC machining services from our 25,000 sq ft facility in Spokane Valley, WA — equipped with Mazak, Monarch, and Mori Seiki machining centers, plus a gantry bandsaw cutting sections up to 50" × 84".