---
title: "CNC Turning Macro Programming Examples (Macro B)"
description: "Macro programming adds variables, loops, and logic to turning G-code. Two worked examples: a parametric grooving loop and a parts counter, plus the syntax map."
url: https://gcodepractice.com/journal/cnc-turning-macro-programming-examples/
canonical: https://gcodepractice.com/journal/cnc-turning-macro-programming-examples/
author: "Lawrence Arya"
authorUrl: https://www.linkedin.com/in/vibecoding/
published: 2026-06-04
updated: 2026-06-04
category: "Practice"
tags: ["macro", "turning", "fanuc", "programming"]
lang: en
---

# CNC Turning Macro Programming Examples (Macro B)

> **TL;DR** Macro programming, Fanuc's Custom Macro B on most lathes, adds variables (#100), arithmetic, IF logic, and WHILE loops to ordinary G-code, so one program handles families of parts. The fastest way in is two turning examples: a grooving loop that cuts evenly spaced grooves from three variables, and a permanent-variable parts counter. LinuxCNC offers the same power through O-codes, and macro programs deserve extra-careful prove-outs because variables hide the numbers.

Ordinary G-code states every number outright. Macro programming lets the program compute them: variables hold values, loops repeat work, and IF statements make decisions. On Fanuc-style lathes this layer is called Custom Macro B, and the [extended G-code syntax](https://en.wikipedia.org/wiki/G-code) it adds turns one program into a family of programs. Two short turning examples carry most of the idea.

## What new words does macro add?

A compact set, which is the good news:

| Element | Syntax | What it does |
| --- | --- | --- |
| Variable | `#101 = 2.5` | Stores a number |
| Arithmetic | `#101 = #101 + #100` | Computes values |
| Loop | `WHILE [...] DO1` ... `END1` | Repeats blocks |
| Condition | `IF [#101 GT 30.] GOTO 90` | Branches |
| Macro call | `G65 P9010 A5. B2.` | Runs a macro with arguments |

Variables come in ranges with different lifespans: locals (`#1` to `#33`) reset per macro call, commons (`#100` range) survive within a session, and the permanent range (`#500` and up) survives power-off, the full map in [#100 vs #500 variable differences](/journal/g-code-variable-100-vs-500-differences/), which the second example uses.

## Example one: a parametric grooving loop

A shaft needs identical grooves every few millimeters. Plain G-code repeats the same four blocks per groove; the macro version reads three variables and loops:

```
#100 = 5.                  (pitch between grooves)
#101 = 10.                 (first groove, mm from face)
#102 = 30.                 (last groove position)
WHILE [#101 LE #102] DO1
  G00 Z[0 - #101]          (position at the groove)
  G01 X16. F0.08           (plunge the groove)
  G00 X22.                 (retract clear)
  #101 = #101 + #100       (step to the next groove)
END1
G00 Z5.
```

Five grooves or fifty, the program is the same length, and re-spacing the pattern means editing one number. The `Z[0 - #101]` expression is the habit worth copying: compute inside brackets so the variable can stay a readable positive distance.

## Example two: a permanent parts counter

Permanent variables remember values across power cycles, which makes a one-line production counter:

```
#500 = #500 + 1            (count this part)
IF [#500 LT 200.] GOTO 99  (keep running until 200)
M00                        (operator: insert change due)
N99 M30
```

The machine now taps the operator's shoulder at part 200 for an insert change, with the count surviving weekend shutdowns. The same pattern drives warm-up gating, offset nudges per N parts, and the probing routines that make up the hand-written macro layer described in [why machinists still write G-code manually](/journal/why-do-machinists-still-write-g-code-manually/).

## What are the gotchas?

Three, all manageable. **The option gate:** on many Fanuc-style controls Macro B is a paid option, and a control without it throws the improper-code alarm on macro statements, the situation covered in [Fanuc alarm 010](/journal/fanuc-alarm-010-improper-g-code/); check the option list first. **Dialect drift:** the concepts are universal but spellings differ, and [LinuxCNC's O-codes](https://linuxcnc.org/docs/html/gcode/o-code.html) (`o100 while` ... `o100 endwhile`) deliver the same loops with different syntax, so port the logic, never the literal text. **Hidden numbers:** a macro's positions live in variables, so a wrong input cuts a wrong part with perfect confidence; prove out with conservative values and single block, watching the variable display, more carefully than any plain program.

## Where do macros fit in your progression?

They are the top rung of the manual-programming ladder: after fluent reading, setup work, and supervised edits, the path mapped in [from CNC operator to programmer](/journal/how-to-go-from-cnc-operator-to-programmer/). Senior machinists who own probing macros and parametric families earn that standing one working example at a time, and the two above are honest first steps. The plain-code layer underneath them, the [standard motion vocabulary](https://linuxcnc.org/docs/html/gcode/g-code.html), has to be reflex first, which is what a recall routine on the [G-code practice hub](/g-code-practice/) builds.

When the same structure serves a whole catalog of sizes, the discipline scales up into [family-of-parts macro programming](/journal/macro-programming-for-family-of-parts-cnc/), variable sheets and all. One more turning niche that leans on exactly this loop structure: [broaching keyways on a lathe](/journal/g-code-for-broaching-on-a-cnc-lathe/), where the stroke-step-repeat pattern is a macro begging to be written.

## Bottom line

Macro programming adds variables, loops, and logic to turning G-code: a grooving loop shows the WHILE pattern, a permanent counter shows `#500` variables, and together they cover most everyday macro work. Mind the option gate, the dialect spellings, and the hidden numbers, and prove out harder than usual. The syntax is small; the leverage is not.

## Sources

- [LinuxCNC O-code reference (loops, conditionals, subroutines)](https://linuxcnc.org/docs/html/gcode/o-code.html)
- [LinuxCNC G-code reference](https://linuxcnc.org/docs/html/gcode/g-code.html)
- [Wikipedia: G-code (extended syntax)](https://en.wikipedia.org/wiki/G-code)

## Frequently asked questions

### What is macro programming in CNC turning?
An extension of G-code, Custom Macro B on Fanuc-style controls, adding variables, arithmetic, IF logic, and WHILE loops, so one program covers a family of parts by reading values instead of fixed numbers.

### What is a simple macro example for a lathe?
A grooving loop: three variables hold first position, pitch, and last position; a WHILE loop positions, plunges, retracts, and steps until done. Re-spacing every groove means editing one number.

### Do all CNC lathes support macro programming?
No. Macro B is a paid option on many Fanuc-style controls, which reject macro statements without it. LinuxCNC includes equivalent O-codes natively. Check the option list first.

### What is the best way to learn macro programming for turning?
Make the plain code layer reflex first. A free app like G-Code Sprint drills the everyday codes and repeats whichever ones you miss; macros then become a short list of new words.

*G-Code Sprint is a study and practice tool only. Always follow your instructor, employer, machine manual, and shop safety procedures.*

---

Source: https://gcodepractice.com/journal/cnc-turning-macro-programming-examples/
Author: Lawrence Arya — https://www.linkedin.com/in/vibecoding/
