CNC Programming

G coding CNC basics for machinists and programmers

What g coding CNC means in practice

G coding CNC is the language layer that tells a computer numerical control machine where to move, how fast to feed, which coordinate system to use, and which support functions to activate. On the shop floor, the skill is not simply memorizing hundreds of codes. It is understanding motion, modal behavior, offsets, units, and how a specific control interprets the program. A clean CNC program should make the machine state clear before cutting starts, keep rapid moves away from clamps and stock, and match the control, post processor, tooling, and setup actually in use.

The explanations below align with public descriptions from ISO 6983-1:2009, NIST RS274/NGC interpreter documentation, LinuxCNC language references, Haas operator documentation, Autodesk post processor guidance, and Grbl project documentation. These references are useful for learning the structure of the language. In production, however, the controlling sources are the machine builder’s manual, the control documentation, and the approved post processor for that machine.

typography, circuit, fonts, character, g, letters, characters, design, typography, typography, typography, typography, typography

For more manufacturing programming topics, see the CNC programming section on MechMeld.

How a G-code block is built

A CNC program is usually written as a sequence of lines, often called blocks. Each block contains one or more words. A word is normally a letter followed by a number or value. In the block G01 X25.0 F200.0, for example, the G word selects a linear feed move, the X word gives the target coordinate, and the F word sets the feed rate. Decimal format, allowed characters, line numbering style, and optional features can vary by control.

Most CNC programs combine motion commands, setup commands, and auxiliary functions. G codes generally prepare or command motion and machining modes. M codes commonly control machine functions such as spindle start, coolant, program stop, or tool change, although M-code behavior can be highly machine-specific. Other letters define axes, feed, spindle speed, tool numbers, offsets, arc centers, dwell times, and cycle values.

Word Common role Programming note
G Preparatory or motion command Often modal, meaning it may stay active until changed.
X, Y, Z Linear axis positions Meaning depends on the active coordinate system and distance mode.
I, J, K Arc center offsets or cycle values Arc format must match the control’s requirements.
F Feed rate Check whether feed is per minute, per revolution, or inverse time.
S Spindle speed command May interact with gear range, constant surface speed, or spindle limits.
T Tool selection Tool-change format differs across mills, lathes, routers, and controls.
M Miscellaneous machine function Coolant, spindle, and tool-change M codes should be verified on the actual machine.

This simple structure can carry a lot of information. One line may change the motion mode, coordinate system, tool compensation, and feed condition. That flexibility is useful, but it also creates risk when a program starts with assumptions left over from a previous operation or control state.

Modal behavior is the key concept

Many G codes are modal. Once called, they remain active until another code in the same modal group replaces them or the control resets them. This is why professional programs often include a safe-start block near the beginning. Its purpose is not decoration; it puts the control into a known state before the machine approaches the part.

Common modal choices include absolute or incremental positioning, inch or metric units, plane selection, feed mode, work coordinate system, cutter compensation, tool length compensation, and canned cycle status. If the program assumes absolute positioning but the control is still in incremental mode, a move that looks ordinary on screen can become dangerous at the machine. If a canned drilling cycle remains active when the programmer expects simple positioning, the next coordinate block may trigger unwanted motion.

Modal groups also explain why repeated G codes are often omitted after the first line. If G01 is active, later coordinate blocks may continue to feed in straight lines without repeating G01. That can make hand-written programs shorter, but it can also make them harder to review. For production work, readability often matters as much as compactness because setup people, operators, and programmers all need to confirm the same intent.

Common G codes and what they usually mean

The following table summarizes common milling and routing codes that appear in many CNC programs. These descriptions reflect widely used conventions, not a guarantee that every machine interprets them identically. Turning centers, mill-turn machines, Swiss-type lathes, routers, laser cutters, and hobby controllers may support different subsets or require different syntax.

