Question:
Can a variable be both const and volatile?
Answer:
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. Source: CoolInterview.com
The above answere refer to the const pointer. the question is about const variable. If you are using the const variable, then you shoud not combine it with volatile since volatile is supposed to change Source: CoolInterview.com
Answered by: bibin | Date: 11/25/2009
| Contact bibin
it is not possible to have a variable which is const and volatile... const means its value cant be changed,but using pointers we can access address of variable and can change its value.. volatile means its value can be changed... Source: CoolInterview.com
Answered by: Nagamani | Date: 2/3/2010
| Contact Nagamani
const means can't change by function and outside e.g. hardware also
volatile means can change by used defined function and hardware
volatile const means can change by hardware/outside but can't change by user defined function. But user defined function can use that variable without any modification
VENKATGOPU Source: CoolInterview.com
Answered by: VENKAT GOPU | Date: 2/20/2010
| Contact VENKAT GOPU
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|