tpat used Ask the Experts™
Can anybody explain me static and volatile keywords in C, and when to use them.

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Static can be applied to a variable to expand it's scope beyond a single routine.
It forces file allocation from the heap instead of the stack, so the variable will persist beyond the execution of the routine. The programmer must assign an initial value, and at the next pass through the routine it will retain its last value. It is also available to reference throughout all routines in a single module (file scope).

Volatile can be applied to a variable to imply its value may change asynchronously (as in a multi-threaded environment). This warns the compiler that it can't generate code that assumes a value just because it hasn't changed locally. Usually, it restricts code optimizers that might make erroneous assumptions about values based only on the code it "sees."
Top Expert 2009

The part of n2fc's post on the 'static' keyword is not entirely accurate/complete.

The 'static' keyword can be used for two different purposes, neither of which change the scope of the symbol the keyword is applied to.
The first usage of the 'static' keyword, is when it is used with a file-scope identifier. In that case, the identifier has internal linkage (ie. it can only be accessed from the compilation unit where it has been defined).
The second usage of the 'static' keyword, is when it is used for an object. In that case, the identifier has static storage duration (ie. its lifetime is the entire execution of the program).

The 'volatile' keyword is a type qualifier that indicates to the compiler that the object can be changed in ways unknown to the compiler (and outside of its control). It prevents the compiler from making certain assumptions (for optimizations eg.) that would get in the way of these external modifications.
As Infinity08 mentions, the 'static' keyword is overloaded to have different meanings.  In one use, 'static' controls scope, in the other use, it controls storage class.  

When applied to a variable at file scope, outside of a function, its use is to change the scope of the variable to limit visibility to only the file in which it is defined.  Variables defined at file scope without 'static' are visible to all files in the program.  All variables defined at file scope have static storage, that is, the are stored with the program data and are initialized only once.  

When applied to a variable within a function, 'static' changes the storage class from auto, which is allocated on the stack and initialized each time the function is entered, to static, where it is defined with the program data and initialized only once.  

To expand Infintiy08's description of volatile:  it requires the compiler to generate code for each read and write of a volatile variable in exactly the same order as they appear in the program.  It's most common use is to read/write variables which are either stored in hardware which might change, such as the value of a clock register, or variables which are accessed by different threads.  
Top Expert 2009

>>  In one use, 'static' controls scope

It never controls the scope. Which was kind of the point I was trying to make in my previous post ;) It can control the linkage and the storage duration, but that's it.
Top Expert 2009

Since eager's post was expanding on what I said, it would have been nice if my post was included in the PAQ too. Moreover, the accepted post is not 100% accurate, as my previous post explains.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial