CNC macro programming is how you add variables, calculations and decisions to G-code on a FANUC control. Instead of a fixed list of moves, a Custom Macro B program can take inputs, do the arithmetic, loop through a pattern and change what happens next. This guide covers the variable types, the language, G65 calls, a worked example and common mistakes.
What a CNC macro is
Plain G-code can move axes, start the spindle, call a canned cycle and stop. What it cannot do on its own is store a value, test a condition, or calculate the next position from a formula. A CNC macro adds that language.
On FANUC controls the language is Custom Macro B, written in the same program stream as G-code. A line can assign a variable, branch with IF and GOTO, repeat with WHILE, or calculate a position and then use it in an X, Y or Z word. That is why G-code is not a programming language on its own, and macros make it one — see G-code is not a programming language. Macros make it one.
A macro earns its place when a job repeats with different sizes, when a pattern would otherwise be copied by hand, or when the program should check an input before it cuts. For a wider list, see What problems do CNC macros solve?. Writing the logic once and changing the inputs is also the subject of Unlock CNC macros productivity on the shop floor.
Variables in FANUC Custom Macro B
Everything in CNC macro programming sits on variables: numbered stores, written with a hash, that hold a numeric value. You can assign one, read it, and use it in a calculation or in a motion word:
#100=40.
#101=#100/2.
G00 X#100 Y#101;
FANUC groups variables by range and by how long their values last. Exact limits can vary between control series, so treat the ranges below as the usual map and check your manual if a number is refused.
Local variables (#1 to #33)
Local variables belong to the macro that is running. G65 argument letters land in this range, and those values are gone when the macro returns. If one macro calls another, each call has its own locals, so the caller’s values are not overwritten. Use this range for arguments and temporary results, not for a value you will need after M99.
Common variables (#100 to #199 and #500 to #999)
Common variables are shared across programs. Use them for values that must survive a return from a macro. #100 to #199 are the working common variables and, on many controls, are cleared by reset or by power off. #500 to #999 are retained after power off, which makes them useful for a setting you want to hold, and risky if a program writes them by accident. Do not use a retained variable for a temporary counter.
System variables
System variables are maintained by the control. They give a macro a window onto tool offsets, work offsets, modal state, clock data and alarm handling. The numbers depend on the control, and some ranges are read-only. Learn the ones you need from that control’s Custom Macro manual; a wrong write can change an offset or raise an alarm.
Vacant values are not zero
A variable can hold a number, including zero, or it can be vacant (empty). Vacant is not the same as 0. #0 is always vacant, and you cannot assign a value to it. If a G65 call omits an argument letter, the matching local is vacant, not zero, and using it in arithmetic usually alarms. Test for a missing argument by comparing with #0. In the line below, X0. was passed, so #24 holds 0 and the test is false. If X had been left out, the test would be true:
IF[#24 EQ #0]GOTO900;
Arithmetic and functions
Macro B uses +, −, * and / for arithmetic. Group expressions with square brackets, not parentheses. Brackets can be nested, and function arguments also go in square brackets:
#100=[#101+#102]/2.
#103=SQRT[#104]
#105=#106*COS[#107];
Trigonometric functions (SIN, COS, TAN) use degrees, not radians, which matters as soon as you calculate a pitch-circle position. Other early functions include SQRT, ABS and ROUND. FIX and FUP drop or raise a value to a whole number; check the rounding rules in your manual before you rely on them. Write decimal points on real dimensions, for example 40. rather than 40. On some controls a number without a decimal point is treated as a different unit.
IF and GOTO
The basic decision in Custom Macro B is IF with GOTO. The condition goes in square brackets. If it is true, the control jumps to that sequence number in the same program; if it is false, execution continues with the next block:
IF[#11 LE 0]GOTO900
...
N900
(INVALID HOLE COUNT)
M99;
The comparison operators are EQ, NE, GT, LT, GE and LE. Do not use =, > or < inside the test. Use IF/GOTO for checks and a clean exit, and WHILE when the same block should repeat.
WHILE, DO and END
A WHILE loop repeats a block for as long as its condition is true. The control tests the condition before each pass and, if the test is false, skips to the block after the matching END:
#30=1
WHILE[#30 LE #11]DO1
...
#30=#30+1
END1;
DO1 and END1 are a pair. On most FANUC controls you can nest loops with DO2 and DO3, and the numbers must match. Watch for a counter that never changes, so the loop never ends, and an update written in the wrong place, so you get one pass too many or too few. For a full walk-through, see How a FANUC macro WHILE loop works.
G65 macro calls and argument letters
G65 is the standard way to call a CNC macro and pass values into it. P is the program number, L is an optional repeat count, and the other letters are arguments that write to fixed local variables in the called program:
G65 P8001 X0. Y0. Z-12. R40. A0. H6. F80.;
The usual letter-to-variable map is:
- A → #1, B → #2, C → #3
- I → #4, J → #5, K → #6
- D → #7, E → #8, F → #9, H → #11
- R → #18, S → #19, T → #20
- U → #21, V → #22, W → #23
- X → #24, Y → #25, Z → #26
G, L, N, O and P are not used as value arguments. P is the program number, and L is the repeat count. Extra inputs can be stored in common variables before the call.
G65 is not the same as M98. M98 can call a subprogram, but it does not load argument letters into #1 to #33. If a program expects X, Y, R and H, call it with G65. End the called program with M99, which returns to the block after the G65 call.
Worked example: a bolt hole circle
This example ties the pieces together. The calling program sets a tool, then G65 passes a centre, depth, pitch-circle radius, start angle, hole count and feed. The macro checks the required inputs, then uses a WHILE loop and COS/SIN to drill each hole with G81. The values are metric training figures, not a job setup.
With a centre at X0 Y0, a 40 mm radius, a start angle of 0 degrees and six holes, the first hole should land at X40. Y0., with the holes 60 degrees apart. If the positions do not match, check the angle units and whether you passed a radius or a diameter.
O8000 (BOLT HOLE CIRCLE CALL)
G90 G54 G17 G21 G40 G49 G80
T1 M06
G00 X0. Y0.
G43 Z50. H01
S1200 M03
G65 P8001 X0. Y0. Z-12. R40. A0. H6. F80.
G00 Z50.
M05
M30
O8001 (BOLT HOLE CIRCLE MACRO)
(X=#24 CENTRE X)
(Y=#25 CENTRE Y)
(Z=#26 HOLE DEPTH)
(R=#18 PITCH CIRCLE RADIUS)
(A=#1 START ANGLE DEG)
(H=#11 NUMBER OF HOLES)
(F=#9 FEED)
IF[#11 EQ #0]GOTO900
IF[#11 LE 0]GOTO900
IF[#18 EQ #0]GOTO900
IF[#18 LE 0]GOTO900
IF[#26 EQ #0]GOTO900
IF[#9 EQ #0]GOTO900
IF[#9 LE 0]GOTO900
#30=1
#31=0
IF[#1 EQ #0]GOTO10
#31=#1
N10
#32=360./#11
WHILE[#30 LE #11]DO1
#33=#24+#18*COS[#31]
#34=#25+#18*SIN[#31]
G81 X#33 Y#34 Z#26 R5. F#9
#30=#30+1
#31=#31+#32
END1
G80
M99
N900
(INVALID BHC INPUT — CHECK THE CALL)
M99;
The vacant tests come before any comparison such as LE, because a vacant value used in arithmetic or in a relational test can alarm. A is optional: if it is omitted, the start angle stays 0. X and Y can be zero, so a centre at X0. Y0. is valid as long as those letters are passed. G80 after the loop cancels the drill cycle before the macro returns.
This is a teaching skeleton: one drill cycle, one radius, and no helical milling. For a fuller bolt hole circle, including helical milling on a pitch circle, see Two new lessons in CNC Automation Pro 2026 — bolt hole circle and A-axis rope groove. To combine a macro with a canned cycle, see Combining Fanuc Macro B with canned cycles.
Common mistakes in CNC macro programming
- Using parentheses for maths. Group expressions in square brackets: SIN[30.] is valid, SIN(30.) is not.
- Using =, > or < in a condition. Write EQ, NE, GT, LT, GE or LE inside the test.
- Treating a vacant variable as zero. A missing G65 argument is empty; test it against #0 before you use it.
- Forgetting that trig uses degrees. COS[90.] is 0; a radian input will place the holes incorrectly.
- Calling a macro with M98 and expecting arguments. Argument letters are loaded by G65 or G66, not by M98.
- Storing a result in a local variable and expecting it after M99. Use a common variable if the caller needs the value.
- Writing a temporary counter into the #500 range. Use #100 to #199, or a local; retained variables keep their values after power off.
- Leaving the loop counter unchanged. The WHILE test then stays true. See the FANUC WHILE loop post for that failure.
- Omitting G80 after a canned cycle inside a macro. The cycle can stay modal and surprise the next move in the caller.
- Skipping input checks. A hole count of zero, a missing radius or a vacant feed should stop the macro.
Practise and next steps
The fastest way to get comfortable with CNC macro programming is to change one input at a time and watch the result. The MacroMill FANUC VMC simulator lets you practise macros without a machine: load a program, step through the variables and confirm the toolpath before it reaches a real control.
For reference material, start with the free CNC macro resources page. When you want complete programs to study, open the CNC macro program library.
For a structured path from these basics into full cycles, probing logic and family-of-parts work, use CNC Automation Pro 2026, also on Payhip. CNC macro programming is still a rare skill on the shop floor, which is why it is worth learning properly — see Why CNC macro programming is a rare skill.
FAQ
What is CNC macro programming?
It is the use of a control language — on FANUC machines, Custom Macro B — to add variables, calculations, decisions and loops to G-code, so a program can take inputs, calculate positions and change what happens next.
What is the difference between G65 and M98?
Both can send the control to another program and return with M99. G65 is a macro call: argument letters such as X, Y, R and H are copied into local variables. M98 is a subprogram call and does not pass those arguments in the same way, so a macro should be called with G65.
Which FANUC variable range should I use?
Use #1 to #33 for arguments and temporary values inside one macro call. Use #100 to #199 for shared working values that do not need to survive power off. Use #500 to #999 only for values you intend to keep. Leave system variables alone until you are following the manual for a specific offset or alarm.
Can I practise CNC macro programming without a machine?
Yes. A simulator is the right place to learn variables, IF/GOTO, WHILE loops and G65 calls, because a typo in a loop or a vacant argument should not be discovered on a real tool. The MacroMill FANUC VMC simulator is built for that practise. Still verify a program on your own control before you cut metal, because parameters and Macro B features vary.
For training only. The code in this guide is for learning and simulation. Verify it on your own machine and control before cutting. Addresses, tool offsets, clearances, spindle speeds and feeds must be confirmed against your machine, your tooling and your control’s Custom Macro B manual.
MacroMill

Leave a Reply