Volatile Qualifier in C

Filed Under: C Programming

What is Qualifiers in C?

Qualifiers in C are the keywords which are used to modify the properties of a variable.There are two types of qualifiers available in C language. They are const and volatile.

What is Const?

Constants are normal variables. Once the value of a constant is defined, it cannot be modified later by the program. They refer to fixed values. They are also called as literals.

Declaration:


const data_type variable_name;
const data_type *variable_name;

What is Volatile?

The value of a keyword defined as volatile cannot be changed explicitly by the program. But these variable values might keep on changing without any explicit assignment by the program. These types of qualifiers are called volatile.

Declaration:


volatile data_type variable_name;
volatile data_type *variable_name;

What is Volatile Qualifier in C?

Volatile Qualifier is a very important topic which needs to be understood by every embedded system programmer as well as an interview point of view. It is one of the vaguest and not clearly understood topic.

Have you ever faced this issue, when you are testing your code independently or without any compiler optimization, it works perfectly fine but as soon as you integrate it with system code or perform compiler optimization, the things start to crash and no longer provides the same output?

This might be due to the volatile qualifier. It’s not so straight forward to have a sample working C program which can easily showcase the exact effect of “volatile” keyword. You probably won’t be able to use a variable which is qualified as “volatile“ unless you’re doing some low-level hardware programming in C.

A volatile variable is one that can change unexpectedly, it means the compiler cannot make any assumption about its value.

Volatile keyword is intended to prevent the compiler to perform any optimization on that object, that can change in ways which cannot be determined by compiler.

The object can be a memory, register, SFR.

The picture shows the ways through which objects can be updated.

volatile qualifiers in C, updation of the volatile variable

OBJECT UPDATION

When to use Volatile Qualifier?

Let’s try to understand this with a simple example.

Say, we have a memory location and we need to continuously monitor it until it becomes zero.

Here is the code:


#define MEMORY_ADDRESS 0x1000U
int main(void)
{
    int value = 0;
    int*p = (int*)MEMORY_ADDRESS;
    while(1)
    {
        value = *p;
        if(value) break;
    }
    return 0;
}

We had defined memory location with macro(MEMORY_ADDRESS) and a pointer which point to this address. In the while(1) loop we are monitoring the value pointed by the pointer p. If at any point of time the value becomes non zero we will break the loop.

This simple piece of code works perfectly fine if there is no optimization done by the compiler. It will always go and read what is the value stored at the address pointed by pointer p.

Now, if you apply compiler optimization here and the value is updated by the interrupt or DMS which is not under compiler’s scope. Then compiler optimizes it by keeping a copy of the first read to avoid multiple read operation which is time-consuming. In that case, even if the value at this address changes but it will never be updated here.

To avoid such situation we need to explicitly tell the compiler that the pointer p is volatile and don’t perform any optimization on that.

By the thumb rule, anything which can change beyond the compilers scope you should declare it as “volatile”.

Consequences of over usage of volatile

Volatile variable forces compiler not to keep a copy and always get the fresh value from memory and register which takes more clock cycles. For real-time application, you should always perform timing analysis on a volatile variable to check it fulfills the timing requirement.

Now at last but not least, there is one interesting thing that we would like to share. Consider the code below:


const volatile int Variable;

Can you guess what this variable means? How it can be constant and variable at the same time?

It means it’s a volatile integer that you are not allowed to write but it can be changed by the outside world. You can use this for read-only or status register.

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages