# CNC Programming Basics and Code Tutorial

Learn CNC programming basics: how a G-code and M-code program is structured, what the main codes do, and how to read a block before you run it on a machine.

CNC programming basics come down to writing instructions a machine control reads line by line, so the tool moves and the spindle runs in a set order. A program is a text file of numbered blocks, each block holding a word such as G01 for a movement or M03 for spindle start. This page explains the code tutorial side of that: what the basic codes mean and how a program is built. 

## How a CNC program is structured 

A CNC program structure has three parts: a safe start, the machining moves, and a clean end. The first blocks set the units, cancel old offsets, and call the tool, so the machine starts from a known state. The middle blocks hold the cutting moves and the codes that switch coolant and spindle on. The last blocks stop the spindle, retract the tool, and end the program. 

## G-code handles motion and geometry 

G-code words tell the control where the tool goes and how it gets there. G00 moves fast without cutting, G01 feeds in a straight line, and G02 or G03 cut an arc with a radius or center point. A work offset such as G54 tells the machine where the part sits, and tool length compensation keeps the tip at the right height. Reading the motion codes in order is the core skill of a code tutorial. 

## M-codes switch machine functions 

M-code words control the functions that are not movement, such as spindle direction, coolant, and program stop. M03 turns the spindle clockwise, M05 stops it, and M08 floods coolant while M09 shuts it off. An M30 ends the program and rewinds it for the next part. Check every M-code against the machine manual, because the same number can differ between controls and builders. 

## Questions about CNC program basics 

### Can I get CNC basic codes as a PDF? 

A CNC basic codes PDF is handy as a pocket list, but treat any sheet as a starting point only. The control on your machine defines what each code does, so a general list can differ from your builder's manual. Keep the manual for your control at the machine and check an unfamiliar code there before you run it. 

### What is the difference between G-code and M-code? 

G-code controls the geometry of the cut, while M-code controls the machine functions around it. A movement block with G01 feeds the tool along a line, and an M-code in the same block can start the spindle. A program needs both kinds of word to cut a part correctly. 

### Do I need to learn G-code to program a CNC machine? 

Learning G-code helps even when you use CAM software, because the software writes G-code for you. Reading that output lets you check feeds, offsets, and safe start blocks before the tool touches metal. A machinist who can read the code catches errors that a screen view can hide. 

### How do I test a new program safely? 

Test a new program with the tool clear of the part and the machine in single block mode. Watch each axis position on the display and confirm every offset and tool call before you cut. Simulate the path in the control or software when it offers that function, and have a trained person review the code first.