Code Typical purpose What to verify
G00 Rapid positioning Clearance, axis order behavior, and maximum rapid strategy.
G01 Linear interpolation at feed Correct feed mode, feed value, tool engagement, and units.
G02 / G03 Clockwise / counterclockwise circular interpolation Plane selection, R versus IJK arc format, and controller tolerance.
G17 / G18 / G19 XY, XZ, or YZ plane selection Arc plane, cutter compensation plane, and cycle behavior.
G20 / G21 Inch or metric units Program units, setup sheet units, tool library units, and machine defaults.
G40 Cancel cutter compensation Whether compensation is intentionally off before positioning or tool change.
G43 / G49 Apply or cancel tool length compensation Correct H offset, tool table value, and safe Z approach.
G54 to G59 Work coordinate systems Correct part zero, fixture offset, tombstone face, or vise station.
G80 Cancel canned cycle That no drilling, tapping, or boring cycle remains active.
G90 / G91 Absolute or incremental positioning Whether each coordinate is from work zero or from the current position.

Several of these codes are not cutting commands by themselves. G54, for example, does not move the tool; it selects a work coordinate system. G20 and G21 define units; they do not safely scale a program after posting. G17 chooses a plane and affects how arcs and certain compensation moves are interpreted. This distinction helps programmers review code by machine state instead of treating every line as an isolated instruction.

A short sample program and how to read it

The simplified example below is for explanation only. It should not be run on a real machine without adapting it to the control, post, tool, fixture, work offset, spindle, coolant, and material. The purpose is to show how state-setting lines, motion lines, and end-of-program lines work together.

Line Example block Meaning to review
1 N10 G90 G54 G21 G17 G40 G49 G80 Use absolute positioning, work offset G54, metric units, XY plane, cancel compensation and cycles.
2 N20 T1 M06 Select tool 1 and perform a tool change if the machine uses this format.
3 N30 S6000 M03 Set spindle speed and start spindle clockwise where supported.
4 N40 G00 X10.0 Y10.0 Rapid to an XY approach position.
5 N50 G43 H01 Z50.0 Apply tool length offset H01 and move to a safe Z height.
6 N60 G01 Z-2.0 F250.0 Feed down to cutting depth at the programmed feed rate.
7 N70 G01 X50.0 F400.0 Cut in a straight line to X50.0 with a new feed rate.
8 N80 G00 Z50.0 Retract rapidly to a safe height.
9 N90 M05 Stop the spindle where this M code is assigned to spindle stop.
10 N100 M30 End and rewind the program on many controls.

The main lesson is that a program is a controlled sequence of states. The safe-start line establishes assumptions. The tool-change and spindle lines prepare the machine. The rapid approach is separated from feed moves. Tool length compensation is called before cutting depth is reached. The end sequence retracts and stops the spindle before the program ends.

Different controls may require a different safe line, tool-change format, spindle command, or compensation approach. Some shops standardize safe-start blocks across machines; others rely on the post processor to output machine-specific sequences. Either method can work if it is documented, verified, and used consistently.

Hand coding, CAM output, and post processors

Modern CNC programming often combines CAM-generated toolpaths with G-code knowledge. CAM software calculates tool motion from part geometry, tools, cutting parameters, and machining strategies. The post processor then converts that toolpath data into the machine-specific output expected by a particular control. Autodesk’s public Fusion documentation describes the post processor as the component that creates code in a language the CNC machine can understand, and its support material emphasizes selecting the correct machine configuration when posting code. See also: CNC Machining.

This is why two machines can cut the same CAD model but require different posted programs. A vertical machining center, a three-axis router running Grbl-derived firmware, and a mill-turn machine with live tooling may all be described as CNC equipment, but they do not share the same complete command set. Some controls support canned cycles, probing macros, high-speed look-ahead options, rigid tapping, polar interpolation, or advanced work offsets. Others require the post to expand complex operations into simpler moves.

