Variables and Pointers Volatile Or Constant - Embedded C Programming Frequently Asked Interview Questions
http://ingenuitydias.blogspot.com/2013/03/variables-and-pointers-volatile-or.html
A. What are volatile variables? Where we should use?
A volatile variable is one that can change unexpectedly. Consequently, the compiler can make no assumptions about the value of the variable. In particular, the optimizer must be careful to reload the variable every time it is used instead of holding a copy in a register. Examples of volatile variables are:
Hardware registers in peripherals (for example, status registers)
Non-automatic variables referenced within an interrupt service routine
Variables shared by multiple tasks in a multi-threaded applications.
More Embedded C Programming Questions And Suitable Answers:
Difference Between Process and Thread
What happens When Interrupt Occurs
Differences between Mutex And Semaphore
Unions-Why And When To Use
What is Storage class
Embedded C Basic Questions
C Programming Questions for Job Interview
Embedded C Programming Basic Questions for Job Interview
Data Declarations And qualifiers
Dangling and Wild Pointers
B. What does the keyword const mean? What do the following declarations mean?
const int a;
int const a;
const int *a;
int * const a;
int const * a const;
The first two mean the same thing, namely a is a const (read-only) integer.
The third means a is a pointer to a const integer (that is, the integer isn’t modifiable, but the pointer is).
The fourth declares a to be a const pointer to an integer (that is, the integer pointed to by a is modifiable,but the pointer is not).
The final declaration declares a to be a const pointer to a const integer
(that is, neither the integer pointed to by a, nor the pointer itself may be modified).
C. Can a parameter be both const and volatile ? Explain.
Yes. An example is a read-only status register. It is volatile because it can change unexpectedly. It is const because the program should not attempt to modify it
D. Can a pointer be volatile ? Explain.
Yes, although this is not very common. An example is when an interrupt service routine modifies a pointer to a buffer
A volatile variable is one that can change unexpectedly. Consequently, the compiler can make no assumptions about the value of the variable. In particular, the optimizer must be careful to reload the variable every time it is used instead of holding a copy in a register. Examples of volatile variables are:
Hardware registers in peripherals (for example, status registers)
Non-automatic variables referenced within an interrupt service routine
Variables shared by multiple tasks in a multi-threaded applications.
More Embedded C Programming Questions And Suitable Answers:
Difference Between Process and Thread
What happens When Interrupt Occurs
Differences between Mutex And Semaphore
Unions-Why And When To Use
What is Storage class
Embedded C Basic Questions
C Programming Questions for Job Interview
Embedded C Programming Basic Questions for Job Interview
Data Declarations And qualifiers
Dangling and Wild Pointers
B. What does the keyword const mean? What do the following declarations mean?
const int a;
int const a;
const int *a;
int * const a;
int const * a const;
The first two mean the same thing, namely a is a const (read-only) integer.
The third means a is a pointer to a const integer (that is, the integer isn’t modifiable, but the pointer is).
The fourth declares a to be a const pointer to an integer (that is, the integer pointed to by a is modifiable,but the pointer is not).
The final declaration declares a to be a const pointer to a const integer
(that is, neither the integer pointed to by a, nor the pointer itself may be modified).
C. Can a parameter be both const and volatile ? Explain.
Yes. An example is a read-only status register. It is volatile because it can change unexpectedly. It is const because the program should not attempt to modify it
D. Can a pointer be volatile ? Explain.
Yes, although this is not very common. An example is when an interrupt service routine modifies a pointer to a buffer