What is an volatile variable?

Answer Posted / chandrashekar kalvacherla

volatile tells the compiler that this variable can get altered by ways that the compiler cannot deduce. As a consequence, the compiler will exclude that variable from optimizations and the code will always fetch the variables content from memory, even if it has done so before (no caching).

Imagine a hardware clock which is mapped into memory. You need some way to tell the compiler: whenever I access that variable (mapped to the hardware clock), I want you to actually fetch the content from there. Otherwise the compiler might just ask the clock once for the current time and use the cached value (in a processor register) throught the rest of the program.

Is This Answer Correct ?    8 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is the use of extern in c?

639


Simplify the program segment if X = B then C ← true else C ← false

2575


Function calling procedures? and their differences? Why should one go for Call by Reference?

627


What is the explanation for cyclic nature of data types in c?

640


Explain how can you be sure that a program follows the ansi c standard?

853






How variables are declared in c?

565


What is a static variable in c?

661


Was 2000 a leap year?

620


Explain what is wrong in this statement?

630


Explain the difference between the local variable and global variable in c?

593


Why do we use main function?

629


How to throw some light on the b tree?

600


What is the code in while loop that returns the output of given code?

1294


What are c header files?

573


Is it better to use malloc() or calloc()?

643