โ† Open Viewer
โ† All Articles What is G-Code Command Anatomy Key Codes Reading Files

What is G-Code? The Complete Beginner's Guide

If you've ever watched a CNC machine carve a perfect pocket into aluminum, or seen a 3D printer lay down a flawless first layer, you've witnessed G-Code in action. G-Code is the universal language that bridges the gap between a digital design and a physical machine. It is arguably the most important programming language that most engineers have never formally studied โ€” yet it quietly runs millions of machines around the world every single day.

This guide is written for complete beginners. By the end, you'll understand what G-Code is, how it works, how to read a real file, and how tools like GCodex let you visualize and verify your code before running it on a real machine.

A Brief History of G-Code

G-Code (formally known as RS-274) was developed in the 1950s and 1960s at MIT under a U.S. Air Force contract. The goal was to automate complex metal-cutting operations on numerically controlled (NC) machines. Early NC machines used punched paper tape to feed instructions โ€” and the G-Code format was designed to be simple enough to encode on that tape, yet expressive enough to describe any machining operation.

Over the following decades, the language was standardized by organizations like the Electronic Industries Alliance (EIA) and later ISO. Today, while many dialects and extensions exist โ€” Fanuc, Haas, Siemens, Marlin, Klipper โ€” the core vocabulary of G-Code has remained remarkably stable for over 60 years.

Anatomy of a G-Code Command

Every G-Code instruction follows a consistent, readable structure. Each command lives on its own line and consists of one or more words, where each word is a letter (called the address) followed by a number (called the parameter).

G1 X50.0 Y30.0 Z-2.5 F800

Breaking this down:

Multiple parameters on a single line are processed simultaneously โ€” meaning the machine moves all three axes at once, arriving at the target position together. That's what creates smooth diagonal cuts rather than stair-stepped motion.

Tip: G-Code is not case-sensitive for addresses (G1 = g1), but by convention all addresses are written in uppercase. Numbers after decimal points are optional for whole values โ€” X50 and X50.0 are identical.

Coordinate Systems

G-Code operates in a three-dimensional Cartesian coordinate space โ€” the same X, Y, Z system you learned in high school geometry. The origin (0, 0, 0) can be defined by the programmer using work offsets (like G54 through G59 on CNC mills, or G92 on 3D printers).

Absolute vs Incremental Positioning

This is one of the most important concepts in G-Code:

Most programs default to G90 (absolute) and switch to G91 only for specific operations like drilling cycles or relative homing moves. Confusing the two is a very common source of crashes โ€” more on that in our Troubleshooting Guide.

Key G-Codes You Must Know

Key M-Codes: Machine Control

While G-codes govern motion, M-codes control auxiliary machine functions โ€” spindle, coolant, program flow, temperature (for 3D printers), and more.

G-Code in 3D Printing

3D printers are essentially CNC machines that extrude molten plastic (or other materials) instead of cutting. Their G-Code is generated by slicing software โ€” Cura, PrusaSlicer, Bambu Studio โ€” which converts a 3D model (STL or 3MF file) into thousands of G-Code lines describing every movement, every temperature change, and every extrusion amount.

A 3D printing G-Code file typically starts with a header block that sets temperatures, homes the axes, levels the bed, and primes the nozzle. Then the main body describes the print layer by layer โ€” each layer is a series of travel moves (G0) and extrusion moves (G1 with an E parameter for extruder position). A typical FDM print of a moderate-sized object might contain 100,000 to several million lines of G-Code.

M109 S210        ; Wait for hotend to reach 210ยฐC
M190 S60         ; Wait for bed to reach 60ยฐC
G28              ; Home all axes
G29              ; Auto bed leveling
G1 Z0.2 F300     ; Move to first layer height
G1 X10 Y10 E0 F1500 ; Start position
G1 X50 Y10 E5.2 F2000 ; Extrude first line

G-Code in CNC Milling

In CNC milling, G-Code is generated by CAM (Computer-Aided Manufacturing) software such as Fusion 360, Mastercam, or Solidworks CAM. The programmer defines toolpaths โ€” roughing passes, finishing passes, drilling cycles, contours โ€” and the CAM software posts out G-Code optimized for the specific machine controller (Fanuc, Haas, Siemens, etc.).

CNC G-Code tends to be more complex than 3D printing G-Code because it must account for tool diameter offsets, cutter compensation, work coordinate systems, and multi-axis simultaneous motion. A 5-axis machining program for an aerospace part might include complex rotational moves using A, B, and C axis commands alongside the standard X, Y, Z.

How to Read a G-Code File

G-Code files are plain text โ€” you can open any .gcode, .nc, .tap, or .cnc file in a basic text editor. Each line is one instruction. Lines beginning with a semicolon (;) are comments and are ignored by the machine. Parentheses (like this) are also used for comments on some controllers.

When reading a file manually, start at the top and follow the flow: initialization, warm-up (for printers), home, then the main cutting/printing loop. Look for coordinate changes to understand where the machine is travelling, and watch the Z axis to understand depth progression.

Visualizing G-Code with GCodex

Reading G-Code as text is one thing โ€” but seeing the toolpath is infinitely more useful. GCodex renders your G-Code file as an interactive 3D/2D visualization, right in your browser. You can rotate, zoom, inspect individual layers, and check for over-travels or unexpected moves โ€” all without sending a single command to a real machine.

This is especially valuable when you receive a G-Code file from a CAM system you're unfamiliar with, or when debugging a sliced file that's printing incorrectly. Drop your file into GCodex, scan the toolpath visually, and catch problems before they damage your machine or waste material.