const | volatile |
---|---|
The keyword “const” is enforced by the compiler and tells it that no changes can be made to the value of that object/variable during program execution. | The keyword “volatile” tells the compiler to not perform any optimization on the variables and not to assume anything about the variables against which it is declared. |
Example: const int x=20; , here if the program attempts to modify the value of x, then there would be a compiler error as there is const keyword assigned which makes the variable x non-modifiable. |
Example: volatile int x; , here the compiler is told to not assume anything regarding the variable x and avoid performing optimizations on it. Every time the compiler encounters the variable, fetch it from the memory it is assigned to. |