Static Variables

Given a permanent address in memory

When the compiler encounters a global variable or a local variable prefixed by the static keyword, it will allocate space for it in RAM, giving it a permanent (static) address. This is in contrast to auto variables which are allocated on the stack.

Exist for the entire life of the program

A static variable is created when the program first starts, and stays alive until the program exits. In an embedded system, this means that static variables always exist – the only time they disappear is when power is removed.

Global variables are always static

A global variable (declared outside of any function) has no choice – it must always be static. You cannot use the auto keyword with a global variable.

A variable declared as static inside a function…

Static variables within functions behave much like global variables in that they have a permanent address in memory. However, if you try to access the variable outside of the function, the compiler will complain that the variable doesn't exist. A static variable inside a function may only be accessed from within the function, but it will retain its value across function calls just like a global variable. This could be used to keep track of how many times a function has been called from within the function and without the use of global variables.

Function parameters cannot be static with some compilers

Unlike variables declared within a function, some compilers, including MPLAB® XC16, do not allow function parameters to be static under any circumstances – they must always be automatic. This isn't generally a problem since XC16 passes parameters very efficiently through the working registers. However, MPLAB C18 does allow static parameters, which can be accessed more efficiently than automatic variables on that architecture. This can be advantageous with the PIC18's relatively small memory size compared to other processors that are typically programmed in C.

In the above example, a will remember its value from the last time the function was called.
If given an initial value, it is only initialized when first created – not during each function call.

© 2016 Microchip Technology, Inc.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.