Click here to Skip to main content
15,886,664 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
what is volatile varaible in c? give some examples ?how its work
Posted
Comments
Eugen Podsypalnikov 4-Jul-12 15:28pm    
// how its work ?
As OriginalGriff said,
the value will be read always from the address of the variable directly :)

1 solution

When you add the volatile attribute to a variable all you are doing is telling the compiler
"The value in this variable is subject to change at any time. Do not try to keep it in a register, or use a working copy of the value. Do not optimise it at all. Just leave it alone."


It is intended for use with interrupts and other such events which do not happen as part of normal execution of your program but which use particular memory to transfer information. If the compiler did not know about it, it could happily optimise
C#
while (bytesRecieved == 0)
   {
   }
into an if condition rather than a while, since bytesRecieved cannot be modified within the loop.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 5-Jul-12 0:27am    
Nice explanation, a 5.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900