CNC G-Code Program Examples
A CNC G-code program example is a short, readable listing of blocks that tells a machine where to move, how fast to feed, and when to turn the spindle on or off. Each block pairs an address letter with a value, and the order of blocks follows the part: setup, safe start, cutting moves, then a controlled end. Reading a sample program line by line shows how the same few codes repeat to shape a part.
- Every block starts with a letter address, such as G for motion mode or X for a position.
- G01 feeds in a straight line, while G02 and G03 cut clockwise and counterclockwise arcs.
- A block runs once, but G-code is modal, so a motion mode stays active until another one replaces it.
- A program opens with safety and setup blocks and closes by canceling compensation and returning the tool.
- Read each block against the part drawing before the program reaches the machine.
How a G-code block is built
A G-code block is one line of the program, and each word in it is an address letter followed by a number. G words set the mode, such as motion or units, while X, Y and Z give positions and F sets the feed rate. An N word can number the block, and a comment in parentheses can explain what the block does.
The machine reads words in a block as one instruction, so the order inside the block matters less than the values matching. A block like G01 X50.0 Y25.0 F150 tells the control to feed to that point at that feed rate. Combining a motion word with axis positions in one block is what keeps a listing compact.
Most controls accept a block without a G word and reuse the active motion mode, which is called modal behavior. Understanding modal state stops you from repeating G01 on every line while still keeping the listing clear. A comment helps a reader see why a block exists without changing what the machine does.
Program structure from start to finish
A CNC program structure opens with a safe start that sets units, plane and work offset before any motion. This header prepares the control so the first move is predictable. Skipping the header invites a machine to act on settings left over from the previous job.
The body of the program holds the cutting moves in the order the toolpath follows. Tool changes, spindle starts and coolant commands sit next to the moves they support. Keeping the body in drawing order makes a sample program easier to check against the part.
The end of the program cancels tool length and cutter compensation, lifts the tool clear, and stops the spindle. A controlled end protects the part and the machine. Write the ending before the body so you always know where the tool must finish.
- Header: units, plane, work offset, safe Z
- Tool call and spindle start
- Rapid to the start point
- Cutting moves with feed rate
- Cancel compensation and retract
- Spindle stop and program end
Sample straight-line moves with G01
G01 is linear interpolation, and it feeds the tool in a straight line at the programmed feed rate. A sample program uses G01 for facing passes, side cuts and any contour made of straight segments. The feed word F sets how fast the tool travels along that line.
A simple facing example might rapid above the stock, feed down to depth, then cut across in X with a single G01 block. The next block feeds back in Y for the following pass. Repeating that pair of blocks clears the surface in even steps.
Feed rate in a G01 block depends on the material, the cutter and the depth of cut, so the value in a sample is a placeholder. Read the F word as a number you must set for your own job. A program copied from a sample needs its feeds checked against your tool and stock.
Cutting arcs with G02 and G03
G02 and G03 cut circular arcs, with G02 running clockwise and G03 running counterclockwise as seen from the standard plane. An arc block gives the end point plus either a radius with R or the arc center with I and J. The choice between R and center coordinates changes how the control computes the path.
A radius word is short and readable, and it works when the arc is a clean sweep under a half circle. Center offsets with I and J describe the arc precisely and handle sweeps that a single radius cannot. Many controls also accept a full circle only when I and J define the center.
Arc direction must match the drawing, or the tool cuts the wrong side of the path. Check the start point, the end point and the center before the block runs. A wrong arc can scrap a part or drive the cutter into the fixture, so a dry run above the stock is worth the time.
A short sample program to read
A sample program below shows the header, a rapid approach, one straight cut and an arc, then a clean end. The listing is a teaching example, not a finished job, so the values need checking against a real part. Read each block and say aloud what the machine will do.
The first blocks set the mode and send the tool to a safe height. Later blocks feed into the work and follow the contour. The last blocks retract and stop the spindle.
Compare the sample against a drawing to see how each coordinate comes from a dimension. The X and Y values in the listing should match points on the part, and Z should match the depth. A listing that reads clearly is easier to debug when a cut goes wrong.
| Block | Code | What it does |
|---|---|---|
| 1 | G21 G17 G90 | Metric units, XY plane, absolute positions |
| 2 | G54 G00 Z10.0 | Work offset, rapid to safe height |
| 3 | G00 X0 Y0 | Rapid to the start point |
| 4 | G01 Z-1.0 F100 | Feed to depth at the set feed rate |
| 5 | G01 X40.0 F200 | Straight cut along X |
| 6 | G03 X60.0 Y20.0 R20.0 | Counterclockwise arc to the end point |
| 7 | G00 Z10.0 | Retract to safe height |
| 8 | M05 M30 | Stop spindle, end program |
Feed, speed and depth in a sample
Feed rate, spindle speed and depth of cut work together, so a number copied from a sample program rarely suits another job. The material, the cutter diameter and the rigidity of the setup all change what the machine can take. Treat every F and S word as a value to confirm, not a default.
A light pass and a slow feed protect a part during a first run. Once the cut proves stable, you can raise values in steps and watch the chips and the sound. Reading the machine is part of judging a program, and no listing can replace that check.
Coolant and chip clearance belong in the same decision as feed and speed. A program that floods a pocket with chips can break a small cutter even at a modest feed. Add coolant commands where the operation needs them and confirm the supply is on.
Checking a program before it cuts
Verifying a G-code program before it runs protects the part, the fixture and the operator. Graphic simulation on the control or in software shows the toolpath, and a dry run with the tool clear of the stock proves the moves. A trained programmer or machinist should review any program that touches a new setup.
Check the work offset, the tool length and the units first, because an error in any of them moves every cut. Confirm that rapid moves stay above the part and that the retract height clears clamps. Look for a missing feed word, which can make a cutting block behave in an unexpected way.
Keep a written checklist and mark each item as you confirm it. A second reader catches what the first missed, and a program that passed a check is easier to trust on the floor. Store the checked version so the running program matches what was reviewed.
Where to practice writing examples
Writing your own short program is the fastest way to learn G-code, and a plain text editor is enough to start. Pick a simple shape, such as a square with one rounded corner, and write the blocks by hand. Then read the listing against the drawing before any machine sees it.
A CNC simulator lets you watch the toolpath without risking a cutter or stock. Compare your handwritten program with the simulated path and correct any block that goes the wrong way. Repeating this on a few shapes builds a feel for how coordinates and modes combine.
When a program runs correctly, add comments that name each operation. A clear listing with short notes helps the next reader and helps you when you return to it. Programming skill grows from writing, checking and correcting small examples.
Questions about G-code program examples
What does a basic CNC program example contain?
A basic CNC program example contains a header that sets units, plane and work offset, a body of motion blocks, and an end that retracts the tool and stops the spindle. Each block pairs an address letter with a value, such as G01 for a straight feed or X for a position. Comments in parentheses can explain any block without affecting the machine.
What is the difference between G01, G02 and G03?
G01 feeds the tool in a straight line, while G02 and G03 cut arcs. G02 runs clockwise and G03 runs counterclockwise as seen in the active plane. Arc blocks add a radius with R or center offsets with I and J to define the curve.
Can I copy a sample program straight to my machine?
No, a sample program is a teaching listing and its values need checking for your own part, tool and material. Work offset, feed rate, spindle speed and depth all depend on the setup in front of you. Verify the program in simulation and with a dry run before it cuts.
What does it mean that G-code is modal?
G-code is modal because a command stays active until another command replaces it. Once a block sets G01, later blocks without a G word keep feeding in a straight line. Modal behavior shortens listings, but you must track the active mode to predict the next move.
How do I check a G-code program before running it?
Simulate the toolpath on the control or in software and compare it with the drawing. Confirm the units, work offset and tool length, and check that rapid moves clear the part and clamps. A dry run with the tool clear of the stock then proves the moves safely.
Do I need a CAM system to write G-code examples?
No, a plain text editor is enough to write short G-code examples by hand. Handwriting blocks for a simple shape teaches how coordinates and modes combine. A CAM system helps on complex parts, but the code it posts still needs a reader who understands it.
Read next
-
G-Code Classes, Courses and Free Training
-
Klipper G-Code Shell Command Extension
-
CNC Programming Basics and Code Tutorial
-
CNC G-Codes
CNC Programming Fundamentals
-
CNC Milling Machine in Hindi
-
CNC Machine Work in Hindi
-
G-Shock Code Names
-
Siemens G-Code Programming
-
CNC G and M Code Simulator
-
Fanuc G-Code Programming
-
G-Code Programming Examples
-
Basic and Programming Books for Beginners
-
Canned Cycles in CNC: What They Are and How They Repeat
-
G-Code Programming for CNC Milling and VMC