Hand coding remains valuable because it helps programmers understand the output. Programmers who understand G00, G01, G02, G03, modal state, work offsets, and compensation are better prepared to review posted code, diagnose alarms, and communicate with operators. At the same time, manually editing posted code without revision control can create hidden risk. If a shop edits code at the machine, the edited file should be saved, identified, and fed back into the programming process when appropriate.

Practical checks before running a CNC program

Program review should focus on failure modes, not only syntax. A program can be syntactically valid and still be unsafe for the setup. The following checks are practical for mills and routers, with adjustments for lathes and other machines.

  • Confirm the controller and post. Use the approved post processor for the exact machine or control family, not a similar-looking default.
  • Check units. Make sure G20 or G21 matches the setup sheet, tool library, stock model, and inspection plan.
  • Verify work offsets. Confirm G54, G55, or another offset points to the intended part zero, fixture station, or tombstone face.
  • Verify tool length and cutter compensation. Match T numbers, H offsets, D offsets, tool stickout, and wear values to the actual setup.
  • Review first rapid moves. Look for safe Z height, clamp clearance, rotary position, and whether XY motion happens before or after Z motion.
  • Check modal cancellations. Look for G40, G49, and G80 where appropriate, and verify distance mode and feed mode before cutting.
  • Simulate and backplot. Use CAM simulation and, where available, controller simulation or independent verification to catch collisions and overtravel.
  • Use cautious prove-out. Single block, optional stop, feed override, rapid override, dry run, or cut above the part according to shop procedure.

These checks are not a substitute for training or supervision. They are a way to make the review systematic. The most costly G-code mistakes often come from ordinary assumptions: the wrong work offset, inch code posted for a metric setup, a missing tool length offset, an unexpected modal cycle, or a post selected for a different machine.

What standards explain and what they do not

ISO 6983-1:2009 publicly describes a program format and address-word definitions intended to improve uniformity of numerical control programming. NIST’s RS274/NGC interpreter documentation and LinuxCNC references are widely used learning resources because they explain blocks, words, modal groups, and interpreter behavior in a concrete way. These sources help readers understand the grammar and logic behind G coding CNC programs.

However, standards and open references do not remove the need for machine-specific documentation. Builders and control vendors add options, cycles, macros, alarms, parameter settings, and safety features. Even familiar commands can carry different restrictions. For example, arc programming may use radius values or IJK center offsets depending on the post and control settings. Canned drilling cycles may differ in retract behavior. M codes can be assigned to custom devices such as mist coolant, vacuum, clamps, probes, bar feeders, or chip conveyors.

The practical rule is simple: learn the common language, then verify the dialect. Treat ISO-style G code as the foundation, the control manual as the authority, and the post processor as the translator that must be maintained.

Frequently asked questions

Is G code the same on every CNC machine?

No. Many controls share common commands such as G00, G01, G02, G03, G17, G20, G21, G54, G90, and G91, but support, syntax, defaults, and options vary. A program should be verified against the machine manual and the approved post processor before it is run.

Do machinists still need to learn G code if they use CAM?

Yes. CAM reduces manual calculation and can generate efficient toolpaths, but programmers and operators still need to understand posted output. G-code knowledge helps with prove-out, alarm diagnosis, setup verification, small edits, and communication between programming and the shop floor.

What is the difference between G codes and M codes?

G codes usually define motion, preparation, or machining modes. M codes usually control auxiliary machine functions such as spindle, coolant, stops, and tool changes. M codes are especially machine-dependent, so their meaning should be checked on the specific control.

Why are modal G codes important?

Modal codes stay active until changed or canceled. This saves programming effort but can create risk when a program begins with an unexpected machine state. Safe-start lines and clear program structure help prevent hidden modal assumptions.

Can posted G code be edited by hand?

It can be, but edits should be controlled. A small manual change may solve a setup issue, but it can also separate the shop-floor file from the CAM source. Shops should document edits, save the proven file, and update the programming record when necessary.