System upgrade reminder, Homepage update.

What is G-Code in CNC Machining?

g-code
Table of Contents

Every part produced on a CNC machine begins with a series of precise instructions called G-code. This language tells the machine exactly how to move, cut, and shape material into the desired form.

The Language Behind CNC

When you look at a finished part produced on a CNC machine, it's easy to admire the craftsmanship. But behind every cut, every curve, and every drilled hole, there is a language telling the machine exactly what to do. That language is G-code.

Think of G-code like a musical score: it doesn't make music itself, but it tells the pianist which keys to press, how long to hold them, and at what tempo. Similarly, G-code gives CNC machines the detailed "sheet music" they need to transform raw material into precise parts.

Though developed in the 1950s, G-code remains the universal standard in CNC machining today. Despite variations across brands like Haas, Fanuc, or Siemens, the fundamentals are shared industry-wide.

cnc-g-codes

What Is G-Code?

At its core, G-code is a programming language for CNC (Computer Numerical Control) machines. It describes tool movements, speeds, positioning, and cutting operations in a way the machine can understand. Two categories of codes are most important:

  • G-codes: “Geometry codes” that control motion and positioning.
  • M-codes: "Machine codes" that manage machine functions such as spindle direction, coolant on/off, or tool changes.

A typical CNC program is a sequence of blocks (lines of code), each containing instructions. For example:

G01 X50 Y20 F200 T01 M03 S1500

This single line tells the machine:

  • Move in a straight line (G01)
  • To coordinates X=50, Y=20
  • At feed rate F200
  • Using Tool #1 (T01)
  • Start spindle clockwise (M03)
  • Set spindle speed to 1500 rpm (S1500)

Another key feature is modal commands: once a G-code is set, it stays active until another code replaces it. For example, after G01 is activated, the machine remains in "linear cut" mode until told otherwise. We will break down an example to make it easier to read later on.

The History of G-Code

G-code dates back to the earliest days of numerical control (NC) machines in the 1950s, when the U.S. Air Force sponsored research into automated part production. Before G-code, machining instructions were punched into paper tapes as sequences of numbers, a primitive but revolutionary method for its time. 

The formalized G-code language (RS-274) emerged soon after as a standardized way to tell machines how to move, cut, and shape parts. In other words, G-code was created as a "common language" for machine tools, ensuring that different machines could interpret commands consistently. This standardization is what allowed CNC machining to evolve rapidly into the global custom manufacturing backbone it is today.

From RS-274 to Modern Variants

While the original RS-274 remains the foundation, G-code has branched into numerous dialects. Every major CNC machine manufacturer – from Haas and Fanuc to Siemens and Heidenhain – implements the standard slightly differently. 

For example, a Fanuc controller might use certain canned cycles with subtle syntax differences compared to a Haas machine, even though both are "speaking" G-code. This creates what machinists sometimes call "flavors" of G-code. Despite these variations, the structure is recognizable: commands always begin with a letter (like G, M, X, Y, Z, F, S) followed by a number or value. 

Over time, these variants have added convenience features, making G-code more powerful without straying too far from its original RS-274 logic.

Conversational Programming and CAM Software

Although G-code remains the backbone of CNC machining, modern shops don't always program parts line by line anymore. Many machinists use conversational programming, where the operator inputs high-level instructions directly into the machine (e.g., "drill a hole of this diameter here"), and the CNC control automatically generates the underlying G-code. 

Similarly, computer-aided manufacturing (CAM) software such as Fusion 360 or Mastercam has become widespread. CAM software allows engineers to design toolpaths visually and then export them as G-code, ready to be read by a CNC machine. 

Even in these cases, the final step is still G-code — it remains the "machine language" that every CNC tool must understand, regardless of how user-friendly the interface looks on the surface.

Why G-Code Still Matters Today?

