Co-Authored By:
Asked by: Shiyong Ndiaye
technology and computing programming languagesCan we use static and volatile together in C?
Keeping this in view, can we use static and volatile together?
Even if you access a static value throughmultiple threads, each thread can have its local cachedcopy! To avoid this you can declare the variable asstatic volatile and this will force the thread toread each time the global value. However, volatile is not asubstitute for proper synchronisation!
Similarly one may ask, what does static volatile mean in C?
static refers to the scope of the variable. Ifthe variable is global, it means that the scope is limited to thesource file it was declared in. If the variable is local to afunction, then it means the memory used to hold this variable is inthe application's statically allocated memory.
Yes. A variable can be declared as bothvolatile and constant in C. Constmodifier does not allow changing the value of the variable byinternal program. But, it does not mean that value of constvariable should not be changed by external code.