Home / CNC Machining / What Is G-Code? CNC Commands, Programming, and Examples

What Is G-Code? CNC Commands, Programming, and Examples

what-is-g-code-cnc-machining

Table of Contents

Introduction

Every CNC machine needs a precise set of instructions before it can position a tool, start the spindle, or remove material from a workpiece. These instructions are commonly written in G-code, the numerical control language used to turn a digital toolpath into physical machine movements.

This guide explains what G-code is, what G code programming involves, how common commands are structured, and how milling and CNC lathe programs are checked before production.

What Is G-Code?

G-code is a numerical control programming language that tells a CNC machine how and where to move.

A CNC G code program can control movement along the X, Y, and Z axes. Advanced machines may also use the A, B, and C rotary axes to machine complex features from multiple angles.

G-code does more than define tool positions. It can also control:

  • Feed rate
  • Coordinate modes
  • Work offsets
  • Tool compensation
  • Drilling and tapping cycles
  • Linear and circular movements

A complete CNC program normally includes G codes together with other instructions. For example, M codes control auxiliary machine functions, T identifies a tool, S sets spindle speed, and F defines feed rate.

G-code developed from early numerical control systems created for automated machine tools. As CNC technology advanced, it became the standard language used to convert digital machining instructions into repeatable machine movements.

People searching for “what is a gcode” are usually asking about either an individual command or the complete program used to machine a part.

Why Is G-Code Important in CNC Machining?

G-code gives CNC machines the precise instructions needed to manufacture parts accurately and consistently.

Without a clear program, the machine would not know where to move, how fast to cut, or when to change direction.

CNC G code supports four key manufacturing goals:

  • Precision: It controls tool position, cutting depth, and movement path.
  • Repeatability: The same program can produce multiple parts with consistent dimensions.
  • Automation: Machines can complete complex operations with less manual control.
  • Complexity: G-code can manage arcs, drilling cycles, tool offsets, and multi-axis movements.

CAM software converts a CAD model into a toolpath, and the CNC program turns that toolpath into instructions the machine controller can execute.

How Does G-Code Programming Work?

G-code programming is the process of creating instructions that a CNC machine can read and execute.

Simple programs may be written manually. More complex parts are usually programmed with CAD and CAM software.

CAD-to-CNC workflow showing CAM toolpath creation, post-processing, and G-code generation

From CAD Model to CNC G Code

The process normally begins with a 3D CAD model or a 2D engineering drawing.

A programmer imports the design into CAM software and defines:

  • The CNC machine
  • Raw material dimensions
  • Cutting tools
  • Workholding method
  • Cutting depths
  • Feed rates
  • Spindle speeds

The CAM software then calculates the toolpath required to machine the part.

A post-processor converts the calculated toolpath into CNC G code that matches the selected machine and controller.

The typical workflow is:

CAD model → CAM setup → Toolpath → Post-processing → G-code program

How the CNC Controller Reads the Program

The CNC controller reads the program one block at a time.

Each block may tell the machine to:

  • Move to a coordinate
  • Follow a straight or circular path
  • Set a feed rate
  • Start or stop the spindle
  • Apply a tool offset
  • Run a drilling cycle

The controller sends these instructions to the machine axes, spindle, and supporting systems in the programmed sequence.

How Is a G-Code Block Structured?

A CNC G code program is divided into individual lines called blocks.

Each block contains one or more instructions that tell the machine what action to perform.

For example:

N10 G01 X20.0 Y10.0 Z-1.0 F200 S3000

This block can be read as follows:

  • N10 identifies the program line.
  • G01 commands a controlled linear movement.
  • X20.0 sets the X-axis destination.
  • Y10.0 sets the Y-axis destination.
  • Z-1.0 sets the Z-axis depth.
  • F200 defines the feed rate.
  • S3000 sets the spindle speed.

The CNC controller reads these values together and converts them into machine movement.

Common Address Letters in CNC G Code

A G-code block may include several address letters:

AddressMeaning
GMovement or machining mode
MAuxiliary machine function
X, Y, ZLinear axis positions
A, B, CRotary axis positions
FFeed rate
SSpindle speed
TTool number
HTool length offset
DCutter compensation offset

Not every block contains all of these values.

The exact order and supported addresses can vary by machine controller, but the basic block structure remains similar.

Common G Codes List for CNC Machines

These commands control tool movement, coordinate settings, compensation, and machining cycles.

Motion G Codes

Motion commands define how the cutting tool moves between programmed coordinates.

CodeFunction
G00Rapid positioning
G01Linear interpolation
G02Clockwise circular interpolation
G03Counterclockwise circular interpolation
G04Dwell or programmed pause

G00 is normally used to move the tool quickly through non-cutting space.

G01, G02, and G03 control cutting movements at a programmed feed rate.

Plane and Unit Commands

These G codes define the active machining plane and measurement system.

CodeFunction
G17Select the XY plane
G18Select the XZ plane
G19Select the YZ plane
G20Use inch units
G21Use millimeter units

The active plane affects circular interpolation, drilling cycles, and cutter compensation.

The unit command determines how the controller interprets coordinates, feed rates, and offset values.

Positioning and Offset Commands

Positioning and compensation commands define how coordinates and tool corrections are applied.

CodeFunction
G40Cancel cutter compensation
G41Cutter compensation left
G42Cutter compensation right
G43Apply tool length compensation
G54–G59Select a work coordinate system
G90Absolute positioning
G91Incremental positioning

G90 measures each coordinate from a fixed program origin.

G91 measures each movement from the tool’s current position.

Drilling and Tapping Cycles

Canned cycles reduce the number of program blocks needed for repeated hole-making operations.

CodeFunction
G80Cancel the active canned cycle
G81Standard drilling cycle
G83Peck drilling cycle
G84Tapping cycle

For example, G83 retracts the tool during drilling to help remove chips from deeper holes.

G84 coordinates spindle rotation and feed movement to produce an internal thread.

This is not a complete CNC G code list. Advanced machines may support probing commands, macros, multi-axis functions, and manufacturer-specific cycles.

The exact syntax and behavior of common G codes can also vary between controllers. Always verify each command against the programming manual for the target CNC machine.

How to Read CNC G Code

Reading CNC G code becomes easier when you review each program block in a consistent order.

Start by identifying the active units, coordinate mode, work offset, and motion command. Then check the axis positions, feed rate, spindle speed, tool number, and compensation values.

A useful reading sequence is:

  1. Check whether the program uses inches or millimeters.
  2. Identify absolute or incremental positioning.
  3. Find the active motion command.
  4. Read the X, Y, and Z coordinates.
  5. Check the feed rate and spindle speed.
  6. Confirm the tool and offset values.
  7. Look for comments, pauses, or program-end commands.

Consider this simple block:

G01 X30.0 Y15.0 Z-2.0 F250

It can be read as follows:

  • G01 commands a controlled linear movement.
  • X30.0 moves the tool to the programmed X position.
  • Y15.0 moves the tool to the programmed Y position.
  • Z-2.0 sets the cutting depth.
  • F250 sets the feed rate.

The controller interprets the coordinate values according to the active unit and positioning modes.

For example, if G21 is active, the coordinates are measured in millimeters. If G90 is active, each position is measured from the program origin.

Some values remain active even when they are not repeated in every block. This means a line without a new feed rate may continue using the previously programmed F value.

Comments can also help explain the purpose of a program section:

G00 X0 Y0     (Move to start position)
G01 Z-2 F100  (Feed to cutting depth)

CNC G-Code Programming Example

CNC G-code example showing a toolpath for machining a 20 by 20 millimeter square

The following CNC G-code programming example shows a simplified toolpath for machining a 20 × 20 mm square profile on a three-axis milling machine.

%
O1001
G17 G21 G40 G49 G80 G90
T1 M06
S3000 M03
G54
G00 X0 Y0
G43 H01 Z5.0
G01 Z-1.0 F100
G01 X20.0 F200
G01 Y20.0
G01 X0
G01 Y0
G00 Z5.0
M05
M30
%

How the Program Works

%

Marks the beginning or end of the CNC program on many controllers.

O1001

Identifies the program number.

G17 G21 G40 G49 G80 G90

This is the safety start block.

It selects the XY plane, activates millimeter units, cancels active compensation and canned cycles, and sets absolute positioning.

T1 M06

Selects tool 1 and commands a tool change.

S3000 M03

Sets the spindle speed to 3,000 rpm and starts clockwise spindle rotation.

G54

Activates the first work coordinate system.

This offset tells the machine where the programmed part origin is located on the physical workpiece.

G00 X0 Y0

Rapidly positions the tool above the starting point of the square.

G43 H01 Z5.0

Applies tool length compensation using offset H01 and moves the tool to a safe height of 5 mm.

G01 Z-1.0 F100

Feeds the tool down to a cutting depth of 1 mm at 100 mm/min.

G01 X20.0 F200

Cuts the first side of the square at 200 mm/min.

G01 Y20.0

Cuts the second side.

Because G01 is modal, it remains active even though it is not repeated on every line.

G01 X0

Cuts the third side of the profile.

G01 Y0

Returns to the starting coordinate and completes the square.

G00 Z5.0

Retracts the tool to a safe height using rapid movement.

M05

Stops the spindle.

M30

Ends the program and resets it to the beginning.

Important Safety Note

This G code example is intended only to explain basic program structure.

It should not be copied directly into a production machine without checking:

  • Machine and controller compatibility
  • Tool number and tool length offset
  • Work coordinate settings
  • Material and cutting parameters
  • Workholding and fixture clearance
  • Spindle direction
  • Safe approach and retract positions

Modal and Non-Modal G Codes

G code commands can be divided into modal and non-modal instructions.

Understanding the difference is important because some commands remain active after the controller reads them.

What Is a Modal G Code?

A modal G code stays active until another command from the same group replaces it.

For example, after the controller reads G01, the machine continues using linear interpolation until it receives another motion command, such as:

  • G00 for rapid positioning
  • G02 for clockwise circular movement
  • G03 for counterclockwise circular movement

This is why G01 does not need to appear before every cutting move.

G01 X20.0 F200
Y20.0
X0
Y0

All four movements use G01 because no other motion mode replaces it.

Other common modal commands include:

  • G20 and G21 for measurement units
  • G90 and G91 for positioning modes
  • G17, G18, and G19 for plane selection
  • G54–G59 for work coordinate systems

What Is a Non-Modal G Code?

A non-modal G code applies only to the block in which it appears.

For example, G04 commands a dwell or temporary pause. The machine pauses for the programmed time, but the dwell does not remain active in the next block.

Modal settings can affect later commands even when they are not repeated.

For this reason, CNC programs often begin with a safety block that clearly defines the active units, plane, positioning mode, and compensation settings.

Are G Codes Universal Across CNC Machines?

G-code is widely used across CNC machines, but a complete program is not automatically compatible with every machine.

Basic commands such as G00, G01, G02, and G03 are recognized by many controllers. However, their available options, syntax, and operating limits may differ.

These differences can depend on:

  • Controller brand
  • Machine configuration
  • Number of axes
  • Installed options
  • Canned cycles
  • Macro functions
  • Tool-change systems
  • Probing equipment

Even two milling machines may require different program formats if they use different controllers.

Why the Post-Processor Matters

CAM software first creates a general toolpath based on the part geometry, tools, and machining strategy.

A post-processor then converts that toolpath into CNC G code for a specific machine and controller.

The post-processor determines how the program handles:

  • Tool changes
  • Work offsets
  • Rotary-axis movements
  • Canned cycles
  • Coolant commands
  • Program numbers
  • Safety positions

Using the wrong post-processor can create unsupported commands, incorrect rotary movements, or unsafe tool positions.

For this reason, CNC G code should always be generated for the machine that will run it.

How CNC G Code Is Verified

A generated program should not be sent directly to production without review.

Before machining begins, the programmer or operator should confirm:

  • The correct machine and controller
  • Tool numbers and offset values
  • Work coordinate locations
  • Units and positioning modes
  • Spindle direction and speed
  • Feed rates and cutting depths
  • Fixture and tool clearance
  • Safe approach and retract movements

CAM simulation can identify many toolpath errors before the program reaches the machine.

The operator may also use graphics mode, single-block operation, dry run, or a reduced rapid override during the first setup.

The first completed part should then be measured against the drawing as part of the CNC quality control process.

G codes may follow common principles, but reliable production still depends on the correct controller, post-processor, machine setup, and verification procedure.

CNC Milling and CNC Lathe G Code

G-code is used in both CNC milling and CNC turning, but the machine movements and programming methods are not identical.

Milling machines rotate the cutting tool. CNC lathes rotate the workpiece.

CNC Milling G Code

More advanced machines may also use A, B, or C rotary axes.

Common milling operations include:

  • Face milling
  • Pocket machining
  • Slot cutting
  • Contouring
  • Drilling
  • Tapping

Commands such as G00, G01, G02, and G03 define rapid, linear, and circular tool movements.

Work offsets and tool length compensation help the controller relate the programmed coordinates to the actual tool and workpiece positions.

CNC Lathe G Code

CNC lathe G code is commonly used for:

  • Facing
  • Straight turning
  • Taper turning
  • Grooving
  • Threading
  • Drilling
  • Boring
  • Parting off

Many basic commands are shared with milling machines.

For example, G00 is used for rapid positioning, while G01 controls a linear cutting move at a programmed feed rate.

However, CNC lathes may also use turning-specific canned cycles for roughing, finishing, grooving, and threading.

Searches for “G code CNC lathe” usually refer to these turning commands and cycles. Their exact format can vary between controllers, so the program must be checked against the machine manual before use.

CNC milling and lathe programs follow the same basic G-code principles, but their axes, tooling systems, coordinate conventions, and machining cycles are different.

G-Code vs M-Code

G-code and M-code work together in CNC programming, but they control different parts of the machining process.

G codes mainly control tool movement, coordinate modes, offsets, and machining cycles.

M codes control supporting machine functions such as the spindle, coolant, tool changes, and program stops.

CategoryG-CodeM-Code
Main purposeControls movement and machining modesControls auxiliary machine functions
Common examplesG00, G01, G02, G90M03, M05, M08, M30
Typical functionsPositioning, cutting paths, units, offsetsSpindle control, coolant, tool changes
Used in CNC programsYesYes

For example:

G01 X20.0 F200

This command moves the tool in a straight line to the programmed X position at a feed rate of 200.

By comparison:

M03

This command starts the spindle in the clockwise direction.

A CNC program often combines both types of commands:

S3000 M03
G01 X20.0 F200
M05

In this example:

  • S3000 sets the spindle speed.
  • M03 starts the spindle.
  • G01 controls the cutting movement.
  • M05 stops the spindle.

It is important to note that the S address, not the G code, defines the spindle speed.

Similarly, the F address sets the feed rate, while the active G code determines the type of movement.

In simple terms, G-code tells the machine how the tool should move, while M-code controls what supporting machine functions should occur during the process.

Who Needs to Understand G-Code Programming?

Different manufacturing roles need different levels of G code programming knowledge.

CNC Programmers

CNC programmers create, edit, and verify machining programs.

They need to understand:

  • Toolpaths
  • Work offsets
  • Tool compensation
  • Canned cycles
  • Cutting parameters
  • Controller-specific commands

They must also recognize unsafe movements and programming errors before a machine begins cutting.

CNC Machinists and Operators

Machinists and machine operators may not create every program from the beginning, but they still need to read and adjust CNC G code.

This helps them:

  • Set tools and work offsets
  • Check spindle speeds and feed rates
  • Run first-part inspections
  • Troubleshoot alarms
  • Make controlled program edits

Manufacturing Engineers

Manufacturing engineers use G-code knowledge to review machining strategies, reduce cycle times, and improve process reliability.

They may also work with programmers to select tools, plan setups, and prevent collisions or part distortion.

Product Designers and Buyers

Product designers and purchasing teams do not normally need to write production-ready G-code.

However, a basic understanding of CNC programming can help them communicate more clearly about:

  • Part geometry
  • Tolerances
  • Materials
  • Surface finishes
  • Production quantities

The manufacturing team then creates the toolpath, generates the CNC G code, verifies the setup, and inspects the finished parts.

Conclusion

G-code converts a digital machining plan into movements and instructions that a CNC controller can execute.

It controls tool positions, cutting paths, coordinate modes, feed rates, offsets, and machining cycles.

Although many G codes are widely recognized, a production program must still match the specific machine, controller, tooling, workholding, and setup.

For custom parts, customers do not normally need to provide finished CNC G code.

The manufacturing team can then create the toolpath, generate the correct program, verify the setup, and inspect the finished parts before delivery.

Frequently Asked Questions

What Is a Gcode?

“Gcode” usually refers to G-code, the numerical control language used to operate CNC machines.

A G-code program contains commands for tool movement, coordinate settings, feed rates, offsets, and machining cycles.

What Is G Code Programming?

G code programming is the process of creating instructions that tell a CNC machine how to move and perform machining operations.

Simple programs may be written manually. More complex programs are normally generated with CAM software and then converted for a specific machine controller.

What Is CNC G Code?

CNC G code is the set of movement and machining instructions interpreted by a CNC controller.

It works with axis coordinates, feed rates, spindle settings, tool calls, offsets, and M codes to control the machining process.

Are CNC Milling and CNC Lathe G Codes the Same?

CNC milling and CNC lathe G codes share many basic commands, including G00 for rapid positioning and G01 for linear movement.

However, their axes, tooling systems, coordinate conventions, and canned cycles are different.

CNC lathe programs may also include turning-specific roughing, grooving, finishing, and threading cycles.

Are G Codes Universal?

Basic G codes are widely recognized, but complete programs are not universal across every CNC machine.

Controller brands, machine configurations, macros, optional functions, and canned-cycle formats can affect how a command is written and executed.

Where Can I Find a Complete G Codes List?

A general G codes list can help you understand common commands.

However, the programming manual for the target machine and controller is the most reliable source for a complete list because supported commands and syntax may vary.

Is G Code Difficult to Learn?

Basic G-code commands are relatively easy to understand.

Production programming requires additional knowledge of tooling, work offsets, cutting parameters, fixtures, machine safety, and controller-specific functions.

What Is the Difference Between G00 and G01?

G00 moves the tool rapidly between positions and is normally used for non-cutting movements.

G01 moves the tool along a straight path at a programmed feed rate and is commonly used during cutting.

Do Customers Need to Provide G-Code?

No. Customers usually do not need to provide production-ready CNC G code.

  • A 3D CAD model
  • A 2D engineering drawing
  • Material requirements
  • Tolerances
  • Production quantity
  • Surface finish requirements

The manufacturer can then create the toolpath, generate the program, verify the setup, and inspect the finished parts.

baosheng logo

BaoSheng Team

This article was written by engineers from the Baosheng industrial team. Comprising professional engineers and technical experts, Baosheng possesses years of industry experience in rapid prototyping, metal part manufacturing, and plastic part production.

Leave A Message

Recent Blog Posts