const means that the variable cannot be changed by c, but cannot be changed. This means that no command can write a variable, but its value can still change.
volatile means that a variable can change at any time, and thus cached values ββcannot be used; every access to the variable must be done at the memory address.
Since the question is marked as βinlineβ, and suppose temp is a user-declared variable and not hardware-related (since they are usually processed in a separate .h file), consider:
An integrated processor that has both volatile data memory for reading and writing, and non-volatile data memory for reading only, for example, FLASH memory in the von Neumann architecture, where the data and program space share a common data and address bus.
If you declare a const temp value (at least if it is different from 0), the compiler will assign a variable to an address in the FLASH space, because even if it was assigned to a RAM address, it still needs FLASH memory to save the initial value of the variable. making the RAM address a waste of space, since all operations are read-only.
As a result:
int temp; - this is a variable stored in RAM, initialized to 0 at startup (cstart), cached values ββcan be used.
const int temp; is a variable stored in (read-ony) FLASH, initialized to 0 at compiler time, cached values ββcan be used.
volatile int temp; - this is a variable stored in RAM, initialized to 0 at startup (cstart), cached values ββwill NOT be used.
const volatile int temp; is a variable stored in (read-ony) FLASH, initialized to 0 at compiler time, cached values ββwill NOT be used
Here is the helpful part:
Currently, most embedded processors have the ability to make changes to their non-volatile read-only memory using a special function module, in which case const int temp can be changed at run time, and not directly. On the other hand, a function can change the value at the address where temp is stored.
A practical example would be to use temp for the serial number of a device. At the first start of the built-in processor, temp will be 0 (or the declared value), and the function can use this fact to run the test during production and, if necessary, ask to assign a serial number and change the value from temp using a special function. Some processors have a special address range with OTP (one-time programmable) memory.
But here comes the difference:
If const int temp is a mutable identifier instead of a one-time programmable serial number and Volatile is NOT declared, the cached value can be used until the next boot, that is, the new identifier may be invalid until the next reboot or, even worse, some functions may use the new value, while others may use the older cached value before reloading. If const int temp declared voltaile , the change of identifier takes effect immediately.