Despite decades of evolution and alternatives, G-code continues to be indispensable. 

  • It is a transparent language: machinists can read and debug it line by line, which is critical when troubleshooting errors or fine-tuning precision operations. 
  • It is universal: virtually every CNC machine can interpret G-code, even if the syntax varies slightly between brands. 
  • Understanding G-code gives machinists a direct grasp of how the machine interprets motion, feeds, and speeds.

In an era where automation and CAD/CAM are prevalent, the ability to "speak" G-code ensures manufacturers can still optimize performance, prevent costly mistakes, and push CNC technology to its limits. G code is important for:

  • Precision: Micron-level accuracy for aerospace, medical, and automotive components.
  • Efficiency: Well-optimized programs reduce tool wear and cycle time.
  • Flexibility: From CNC milling and CNC turning to laser cutting and even 3D printing, G-code is adaptable across processes.
  • Compatibility: Despite being over 60 years old, it remains the common standard worldwide.
Get Instant Quote From Xmake

Get an instant quote from Xmake by uploading your design file today, and enjoy fast, reliable delivery with our expert manufacturing services.

Start a Quote

Practical Tips to Learn G-Code

For newcomers or professionals looking to sharpen their skills, here are practical steps:

  1. Start with core codes – G00, G01, G02, G03.
  2. Use simulation software (like Fusion 360, Mach3, or NC Viewer) to test code safely before machining.
  3. Read your machine's manual – different controllers (Fanuc, Haas, Siemens) have their own dialects.
  4. Gradually explore advanced features like canned cycles, offsets, and macros.

Common G-Codes Explained

The-Complete-Guide-of-CNC-G-Code

At first glance, G-code can look intimidating, a wall of letters and numbers. In reality, most machining relies on a handful of essential commands.

G-CodeMeaningCommon Use CaseExample CommandWhat Happens
G00Rapid positioningQuickly move between cutting locationsG00 X0 Y0Tool moves at max speed to (0,0) without cutting.
G01Linear interpolationStraight-line cutting at a set speedG01 X50 Y25 F150Tool cuts a straight path to (50,25) at 150 mm/min.
G02Circular interpolation (CW)Cutting clockwise arcs or circlesG02 X30 Y10 R5Tool follows a clockwise arc with 5 mm radius.
G03Circular interpolation (CCW)Cutting counterclockwise arcsG03 X30 Y10 R5Tool follows a counterclockwise arc.
G17 / G18 / G19Plane selectionDefines XY, XZ, or YZ working planesG17Circular moves now apply in XY plane.
G20 / G21Unit selectionProgram in inches (G20) or millimeters (G21)G21Switches units to millimeters.
G54–G59Work coordinate systemsSet part origins or multiple fixturesG54Activates first work offset, useful for multiple parts.

How to Read G-Code: A Step-by-Step Guide

Letters like X, Y, Z define axis positions, F sets feed rate, S controls spindle speed, and T selects the tool. Each block of code tells the machine where to move, how fast to move, and what tool behavior to follow.

Step 1: Recognize the structure of G-code

Each line of G-code is called a block. A block usually begins with a letter (like G, M, X, Y, Z, F, or S) followed by a number. Example:

G01 X10 Y20 F150

This block has:
  • G01 – type of motion (cutting move)
  • X10 Y20 – coordinates of where the tool will move
  • F150 – feedrate (speed of tool movement)
Think of each block as a short sentence telling the CNC machine what to do next.

Step 2: Spot the "G" command first

The G-code word is the heart of the block — it sets the motion type. Common ones:
  • G00 – rapid positioning (fast move, no cutting)
  • G01 – linear interpolation (cutting move in a straight line)
  • G02 – clockwise arc
  • G03 – counter-clockwise arc
Example:

G01 X50 Y25

  • Translation: Move in a straight line to position X=50, Y=25.

Step 3: Read the coordinates (X, Y, Z)

These tell the machine where the tool should go. X and Y control horizontal movement. Z controls vertical movement (depth).

Example:

G01 X100 Y0 Z-5

  •  Translation: Cut a line to (100,0), while lowering the tool to 5 mm below the surface.

Tip: Negative Z values mean the tool is cutting into the material.

Step 4: Notice the feedrate and spindle commands

  • F = feedrate, how fast the tool moves through the material (mm/min or in/min).
  • S = spindle speed, how fast the tool rotates (RPM).

Example:

G01 X50 Y25 F200 S1200

  • Translation: Cut straight to (50,25) at a speed of 200 mm/min, with the spindle rotating at 1200 RPM.

Step 5: Look for setup commands at the beginning

Programs usually start with preparation code.

Common examples:

  • G20 – use inches
  • G21 – use millimeters
  • G17 – select XY plane
  • G90 – absolute positioning (coordinates based on origin)

Example:

G21 G90

  • Translation: Use millimeters, and measure all positions from the fixed origin point.

Step 6: Identify the end of the program

CNC programs end with special commands:

  • M30 – stop program and rewind
  • M02 – end of program (no rewind)

Example:

M30

  • Translation: Program complete, reset the machine to the beginning.

Step 7: Put it all together

G21        (Work in millimeters)

G17        (XY plane selected)

G90        (Absolute positioning)

G00 X0 Y0  (Rapid move to origin)

G01 X50 F200  (Cut straight to X=50 at feedrate 200)

G01 Y50       (Then cut upward to Y=50)

G00 Z10       (Retract tool to safe height)

M30           (End of program)

Translation:

  1. Use millimeters, XY plane, absolute coordinates.
  2. Start at the origin (0,0).
  3. Cut a line 50 mm to the right.
  4. Then cut a line 50 mm upward.
  5. Lift the tool 10 mm above the part.
  6. Stop the program.

This creates a simple "L" shaped toolpath.

Even without prior coding experience, the logic is readable: move to start, plunge, cut four edges, retract.

Advanced G-Code Features

While basic G- and M-codes handle most machining, advanced functions provide more efficiency and flexibility:

  • Canned Cycles (e.g., drilling, tapping)
    Commands like G81 or G84 automate repetitive tasks. Instead of writing multiple lines for a drilling sequence, a canned cycle executes the operation with just one line of code.
  • Subroutines & Macros
    Allow for loops, conditional statements, and parameterized machining. This is especially useful for parts with repeating features.
  • Comments & Notes
    Operators can include remarks (in parentheses or after semicolons) to make code easier to read.

For example:

G01 X30 Y10 F100 (Cut side wall)

Complete CNC G-Code List

G-code-command-overview

While most machinists rely on a small set of codes daily, there are dozens of G-codes that control specific functions. Below is a more comprehensive list. (Note: Availability may vary depending on the CNC controller brand, e.g., Fanuc, Haas, Siemens.)

