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

61.9k views Asked by At

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

4

There are 4 answers

0
0___________ On BEST ANSWER

static - in this case makes the variable visible only inside the current file. static object have static storage duration.

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.

0
Manfred Steiner On

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.

0
Shayban On

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
2
Akanksha gore On

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.