You copied the Doc URL to your clipboard.


The __global_reg storage class specifier allocates the declared variable to a global variable register.


__global_reg(n) type varName



Is an integer between one and eight.


Is one of the following types:

  • any integer type, except long long

  • any char type

  • any pointer type.


Is the name of a variable.


If you use this storage class, you cannot use any additional storage class such as extern, static, or typedef.

In C, global register variables cannot be qualified or initialized at declaration. In C++, any initialization is treated as a dynamic initialization.

The number of available registers varies depending on the variant of the AAPCS being used, there are between five and seven registers available for use as global variable registers.

In practice, it is recommended that you do not use more than:

  • three global register variables in ARM or Thumb-2

  • one global register variable in Thumb-1

  • half the number of available floating-point registers as global floating-point register variables.

If you declare too many global variables, code size increases significantly. In some cases, your program might not compile.


You must take care when using global register variables because:

  • There is no check at link time to ensure that direct calls between different compilation units are sensible. If possible, define global register variables used in a program in each compilation unit of the program. In general, it is best to place the definition in a global header file. You must set up the value in the global register early in your code, before the register is used.

  • A global register variable maps to a callee-saved register, so its value is saved and restored across a call to a function in a compilation unit that does not use it as a global register variable, such as a library function.

  • Calls back into a compilation unit that uses a global register variable are dangerous. For example, if a function using a global register is called from a compilation unit that does not declare the global register variable, the function reads the wrong values from its supposed global register variables.

  • This storage class can only be used at file scope.

  • Volatile variables with the __global_reg storage class specifier are not treated as volatile.


Example 5 declares a global variable register allocated to r5.

__global_reg(2) int x; v2 is the synonym for r5

Example 6 produces an error because global registers must be specified in all declarations of the same variable.

int x;
__global_reg(1) int x; // error

In C, __global_reg variables cannot be initialized at definition. Example 7 produces an error in C, but not in C++.

__global_reg(1) int x=1; // error in C, OK in C++