G-CodeFunctionNotes / Use Case
G00Rapid positioningFast, non-cutting moves
G01Linear interpolationControlled cutting move
G02Circular interpolation, CWClockwise arcs
G03Circular interpolation, CCWCounterclockwise arcs
G04DwellPause for a set time
G05High-precision cycleSometimes "HPCC" for smoother paths
G06Parabolic interpolationRare, used for complex curves
G07Cylindrical interpolationFor wrap-around cutting on cylinders
G08Acceleration controlMachine-specific
G09Exact stop checkForces stop at end of block
G10Programmable offsetsSet work offsets or tool data
G11Cancel offset valuesClears G10 settings
G12Circular pocket milling, CWMill a pocket clockwise
G13Circular pocket milling, CCWMill a pocket counterclockwise
G14–G15Reserved / Spindle orientationDepends on controller
G16Polar coordinate interpolationUseful for lathe/live tooling
G17XY plane selectionDefault plane for arcs
G18XZ plane selectionFor turning and vertical arcs
G19YZ plane selectionFor special cases in milling
G20Programming in inchesImperial units
G21Programming in millimetersMetric units
G22Stored stroke limit ONDefine axis travel limits
G23Stored stroke limit OFFCancel limits
G24Reserved / not standardController-specific
G25Spindle speed clampSafety limit on RPM
G26Thread cutting cycle (lathe)Used in turning operations
G27Machine zero return checkConfirm machine reference point
G28Return to machine homeSends axes to home position
G29Return from homeReturn via intermediate point
G30Return to secondary homeAlternative machine zero
G31Skip function (probing)Used in probing cycles
G32Single-thread cuttingThreading one pass
G33Spindle synchronized motionFor threading cycles
G34Variable pitch threadingRare use case
G35Fixed pitch threadingFor constant thread pitch
G36–G37Automatic acceleration / decelerationController-specific
G38.xProbing cyclesCommon in 3D probing, e.g. G38.2
G39Circular interpolation, end at angleSingle-arc cut
G40Cancel cutter radius comp.Reset compensation
G41Cutter radius comp. leftOffset tool left of path
G42Cutter radius comp. rightOffset tool right of path
G43Tool length compensation (+)Apply tool length offset
G44Tool length compensation (–)Less common
G45Tool offset increaseAdjust offsets dynamically
G46Tool offset decreaseFine adjustments
G47Tool offset double increase 
G48Tool offset double decrease 
G49Cancel tool length compensation 
G50Spindle speed clamp (lathe)Limits RPM on lathes
G51Scaling function ONScale programmed moves
G52Temporary work offsetLocal coordinate shift
G53Machine coordinate systemOverrides work offset
G54–G59Work coordinate systemsDefine multiple part setups
G60Single direction positioningImproves accuracy
G61Exact stop check modeStops at each block end
G62Automatic corneringRounds sharp corners
G63Tapping cycleRigid tapping
G64Cancel exact stop checkDefault cutting mode
G65Simple macro callRun custom macros
G66Modal macro callMacro stays active
G67Cancel modal macro 
G68Coordinate rotationRotate program plane
G69Cancel rotationReset plane rotation
G70Finishing cycleLathe finishing
G71Stock removal cycle (turning)Roughing cycle
G72Finishing cycle (turning)Finishing pass
G73High-speed drilling cyclePeck drilling
G74Peck drilling / tapping (lathe)Reverse spindle
G75Peck grooving cycleLathe grooving
G76Thread cutting cycleAutomated threading
G77Reserved 
G78Boring cycle (lathe) 
G79Canned cycle, specialController-specific
G80Cancel canned cycleResets to normal mode
G81Drilling cycleSimple drill
G82Drilling cycle with dwellDrill and pause
G83Peck drilling cycleDeep hole drilling
G84Tapping cycle 
G85Boring cycle, feed in/out 
G86Boring cycle, feed in/spindle stop/rapid out 
G87Back boring cycle 
G88Boring cycle, dwell, spindle stop, manual retract 
G89Boring cycle, feed in/out with dwell 
G90Absolute programmingUses absolute coordinates
G91Incremental programmingUses relative moves
G92Position register / spindle clampMulti-purpose
G93Inverse time feed rateUsed in 5-axis
G94Feed per minuteStandard milling feed
G95Feed per revolutionStandard lathe feed
G96Constant surface speed (CSS)Lathe mode
G97Cancel CSS (fixed RPM) 
G98Return to initial planeDrilling cycles
G99Return to R planeDrilling cycles

(This table intentionally broadens beyond just "common codes" to act as a reference hub — not every shop will use them all.)

Conclusion

Despite its age, G-code continues to power the manufacturing world. It's the invisible hand guiding every precise movement in CNC machining. While newer standards like STEP-NC are being explored for richer, data-driven manufacturing, G-code remains the common ground for machinists everywhere.

In essence, G-code is not just a set of commands. It's the bridge between digital design and physical reality, enabling ideas on a screen to become tangible, functional parts.

For fast, expert CNC machining and instant online quotes, check out Xmake – one of the leading online CNC machining companies.

Tags: