# What Is G-Code in CNC Machining: Definition and Purpose

G-code explained: what it is in a CNC machine, what it is used for, how it differs from M-code, and what program blocks and motion commands do.

G-code is the text language a CNC machine control reads to move the machine and run a job, and its purpose is to turn a part drawing into ordered machine motion. A programmer writes G-code as a numbered list of program blocks, using codes that set the motion type, coordinates, feed rate and spindle speed. The control on the machine executes those blocks in order, so the words in the program become the paths of the tool. 

## How a G-code program is built 

A G-code program is a sequence of program blocks, and each block is one line of instructions for the control. A block can hold a G-code for motion, axis coordinates, a feed rate and a spindle speed at the same time. The control reads the blocks from top to bottom and applies each one before it moves to the next. A block that carries only an M-code runs a machine function without a motion command. 

## G-code against M-code 

G-code covers what the machine does with its axes, and M-code covers machine states such as the spindle turning or coolant flowing. A G-code motion command tells the control where the tool goes, while an M-code switches a function on or off. Both types sit in the same program and work together, because a cut needs the motion and the spindle active at once. Machine control reads the two sets of codes as one instruction stream. 

## Reading a G-code example line 

A single line such as G01 X50 Y20 F200 tells the control to move the tool in a straight line to that point at a set feed rate. The G01 word selects linear feed motion, the X and Y words give the endpoint, and the F word sets the feed rate. A block number at the start of the line only labels it, so the operator can find it on the screen. The ISO code style of these words keeps a program readable on a machine from a different builder. 

## Questions about G-code in CNC 

### What is G-code used for in CNC machining? 

G-code is used to command the motion and machine functions of a CNC machine through a full job cycle. Programmers use it to set the path of the tool, the feed rate, the spindle speed and the sequence of passes over the part. The control turns each program block into machine motion, so the same program can run a part again on a machine of the same setup. 

### What is the purpose of G-code in CNC machining? 

The purpose of G-code is to describe a machining job as an ordered set of instructions a machine control can execute without the operator guiding the tool by hand. It connects the part drawing and the process plan to the actual cutting path. Because the code is written down, the same job can be checked, edited and corrected before the tool touches the material. 

### What is G-code and M-code in a CNC machine? 

G-code and M-code are the two families of words in a CNC program, with G-code handling motion and M-code handling machine functions. G-code words position the axes and set the motion type, and M-code words start or stop the spindle, coolant or a program stop. A single program block may carry both kinds of word, so motion and machine state change together. Knowing which family each word belongs to helps when you read a program line by line. 

### What is G-code in a CNC machine with an example? 

G-code is the instruction word that tells a CNC control how to move the machine, as in the line G01 X50 Y20 F200 that feeds the tool to a point. The G01 word sets linear motion, X and Y set the endpoint, and F sets the feed rate. A rapid move uses a different G-code word for travel between cuts. Run a new or edited program in the air or in single block first, since a wrong coordinate can drive the tool into the chuck or fixture.
