Arm Community
Arm Community
  • Site
  • User
  • Site
  • Search
  • User
  • Groups
    • Arm Research
    • DesignStart
    • Education Hub
    • Graphics and Gaming
    • High Performance Computing
    • Innovation
    • Multimedia
    • Open Source Software and Platforms
    • Physical
    • Processors
    • Security
    • System
    • Software Tools
    • TrustZone for Armv8-M
    • 中文社区
  • Blog
    • Artificial Intelligence
    • Automotive
    • Healthcare
    • HPC
    • Infrastructure
    • Innovation
    • Internet of Things
    • Machine Learning
    • Mobile
    • Smart Homes
    • Wearables
  • Forums
    • All developer forums
    • IP Product forums
    • Tool & Software forums
    • Pelion IoT Platform
  • Activity
  • Support
    • Open a support case
    • Documentation
    • Downloads
    • Training
    • Arm Approved program
    • Arm Design Reviews
  • More
  • Cancel
System
  • Developer Community
  • IP Products
  • System
  • Jump...
  • Cancel
System
Embedded blog Using the static keyword in C
  • Blogs
  • Forums
  • Videos & Files
  • Mentions
  • Sub-Groups
  • Tags
  • Jump...
  • Cancel
  • New
More blogs in System
  • Embedded blog

  • SoC Design blog

Tags
  • Programming Languages
  • Cortex-M
  • Variable
  • Tutorial
Actions
  • RSS
  • More
  • Cancel
Related blog posts
Related forum threads

Using the static keyword in C

Jacob Beningo
Jacob Beningo
April 13, 2014

There are many topics in the C language that often confuse developers but the use of the static keyword seems to be one of the more common. One of the points of confusion is how static affects variables that are local and variables that are global. In each instance the static keyword has a different effect on where the data is stored and how it persists throughout the life of the program. There are three primary uses for the static keyword; local variable in a function, global variable in a module and a function in a module. In this post we will examine all three uses and how they affect not only where variables are stored but also the use of static can increase code quality.

A definition of static

In general, static is a storage class specifier that can be applied to any data type. While static has many definitions, the definition that best applies to all three uses is that static tells the compiler to make the variable or function limited in scope while allowing it to persist throughout the life of the program. This allows static to be used to encapsulate or hide variables from the rest of the program to prevent inadvertent access. The developer is then able to strictly control how variables are accessed within a module which is considered good programming practice.

The variable

When a variable is declared within a function without the use of static, the variable is considered to be an automatic variable. Automatic variables are created and stored on the stack (or within a CPU register) and destroyed when the function returns. If a developer wanted the variable within the function to retain its value between calls, the variable would be declared as static. In this case, the variable would no longer be stored on the stack but would instead be stored in the global memory space; however, even though the variable itself is stored in global space the compiler enforces a local scope on the variable causing it to only be visible within that function!  Other functions within the module will be unaware that the variable exists. Not only will the variable retain its value throughout the life of the program, the static variable will also only be initialized the first time the function is called.

Defining a variable within the global scope of a module (outside of any function) implicitly declares the variable as extern. This causes the variable to be defined within the global memory space and for the linker to link other extern definitions to it. If the variable is only going to be used within the current module then it should be explicitly declared with static. Good programming practice indicates that a variable should be declared within the most local, applicable scope. Therefore, if the variable is only used within a single function then the more appropriate location to declare the variable may be within the function itself rather than the module scope.  Static variables declared at the module level are initialized only once during the C copy down that occurs when the processor is being initialized.

Function within a module

Static can also be applied to a function within a module  By default, functions are implicitly declared as extern. This means that if a function is defined within a c file and not prototyped within a header file, the compiler will still be able to link to the function (with perhaps a few warnings to the developer). In order to only make a function usable within a single module, the developer can place the static keyword before the function declaration.  This will effectively hide the function from the external world and protect the use of that function and its variables.

Summary

The use of the static keyword has many uses. Its most effective use is to limit the scope of variables defined within a module or function. Through the use of static, variables and functions can be hidden from the external program, resulting in behavior that is similar to the use of private and public in more modern object oriented languages.

Anonymous
  • Jacob Beningo
    Offline Jacob Beningo over 6 years ago

    Thanks for the feedback.  I also want to thank Peter Wilson who pointed this out.  I've made a minor update to correct.

    • Cancel
    • Up 0 Down
    • Reply
    • More
    • Cancel
  • Anand Shastry
    Offline Anand Shastry over 6 years ago

    Nice explanation . Static variables are stored in the data segment of the memory.

    It would be helpful, if you explain const and volatile also

    • Cancel
    • Up 0 Down
    • Reply
    • More
    • Cancel
  • bandit gangwere
    Offline bandit gangwere over 6 years ago

    "Defining a variable within the global scope of a module (but not global within the context of the entire program) implicitly declares the variable to be static. " .... Umm, no.

    foo.c

    int bletch; // any function on foo.c can see bletch

    bar.c

    extern int foo; // any function in bar.c can see bletch

    Now, if you have

    foo.c

    static int bletch;

    no other file can see bletch using an "extern int bletch;" - the linker cannot see bletch because foo.c does not expose bletch to the linker. The variable must be declared static to keep it local to the file. The interesting thing is when you have foo.c as above ("static int bletch;") and

    bar.c

    int bletch;

    baz.c

    extern int bletch;

    the linker CAN match up "bletch" in bar.c and baz.c In fact, if baz.c does not use the 'extern', the linker is still likely to match 'bletch' in bar.c and baz.c - that is implementation dependent - you may or may not get a warning.

    To clarify another point: " Not only will the variable retain its value throughout the life of the program, the static variable will also only be initialized the first time the function is called." Assume

    void foo()

    {

      static int bar = 5;

    }

    The first time foo() is called, bar is set to 5. Correct so far. However, if the function is declared as:

    void foo( int new_bar )

    {

    static int bar = 5;

    if( new_bar > 0 )

        bar = new_bar;

    }

    or something similar, 'bar' can be set to anything inside the function at any time the function executes.

    (BTW - I am impressed - the spell checker does not like baz but it does like bletch...)

    • Cancel
    • Up 0 Down
    • Reply
    • More
    • Cancel
<
Embedded blog
  • Embedded blog: Development of Arm based systems with Synopsys Virtual Prototyping: Anytime, Anywhere!

    Jason Andrews
    Jason Andrews
    Find out more about virtual prototyping from Synopsys as thousands of engineers around the world have been asked to work from home.
    • May 5, 2020
  • Embedded blog: Accelerating Innovation for Safety Systems with Arm Flexible Access

    Chet Babla
    Chet Babla
    Arm is proud to announce the addition of Arm Safety Packages for select IP within Arm Flexible Access, with the goal to make it easier for developers in the automotive and other safety-related industries…
    • February 19, 2020
  • Embedded blog: Functional Safety on Arm – Beyond Automotive

    Antonio Priore
    Antonio Priore
    Arm has moved a long way from the days where our only ‘safety’ requirement was to provide ECC or parity protections on CPU RAMs and we are now able to offer a number of System IP, GPUs, ISPs and NPUs as…
    • December 13, 2019