C Keywords – Review


Static keyword can be used for both variables and functions.

By default, functions and global variables defined in a file are available for use in other files. However, if the static keyword is used, they are restricted to the current file only. They will not be available in other files even if the extern keyword is used.

Local variables by default will be unavailable once the program leaves the scope in which it was defined. If the static keyword is used with a local variable, the local variable is preserved even when the program leaves the scope in which it was defined. This is commonly used to preserve local variable values between successive function calls. Static variables are initialized during compile time.

Continue Reading Below


Volatile indicates that the variable can be changed in unpredictable ways. A volatile variable has to be retrieved from memory each time it is used rather using a copy if it is available in a register or cache.

int itr_rx; // Interrupt Address
while( !itr_rx ) {
    // Do something

In this case, if the optimization is turned on, the compiler will convert this to an infinite while loop since the value of itr_rx could not have changed due to the program flow. The compiler doesn’t know that the variable is an interrupt address so it will treat it like a normal variable. We don’t want that, do we?

volatile int itr_rx; // Interrupt Address
while( !itr_rx ) {
    // Do something

If the variable is declared as volatile, it will be reloaded from memory each time. It will not be optimized into an infinite while loop. It can be used for peripheral addresses and variables which are used to synchronize multiple threads.


const int a;

The value of a can not be changed. It will generate a compile error if you try to do it. However, there is a way you can change it but it is not recommended.

*(int *) &a = 123;
const int * b;

The address to which the pointer is pointing to can not be changed however the value in that address can be changed.

int * const b;

The value in the address, b is pointing to, can not be changed.


Leave a Reply

Using Gravatars in the comments - get your own and be recognized!

XHTML: These are some of the tags you can use: <a href=""> <b> <blockquote> <code> <em> <i> <strike> <strong>