GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Programming  >  C
Go To First  |  Previous Question  |  Next Question 
 C  |  Question 9 of 453    Print  
Can a variable be both const and volatile?
Yes. The const modifier means that this code cannot change the value of the variable, but that does not mean that the value cannot be changed by means outside this code. For instance, in the example in 
FAQ 8, the timer structure was accessed through a volatile const pointer. The function itself did not change the value of the timer, so it was declared const. However, the value was changed by hardware on the computer, so it was declared volatile. If a variable is both const and volatile, the two modifiers can appear in either order.



  
Total Answers and Comments: 5 Last Update: July 31, 2006   
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: santosh
 
This is possible and mostly used in embedded system.The example is Interrupt Status Register.As it is a status register , in the program we should not modify this variable.So it should be a constant.But this variable can be changed by the processor or hardware based on the interrupt condition.So when in the program ,we want to read the value of this varible , it should read the actual value with out any optimisation.For this reason ,the variable can be declared as volatile too.

Above answer was rated as good by the following members:
harishpandeymail, ramyaram84
July 30, 2005 03:11:36   #1  
kannan        

RE: Can a variable be both const and volatile?
what is the main differences between c and c++?
 
Is this answer useful? Yes | No
September 16, 2005 04:26:13   #2  
malli        

RE: Can a variable be both const and...
yes it is true a variable can be cont and volitle
 
Is this answer useful? Yes | No
May 11, 2006 02:28:57   #3  
kanaga        

RE: What's th main diff btn c & c++
The major difference between c & C++ is C is a structural programming language while C++ is Object Oriented Programming language.
 
Is this answer useful? Yes | No
June 28, 2006 06:43:16   #4  
santosh        

RE: Can a variable be both const and...
This is possible and mostly used in embedded system.The example is Interrupt Status Register.As it is a status register in the program we should not modify this variable.So it should be a constant.But this variable can be changed by the processor or hardware based on the interrupt condition.So when in the program we want to read the value of this varible it should read the actual value with out any optimisation.For this reason the variable can be declared as volatile too.
 
Is this answer useful? Yes | NoAnswer is useful 2   Answer is not useful 0Overall Rating: +2    
July 31, 2006 06:37:25   #5  
Pradeepa        

RE: Can a variable be both const and...
But isnt like the constant variables will be stored by in ROM (read only memory).Then how can the some process other than the code can change the value of const volatile
 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape