Q:

Can a variable be both constant and volatile in C?

0

Can a variable be both constant and volatile in C?

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Yes, we can use both constant and volatile together.  One of the great use of volatile and const keyword together is at the time of accessing the GPIO registers. In the case of GPIO, its value will be changed by the ‘external factors’ (if a switch or any output device is attached with GPIO), if it is configured as an input. In that situation, volatile plays an important role and ensures that the compiler always read the value from the GPIO address and avoid to make any assumption.

After using the volatile keyword, you will get the proper value whenever you are accessing the ports but still here is one more problem because the pointer is not const type so it might be your program change the pointing address of the pointer. So we have to create a constant pointer with a volatile keyword.

Syntax of declaration,
int volatile * const PortRegister;
How to read the above declaration,
int volatile * const PortRegister;
| | | | |
| | | | +------> PortRegister is a
| | | +-----------> constant
| | +---------------> pointer to a
| +---------------------> volatile
+---------------------------> integer

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Embedded C interview questions and answers (2022)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Can we have a volatile pointer?... >>
<< What is the difference between the const and volat...