The C keyword 'static' - Embedded.com

The C keyword ‘static’

In C and C++, the keyword ‘static’ has two, quite separate uses. In C++, there is even a third use of the keyword. The nuances of these meanings/uses are not always well understood. This article explains those different uses and clarifies their relevance to embedded developers, who are much more likely to be concerned about the details than their desktop programming counterparts.
Here is some outline C++ code which shows static in all its guises:
static int alpha;

  struct beta
  {
    static int gamma;
    …
  };

  static void delta()
  {
    static int epsilon;
    …
  }

Static and dynamic storage of variables
In English, the word “static” has a variety of meanings, but they can be summed up by the definition of the adjective: “pertaining to or characterized by a fixed or stationary condition”. In the software world, it generally refers to things that do not change over time. In both cases, the opposite may be “dynamic”.

In C, a variable that is stored statically is assigned an address at build time. It is, therefore, always safe to use a pointer to such a variable. There are two types of dynamic memory in C. Chunks of memory (from the heap) may be requested, typically using the malloc() function or one of its cousins (or the new operator in C++). It is safe to use pointers to data stored in this way, so long as care is taken to avoid their use after the memory is deallocated (using free() or delete). The other possibility is for variables to be stored on the stack. Again, pointers may refer to such variables, as long as they are only used when the variable is in scope.

Inside a function
If you declare a variable inside of a function, it is considered to be an “automatic”, unless you request otherwise. You can explicitly declare a variable thus using the keyword auto (but nobody ever does!). An automatic variable is stored on the stack (though optimization may result in it being allocated to a machine register, particularly if you suggest that by using the keyword register). An automatic variable remains in scope throughout the function/block in which it is declared; in C++ it is accessible for the rest of the function/block, as there is more flexibility in declaration placement.
For example:

  void fun1()
  {
    int var1;
    …
    {
      int var2;
      …

Here, both var1 and var2 are automatic and have probably been allocated space on the stack. The scope of each is different: var1 is accessible throughout fun() ; var2 is accessible only in the block in which is declared. However, if we modify the code thus:

  void fun2()
  {
    static int var3;
    …
    {
      static int var4;
      …

Now, variables var3 and var4 have been allocated addresses at build time. Their scope is the same as their counterparts in the previous example. The big difference is, because the static variables have fixed memory locations, any data they contain persists from one call of a function to another. This is the first meaning of the C keyword 'static'. It has no effect on scope and only affects the storage of a variable.

It is important to appreciate that a function with static variables is not reentrant. This means that problems will arise if the same function code is called from two separated execution threads.

14 thoughts on “The C keyword ‘static’

  1. The principle effect of the static keyword is “to hide”.
    To hide a global variable or a function in a file from all other files put static in its definition.
    Similarly, to hide a “global” variable from all other functions, put the variable inside a functio

    Log in to Reply
  2. Interesting, in C, static is implicitly true for all global variables.
    That is, for any variable defined in one file, before using it another file, the programmer must add a declaration for it in the other file.

    Log in to Reply
  3. What's more important about embedded “C” use of static variables is understanding how, at restart or power-on, the assignments to the variables is accomplished.

    E.g.
    static char bar = 'A';

    'A' must be assigned to the RAM allocation for variable, bar.

    Log in to Reply
  4. In the C++-specific use of static, I prefer to think of the static elements as belonging to the class itself rather than being shared by the objects of that class. The distinction being that these elements are still valid even when there are no objects in

    Log in to Reply
  5. Perhaps it's just me, but it looks like this article was cut off in the middle. From the introduction I thought there would be a discussion of static in 'all its guises', but after describing its use inside function definitions, the article just ends “in m

    Log in to Reply
  6. When I do technical interviews “What does the keyword static mean?” is one I often throw in. Those whose answer starts “In what context?” or “It depends on context…” tend to be the better candidates.

    Log in to Reply
  7. Got burned once by an optimizer bug involving “static” which was a devil to find:

    switch (…)
    {
    case 0:
    {
    static int foo = 0;

    break;
    }
    case 1:
    {
    static int bar = 0;

    break;
    }
    }
    This was in a message handler.

    Log in to Reply
  8. “use of “static” keyword in Embedded C/C++ ? if it is OKn case 1: where it is storing(which memory segment/section like text/data/BSS/heap/stack)?n case 2: “static” keyword, how it will work on embedded C/c++/java on RTOS,embedded Linux or L

    Log in to Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.