FANUC macro variables are the foundation of every macro you will ever write, and most macro bugs come from using the wrong kind. By the end you will know the four kinds of variable on a FANUC 0i with Custom Macro B, how a G65 call drops its values into local variables, which variables survive a power-off, how to read the machine’s own data with system variables, and why vacant is not the same as zero.
You can try every example in the MacroMill FANUC VMC simulator, the FANUC VMC Advanced Control Suite, and watch the variables change as the program runs. Yes, programs and macros you write and test in a MacroMill simulator can be saved or exported and run on a real CNC machine, after a normal prove-out with single block, reduced rapids and a dry run.
The four kinds of FANUC macro variables in one minute
A variable is a numbered memory slot written with a # sign. You can put a number in it, read it back, and use it in place of a number in a block. What differs is where the number lives and how long it stays.
- Local variables, #1 to #33. Private to one macro call. They are where G65 puts your arguments, and they are emptied when the macro ends.
- Common variables, #100 to #199. Shared by every program. On most controls they are cleared when the power goes off.
- Retained common variables, #500 to #999. Shared by every program, and kept when the power goes off.
- System variables, such as #3000, #4001, #5021 and #5221. They connect the macro to the control itself: alarms, modal codes, positions and offsets.
The exact ranges available depend on your control and its options, so check your Custom Macro B manual. The numbers above are the FANUC macro variables you will find on a typical FANUC 0i Macro B mill.
Local variables, #1 to #33, and how G65 fills them
When you call a macro with G65, each argument letter goes into one fixed local variable of the called program. The letter does not matter to the macro. Only the variable number does. This is the table for the usual form, argument specification I.
| Letter and variable | Letter and variable | Letter and variable |
|---|---|---|
| A = #1 | I = #4 | T = #20 |
| B = #2 | J = #5 | U = #21 |
| C = #3 | K = #6 | V = #22 |
| D = #7 | M = #13 | W = #23 |
| E = #8 | Q = #17 | X = #24 |
| F = #9 | R = #18 | Y = #25 |
| H = #11 | S = #19 | Z = #26 |
Look at what is missing. G, L, N, O and P cannot be used as arguments, because the control needs them for other jobs: P is the program number and L is the repeat count. Also notice that #10, #12, #14, #15, #16 and #27 to #33 have no letter. In a G65 call they are never filled for you, so they are the natural places for a macro’s own working numbers, such as a counter.
A second form, argument specification II, lets you pass A, B and C once and then up to ten sets of I, J and K, which reaches all the way to #33. Most macros never need it. Check your manual before you rely on it.
Two more rules matter. Every letter you leave out stays vacant in the called macro, and that is not the same as zero, which we come back to below. And each G65 call gets its own set of local variables, so a macro can call another macro without the two overwriting each other’s #1 to #33. This is the real difference between G65 and M98: G65 passes arguments and starts a new set of locals, while M98 passes nothing and keeps the caller’s locals.
Common variables, #100 to #199 and #500 to #999
Common variables are the shared FANUC macro variables that act as the programmer’s notice board. Any program can write a value into #100, and any other program can read it. The pattern below is how a main program hands data to a macro, and how a macro hands a result back.
(PATTERN: MAIN PROGRAM SETS A VALUE)#100 = 45.(PATTERN: ANOTHER PROGRAM USES IT)#101 = #100 * 2
The difference between the two ranges is memory. #100 to #199 are cleared when the power is switched off on most controls, so use them for scratch values and for values passed between programs during a run. #500 to #999 are retained, so they are the place for things the machine must remember: a part counter, a tool-life count, or a measured offset that the next shift needs.
(PATTERN: PART COUNTER, KEPT AFTER POWER OFF)#500 = #500 + 1IF [#500 GE 100] THEN #3006 = 1 (CHANGE INSERT)
Setting #3006 stops the program and shows the message, so the operator can act and then continue. Because retained variables are kept, set the counter to 0 on purpose when you change the insert, or the count will carry on from the old value. A parameter on some controls can also clear common variables on reset, so know what your control does.

The screenshot shows the idea at work. In the bolt hole circle macro the numbers you type are common variables, #100 to #114, and the program reads them wherever it needs them. Change #103 from 8 to 6 and the same program cuts six holes. That is the whole point of putting values in variables instead of in the blocks. The full macro is in our WHILE vs IF lesson.
System variables: reading and writing the machine itself
System variables are FANUC macro variables with fixed numbers and fixed meanings. Some are read-only, like a gauge. Others you can write, like an offset. This table covers the ones used most on a FANUC 0i Macro B mill.
| Variable | What it holds | Read or write |
|---|---|---|
| #3000 | Macro alarm. #3000 = 1 (MESSAGE) stops the program with an alarm and shows the message | Write |
| #3006 | Stop with message. The program stops and shows the message | Write |
| #4001 | Modal G-code group 01: 0 for G00, 1 for G01, 2 for G02, 3 for G03 | Read |
| #4003 | Modal G-code group 03: 90 for G90, 91 for G91 | Read |
| #4014 | Modal G-code group 14: 54 for G54, 55 for G55, and so on to 59 | Read |
| #5021, #5022, #5023 | Current X, Y, Z position in machine coordinates | Read |
| #5041, #5042, #5043 | Current X, Y, Z position in work coordinates | Read |
| #5221, #5222, #5223 | G54 work offset for X, Y, Z | Read and write |
| #5241, #5242, #5243 | G55 work offset for X, Y, Z (G56 starts at #5261, G57 at #5281, G58 at #5301, G59 at #5321) | Read and write |
| #2001 and up | Tool offsets, the older numbering for the first 200 offsets. On a mill with offset memory C the newer numbers are #10001 for H wear, #11001 for H geometry, #12001 for D wear and #13001 for D geometry, plus the offset number | Read and write |
In the position and offset families the last digit is the axis: 1 is X, 2 is Y, 3 is Z. So #5043 is the Z work position and #5223 is the G54 Z offset. A few ways to use them:
(PATTERN: REFUSE TO RUN IN INCREMENTAL)IF [#4003 EQ 91] THEN #3000 = 10 (G91 ACTIVE)(PATTERN: SAVE THE MACHINE Z POSITION)#101 = #5023(PATTERN: READ THE G54 X OFFSET)#102 = #5221(PATTERN: H GEOMETRY OF OFFSET 1, MEMORY C)#103 = #11001
Treat the writable ones with respect. Writing a value into #5221 changes where G54 sits, and the next program will use it. Read before you write, and never write to an offset you have not checked. Offset memory type and variable numbers vary between controls, so confirm the tool offset numbers on your own machine before you use them. Position variables can also give an unexpected value if you read them while the control is still looking ahead through the program, so read them after the move has finished. The probing lesson uses these same system variables to turn a measured position into a work offset.

Here the simulator shows the variables beside the program lines. The last-change line at the bottom of the panel says #153 went from 450 to 540, and that it is a common variable. Reading the variable table while you step through a macro is the fastest way to learn which variable did what.
Vacant, #0 and the trap of null versus zero
A variable that has never been given a value is vacant, also called null. #0 is a special variable that is always vacant. You can read it and compare with it, but you cannot write to it. Vacant is not zero, and the control treats them differently in four places.
| Where | If #1 is vacant | If #1 is 0 |
|---|---|---|
| In an address: G90 X100. Y#1 | The Y word disappears and the block runs as G90 X100. | Runs as G90 X100. Y0 and Y moves to zero |
| Copying: #2 = #1 | #2 becomes vacant | #2 becomes 0 |
| Arithmetic: #2 = #1 * 5 | #2 is 0, because vacant acts as zero here | #2 is 0 |
| Test: #1 EQ #0 | True | False |
| Test: #1 NE 0 | True | False |
| Test: #1 GT 0 | False | False |
The rule is that vacant behaves like zero in arithmetic and in GT, GE, LT and LE. It differs in a straight copy, in an address, and in EQ and NE. That gives you the right way to test whether an argument was passed:
(PATTERN: CORRECT TEST FOR A MISSING ARGUMENT)IF [#26 EQ #0] THEN #3000 = 3 (Z MISSING)(PATTERN: WRONG, A VACANT Z IS NOT EQUAL TO 0)IF [#26 EQ 0] THEN #3000 = 3 (Z MISSING)
The first line fires when Z was left out. The second does not, because a vacant Z is not equal to 0 in an EQ test, so the macro carries on without a depth. To clear a variable back to vacant, copy #0 into it, for example #101 = #0.
A worked example: a G65 call into a row of holes
This worked example shows FANUC macro variables in a small parametric program, O9100, that drills a row of equally spaced holes with a peck cycle. It is a teaching example: tool, tool length offset, spindle and a safe Z must already be in place before the call, and every value must be proved out before you cut. The call looks like this:
G65 P9100 X20. Y30. Z-15. R2. Q5. F120. I25. K4.
Every dimension has a decimal point, and that matters. An argument written without one can be read in the control’s least input increment, so R2 could arrive as 0.002 instead of 2. The call lands in these local variables:
| Argument | Local variable | Value | Meaning in O9100 |
|---|---|---|---|
| X20. | #24 | 20 | X of the first hole |
| Y30. | #25 | 30 | Y of the row |
| Z-15. | #26 | -15 | hole bottom |
| R2. | #18 | 2 | R plane above the top |
| Q5. | #17 | 5 | peck depth |
| F120. | #9 | 120 | feed |
| I25. | #4 | 25 | pitch between holes |
| K4. | #6 | 4 | number of holes |
Every other local variable, such as #1, #2 and #3, starts vacant. Now the macro:
O9100 (ROW OF HOLES - PATTERN)IF [#24 EQ #0] THEN #3000 = 1 (X MISSING)IF [#25 EQ #0] THEN #3000 = 2 (Y MISSING)IF [#26 EQ #0] THEN #3000 = 3 (Z MISSING)IF [#9 EQ #0] THEN #3000 = 4 (F MISSING)IF [#17 EQ #0] THEN #3000 = 5 (Q MISSING)IF [#18 EQ #0] THEN #18 = 2.IF [#6 EQ #0] THEN #6 = 1.IF [[#6 GT 1] AND [#4 EQ #0]] THEN #3000 = 6 (I MISSING)#10 = 1.G90 G99 G83 X#24 Y#25 Z#26 R#18 Q#17 F#9WHILE [#10 LT #6] DO1#24 = #24 + #4X#24#10 = #10 + 1.END1G80M99
Read it from the top. The first five IF lines use EQ #0, the correct vacant test, to refuse to run if the X, Y, Z, F or Q argument is missing. The next two lines give R and K sensible defaults when they are left out, so a call without K drills one hole. The IF with AND checks that a pitch was given whenever there is more than one hole.
Then #10, a local variable with no argument letter, becomes the hole counter. The G83 block drills hole 1 at X20, Y30. The WHILE loop adds the pitch to #24, moves X, which runs the modal G83 cycle at the new position, and counts. This is the same WHILE you met in the WHILE vs IF lesson. Here is the trace:
| Step | #24 (X) | #10 (counter) | Test: #10 LT #6 |
|---|---|---|---|
| Hole 1, before the loop | 20 | 1 | 1 LT 4, true |
| Pass 1, hole 2 | 45 | 2 | 2 LT 4, true |
| Pass 2, hole 3 | 70 | 3 | 3 LT 4, true |
| Pass 3, hole 4 | 95 | 4 | 4 LT 4, false, loop ends |
Four holes at X20, X45, X70 and X95, all on Y30. The M99 at the end returns to the caller and the local variables are thrown away. The caller’s own #1 to #33 were never touched, because the G65 call gave O9100 its own set.
Step through this program in the simulator and watch #24 and #10 change on each pass. If you change K4. to K6., you will see two more holes appear, with no other change to the program.
Common variable mistakes and how to avoid them
- Assuming a missing argument is zero. It is vacant. Test it with EQ #0, not EQ 0.
- Leaving the decimal point off a G65 argument. Write Z-15., not Z-15. A missing point can make the value a thousand times too small on a typical metric control.
- Passing a letter that cannot be an argument. G, L, N, O and P are not available. L is the repeat count, so G65 P9100 L5 calls the macro five times, it does not pass a number.
- Using the same common variable for two jobs. If your probing macro and your drilling macro both use #100 as scratch, they will overwrite each other. Use locals for scratch values and keep a written list of which common variables belong to which macro.
- Expecting values to outlive their range. Locals disappear at M99. #100 to #199 are cleared at power-off on most controls. Only #500 and up are kept.
- Trusting a retained variable you never set. A part counter in #500 carries over from whoever used it last. Reset it on purpose.
- Writing to a read-only system variable. Variables like #4001 and #5041 report the machine state. Writing to them raises an alarm.
- Mixing up G65 and M98. M98 does not pass arguments and does not start a new set of locals, so a subprogram can change the caller’s #1 to #33.
For more of these and how to catch them early, read common FANUC macro mistakes.
Why FANUC macro variables make one program do the work of many
Without FANUC macro variables, a row of four holes at 25 mm is four sets of blocks. A row of six holes at 30 mm is another program. With variables, it is one program and one G65 line. The geometry lives in the call, the logic lives in the macro, and the checks live in the IF lines. That is how a macro becomes something you can reuse on any job. For the bigger picture, see the CNC macro programming guide and write, test and run a FANUC macro.
Try it yourself
1. Which local variables do the arguments A, F and Z land in?
A goes into #1, F into #9 and Z into #26.
2. You call G65 P9100 X20. Y30. Z-15. F120. Q5. with no K. What is #6 when the macro starts, and what does O9100 do about it?
#6 is vacant. The line IF [#6 EQ #0] THEN #6 = 1. sets it to 1, so the macro drills one hole.
3. Which survives a power-off, #150 or #550?
#550. Common variables from #500 to #999 are retained, while #100 to #199 are cleared on most controls.
4. #1 is vacant because A was left out. Is IF [#1 EQ 0] true or false? And IF [#1 EQ #0]?
The first is false, because vacant is not equal to 0 in an EQ test. The second is true. Use EQ #0 to detect a missing argument.
5. In the worked example, what is the X position of the last hole if K is 4 and I is 25, starting at X20.?
X95. The holes are at X20, X45, X70 and X95.
6. Which system variables hold the current Z position in work coordinates and the G54 Z offset?
#5043 holds the Z position in work coordinates. #5223 holds the G54 Z offset.
Practise macro variables in the MacroMill simulator
Reading about variables helps, but you learn them fastest by watching them change. The MacroMill FANUC VMC simulator lets you write a macro, call it with G65, run it one block at a time, and see each variable and each test as it happens. You can leave out an argument on purpose, watch the macro stop with its own alarm, and fix it, all before you go near a machine. Programs and macros you write and test there can be saved or exported and run on a real CNC machine, after a normal prove-out with single block, reduced rapids and a dry run.
Get the simulator here: MacroMill FANUC VMC Advanced Control Suite on Payhip, or see it on the MacroMill homepage. If you have a question about the lesson, write to info@macromillcnc.online.
For tips and updates, join the MacroMill WhatsApp group: chat.whatsapp.com/Htae97asBiD14QiwMXjGJA.
The software is for training and simulation only. Yes, programs and macros you write and test in a MacroMill simulator can be saved or exported and run on a real CNC machine. As with any new program, prove it out first with single block, reduced rapids and a dry run before you cut. Addresses, variable numbers, feeds, speeds, clearances and tool offsets in these examples must be confirmed against your machine, your tooling and your control’s Custom Macro B manual.
MacroMill

Leave a Reply