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.

Showing Answers 1 - 11 of 11 Answers

kannan

  • Jul 30th, 2005
 

what is the main differences between c and c++?

malli

  • Sep 16th, 2005
 

yes it is true a variable can be cont and volitle

  Was this answer useful?  Yes

kanaga

  • May 11th, 2006
 

The major difference between c & C++ is C is a structural programming language while C++ is Object Oriented Programming language.

  Was this answer useful?  Yes

santosh

  • Jun 28th, 2006
 

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.

Pradeepa

  • Jul 31st, 2006
 

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

  Was this answer useful?  Yes

Ajay

  • Apr 17th, 2017
 

Interrupt status register should be changed by the program. For example, if status register is updated by the hardware for some kind of interrupt. In our program we read the status register to service the interrupt using some ISR, but once the servicing is done, we should change the status register, other wise if next time we get the interrupt then we may not know whether the interrupt is new or the old one which we had already serviced.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions