'static volatile' vs. 'static' vs. 'volatile' in C

35,263

Solution 1

static - in this case makes the variable visible only inside the current file

volatile - it is information for the compiler that the object can be changed by something outside the normal execution path (for example, the interrupt routine) and guarantees that the variable will be read before any use and written after every change. volatile (which is a very common misunderstanding) does not guarantee anything else - no atomicity, no cache coherency, etc., etc.

Solution 2

For the keywords static and volatile there is written enough...

See for example:

In the concern of the TWI interface, volatile is needed, because functions which modify these variables could be called from different interrupt service handlers. If volatile would be removed, the compiler will optimize code, not knowing that code can be interrupted. That may lead to failures.

Solution 3

static:

A static variable refers to a class variable that's shared among all instances.

volatile:

Volatile variables are those which are read and written to main memory. They aren't stored in local cache and are always fetched from main memory.

For example, two threads use, say, private volatile int x;. If thread A write(x) and thread B read(x) then, both the times it will write and read from main memory, without using the threads' local cache.

static volatile:

Even if the static variables are shared variables, but in different thread there can be different values for a static variable in the local cache of a thread. To make it consistent for all threads, just declare it as static volatile. So each time it will fetch from main memory.

Solution 4

Many good answers were provided here, but no mention of scope.

Static variables once initialized and later changed within a scope, they retain the changes and never be destroyed or initialized again especially when leaving the scope. Not unless dictated in code. You can say, static variables resemble global variables in terms of their lifecycle but can only be accessed throughout their own scope.

The volatile part has the tendency to force execution to fetch a variable from RAM and not the cached copy in registers or flash. Suppose for example a certain code was submitted to the compiler under certain level of optimization setting. The compiler does not assume any further conditions are attached to variables other than to clear them when they are not used or outside their scope. There are inherently dual uses for volatile, either to disregard optimization offered by the compiler for that variable, or to refrain from using the prefetched copy of that variable except for the one in RAM.

The static volatile is the combination of both behaviors, persistence of that variable in RAM beyond any optimization.

Potential areas of application:

  • Flash programming
  • Cyclical buffers
  • Ring buffers
  • Concurrency and multiprocessing/multithreading
Share:
35,263

Related videos on Youtube

R1S8K
Author by

R1S8K

I'm a basics electronics trainer, I've been a trainer for 6 years. I'm trying to learn programming in C/C++, if I got them very good I would be very happy to move to other programming languages but actually C/C++ are full of things that I need a lot of time to grasp. So yeah .. let's dive into those fields :)

Updated on April 03, 2022

Comments

  • R1S8K
    R1S8K about 2 years

    What's the difference between using the variable specifiers static volatile combined? Or using one alone; like static or volatile in microcontroller programming?

    • David C. Rankin
      David C. Rankin over 6 years
      You may want to reference C11 Standard (draft n1570) § 6.2.2 Linkages of identifiers followed by § 6.2.4 Storage durations of objects and § 6.7.1 Storage-class specifiers
    • Milind Deore
      Milind Deore over 6 years
      These are keywords, datatypes and qualifiers. If you understand them individually, would help you understand what they mean and how can they be used. May be exercise for you :)
    • R1S8K
      R1S8K almost 4 years
      @JohnBollinger thank you for valuing my question overall even it's wasn't well structured. Anyway it's been more than 2 years and I started to understand these specifiers. It's really that I won't get a deep understanding until I get into a program that need me to use them as an alternative way or a must one ! The main idea I learned is that with more advanced codes, I learn new stuff.
    • R1S8K
      R1S8K almost 4 years
      @DavidC.Rankin Thanks for the reference.
  • Dominick Pastore
    Dominick Pastore about 4 years
    This question is tagged "C". static in C has nothing to do with classes.
  • Abhinandan
    Abhinandan almost 4 years
    What does classes have anything to do with static ?? This is a bit misleading.