x
Send Your Inquiry Today



    G-Code vs M-Code: What Each CNC Command Controls

    G-code tells a CNC machine where and how to move. M-code tells it to carry out machine functions around that motion, such as starting the spindle, changing a tool, turning coolant on, or stopping the program. They work in the same program, but they do different jobs. Exact code meanings and formats can change with the controller, machine builder, and options installed on the machine.

    If you need the wider production picture first, read what CNC means in manufacturing. This article stays close to the code and the machine actions behind it.

    G-code and M-code at a glance

    Think of a CNC program as a sequence of instructions. G-codes mainly set a motion or machining mode. They tell the control whether the next move is rapid positioning, a cutting move, an arc, a drilling cycle, or a coordinate-system selection.

    M-codes handle machine-side actions. They can start or stop the spindle, call a tool change, switch coolant, pause the program, operate a clamp, or end the job. Many are standard enough to be familiar across shops. Others are machine-specific, especially codes tied to pallets, probes, air blast, doors, conveyors, or a custom fixture.

    The distinction sounds simple until someone copies a program from another machine. A code that is harmless or useful on one controller can be unsupported, differently assigned, or wired to an optional device on another. The setup sheet, postprocessor, and machine manual matter just as much as the code line itself.

    Program elementWhat it commonly controlsExample
    G-codeMotion mode, geometry, coordinate behavior, or canned cycleG01 X25.0 F300 is a linear feed move on many controls.
    M-codeA machine function or program eventM08 commonly turns coolant on; confirm it on the actual machine.
    Address wordA value used by a commandS6000 requests spindle speed; F300 sets a feed value in the active mode.
    CommentA note for the operator or programmer(CHECK CLAMP CLEARANCE) does not command motion.

    The movement side of a CNC program

    G-codes are usually the lines people mean when they say “the machine program.” They establish how the axes behave. On a mill, that often means X, Y, and Z movement. A turning program uses a different coordinate arrangement, but the same idea applies: the code defines the path and the mode for making it.

    The most familiar motion codes are rapid positioning, linear feed, and circular interpolation. Rapid moves get the tool into a safe starting location. Feed moves remove material. Arc moves create radii and circular features. A drilling cycle can compress a repeated drilling sequence into a few lines.

    Here are common examples from Haas-style milling documentation. They are included to explain the job of each command, not as a program to run on an unverified machine.

    Common codeTypical purpose on a Haas-style millWhy the buyer may care
    G00Rapid positioningAffects non-cutting travel and clearance planning.
    G01Straight cutting move at the active feed rateUsed for faces, profiles, slots, and many pocket passes.
    G02 / G03Clockwise / counterclockwise circular interpolationUsed for arcs, bores, and radiused toolpaths.
    G54Work coordinate offset selectionConnects the program to the part’s physical setup.
    G81Basic drilling cycleRepeats a controlled drilling sequence.

    The code alone does not prove that a part will be accurate. A G01 move may be perfectly programmed but still cut poorly if the tool is worn, the fixture allows vibration, or the material shifts after the clamp is released. For close-tolerance work, programming, tooling, machine condition, and inspection have to agree.

    G-code is also modal on many controls. Once a mode is selected, it may remain active until another command changes it. A programmer has to know what the machine is already carrying forward. This is why clean programs often repeat certain safety lines at a tool change or before a critical operation.

    What M-code handles around the cut

    M-codes handle the actions that let a toolpath actually run in the machine. On a mill, a typical sequence might call a tool, start the spindle in the correct direction, turn coolant on, cut the feature, retract, stop the spindle, and then move to the next tool.

    Examples such as M03 for clockwise spindle rotation, M05 for spindle stop, M06 for tool change, M08 for coolant on, M09 for coolant off, and M30 for program end are common on Haas milling controls. A lathe can use codes differently because it may need chuck, tailstock, turret, or parts-catcher functions.

    That difference matters when a buyer is reviewing a quote or transferring a proven part. “We already have the G-code” does not guarantee plug-and-play production elsewhere. Tool numbers, offsets, fixture zero points, coolant setup, alarms, safe positions, and M-code assignments all need to match the receiving machine.

    Custom M-codes are a good example. A shop may use one to trigger a fixture clamp, a robot, a bar feeder, a probe, or an air blast. Those instructions can be useful for repeat production, but they reflect the equipment in that shop. They are not portable manufacturing requirements to copy into a purchase order.

    One short program block, read line by line

    This simplified example shows the two code families working together. It is an explanation only. It does not include the offsets, safety checks, tools, speeds, workholding, or verification needed to run a real job.

    T01 M06             (select tool 1 and change tool)
    G54 G00 X0 Y0       (select work offset and rapidly position)
    S6000 M03           (request spindle speed and start clockwise)
    G43 H01 Z50.0       (apply tool-length compensation and move to a safe height)
    M08                 (coolant on)
    G01 Z-2.0 F150      (feed the tool into the material)
    G01 X40.0 F300      (make a straight cutting move)
    G00 Z50.0           (retract rapidly)
    M09                 (coolant off)
    M30                 (end and reset program)

    G54, G00, G43, and G01 are doing the geometry and motion work. M06, M03, M08, M09, and M30 control events on the machine. T, S, H, X, Y, Z, and F supply the tool selection, spindle speed, compensation offset, coordinates, and feed value needed by those commands.

    On a real job, the missing details are the important ones: Is the tool clear of the fixture? Is the correct work offset loaded? Is the spindle direction right for this cutter? Is the coolant safe for this material and process? Has the first article been measured? Code cannot answer those questions by itself.

    Why the controller, postprocessor, and machine still matter

    CAD describes the part. CAM creates the toolpaths. The postprocessor turns those toolpaths into code for a particular controller. A Fanuc-style, Haas, Siemens, Heidenhain, or LinuxCNC setup can all express machining intent, but the exact output is not necessarily interchangeable.

    The postprocessor has to know the machine’s axis layout, tool-change sequence, rotary limits, control language, and supported cycles. This becomes especially important on multi-axis equipment. A five-axis machine may have a tilting table, a swiveling spindle, or both. The safe approach and rotation limits are part of the post, not a detail the operator should guess on the floor.

    That is one reason why a machining supplier normally owns the production program. The customer provides the model, drawing, material, critical dimensions, and functional requirements. The shop chooses a proven route for its own machines. For a look at how axis access changes that route, see 3-axis vs. 5-axis CNC machining.

    Code choices show up in the quote

    Buyers do not need to program CNC machines to request a good part. It helps, though, to understand why the supplier may ask about a deep pocket, a small radius, an angled hole, a cosmetic face, or a free-state flatness requirement.

    Those drawing details affect the CAM strategy and, eventually, the program. A deep feature may force a long tool and slower cutting. A hole from an awkward angle may require another setup or a multi-axis route. A tight bore may need a finish pass and additional probing. None of that is “just code.” It is machine time, fixture time, tool wear, and inspection effort.

    For the full pricing picture, see CNC machining cost factors. It covers material, setups, tolerances, finishing, quantity, inspection, and the other items behind a realistic quote.

    When the order involves magnesium, copper, thin walls, or another material with special handling needs, tell the shop early. The safe spindle, coolant, chip-control, workholding, and cleanup plan depend on the material and part—not on the presence of one G-code or M-code.

    Practical questions about G-code and M-code

    Is G-code the same on every CNC machine?

    No. Many basic commands are widely recognized, but syntax, supported cycles, parameters, and machine behavior vary by controller and machine builder. Verify code against the manual for the actual control.

    Does M-code move the machine axes?

    M-codes are generally used for machine functions rather than cutting-path geometry. Their actions can affect when and how the machine moves, however; a tool change, clamp command, or pallet sequence is not something to treat casually.

    Can a customer send G-code to a CNC supplier?

    You can share it as useful background, especially for a proven process. The supplier should still review or regenerate the program for its own equipment, tooling, workholding, and safety procedures.

    Why do two shops post different code for the same part?

    They may use different machines, work offsets, fixtures, cutters, coordinate strategies, and safety conventions. Different code can make the same conforming part.

    Do G-code and M-code affect CNC machining cost?

    Indirectly, yes. The program reflects the number of setups, tool changes, toolpaths, probing steps, and machine functions required. The real cost is the manufacturing work those instructions represent.

    Scroll to Top