top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Why we use volatile variables in c?

+3 votes
1,705 views
Why we use volatile variables in c?
posted Jan 21, 2014 by Prachi Agarwal

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+2 votes

Keyword volatile says that the code related to the variable in question should not be optimized i.e. the code is written on purpose and does not need optimization.
An example of declaring a simple volatile int type variable would be:

Volatile int x;
Int volatile x;
If you had a pointer variable where the memory pointed to was volatile you could indicate that using:

Volatile int * x;
Int volatile * x;
On the other hand, if you have a pointer variable where the address itself was volatile but the memory pointed to was not then we have:

Int * volatile x;
Last but certainly not least if there was a pointer variable where both the pointer address and the memory pointed to was both volatile then you could do:

Volatile int * volatile x;
Int volatile * volatile x;

answer Jan 22, 2014 by Giri Prasad
+1 vote

C’s volatile keyword is a qualifier that is applied to a variable when it is declared. It tells the compiler that the value of the variable may change at any time–without any action being taken by the code the compiler finds nearby.

By declaring a variable volatile you are effectively asking the compiler to be as inefficient as possible when it comes to reading or writing that variable. Specifically, the compiler should generate object code to perform each and every read from a volatile variable and each and every write to a volatile variable–even if you write it twice in a row or read it and ignore the result. No read or write can be skipped. Effectively no optimizations are allowed with respect to volatile variables.

The use of volatile variables also creates additional sequence points in C and C++ programs. The order of accesses of volatile variables A and B in the object code must be the same as the order of those accesses in the source code. The compiler is not allowed to reorder volatile variable accesses for any reason.

Here an examples of declarations of volatile variables:

int volatile g_flag_shared_with_isr;

This example declares a global flag that can be shared between an ISR and some other part of the code (e.g., a background processing loop in main() or an RTOS task) without fear that the compiler will optimize (i.e., “delete”) the code you write to check for asynchronous changes to the flag’s value. It is important to use volatile to declare all variables that are shared by asynchronous software entities, which is important in any kind of multithreaded programming. (Remember, though, that access to global variables shared by tasks or with an ISR must always also be controlled via a mutex or interrupt disable, respectively.)

answer Jan 21, 2014 by Amit Kumar Pandey
Similar Questions
+7 votes

Where we need to use volatile variable? If possible please provide some real time example ?

+7 votes
#include<stdio.h>

int &fun()
{
   static int x;
   return x;
}   

int main()
{
   fun() = 10;
   printf(" %d ", fun());

   return 0;
}

It is fine with c++ compiler while giving error with c compiler.

...