Volatile in C: Volatile keyword in C lets the compiler know that the variable’s value might change at any point in time due to many external conditions, and the change is not only a result of the program’s control flow. The compiler associated will not rewrite anything that has to do with volatile variables.

Volatile in C came into existence for not caching values of a variable automatically. It tells the computer system not to cache the value of this volatile variable. This generated code to obtain the value of the volatile variable from memory every time it confronts it. This mechanism is of great help because the value can be modified by the operating system or an interrupt, for that matter. Henceforth, using volatile helps in accessing the value every time it is needed.

Syntax:

The main reasons to use volatile variables:

A variable should be declared as a volatile variable only when its value changes unexpectedly.

  • To interface with the hardware that contains memory-mapped I/O registers, that is, the interface is a shared boundary between components present in a computer system. Volatile keyword helps in accessing the features by using addresses and pointers. In other words, memory-mapped peripheral registers lead to the volatile declaration.
  • Volatile is used to modify variables outside the program control flow, that is, during an interrupt service routine. An interrupt service routine is a process that is invoked by an interrupt request from a hardware device, that is, when others modify global variables.
  • Another thread running uses the same variable used by one compiler, and multiple tasks within a multi-threaded application are accessing global variables.
  • When there is a signal handler, that may change the value of the variable. A Signal handler is one of the functions called by the target environment when a similar signal occurs.
  • RTOS (Real-Time Operating System) will be working perfectly fine until some task is given, leading to some error. In such cases, volatile is used for debugging the error. RTOS is an operating system (OS) that is expected to serve real-time applications that process the data as it comes without any buffer delays.
  • Not using volatile may lead to flaky hard drives constantly leading the computer system to become slow.

Example

When the above code is compiled and run, it usually works and gives the output. But for the sake of completeness, if the compiler optimization is turned on, it throws an error by not running some part of the code snippet. When the central part is viewed, there will be no evidence that will change; so the compiler does not realize that another thread modifies the global variable, breaking in the middle of the program. If the while loop is changed like shown below, the compiler sees the sleep function inside the while loop and thinks that there might be some more code left to compile. Hence it will run smoothly without any errors.

The program’s correctness is at the whim of the compiler as the set of heuristics that are used to optimize the code. Because different compilers might have different heuristics, and it might be broken in both or might work correctly in both cases. Therefore, adding sleep is not the solution. Here we can make use of volatile by declaring it in the beginning as shown below;

Doing so, the program tells the compiler that the variable might not be apparent to the compiler. For example, it might be a thread or a signal handler or an interrupt service. The main point is when we make a variable volatile, we are indicating the compiler not to make any assumptions about when this variable can be changed.

Pin It on Pinterest

Share This