How to Use the Volatile Keyword in C? Last Updated : 28 Jun, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In C, the volatile keyword is used to inform the compiler that the value of a variable may change at any time, without any action being taken by the code in the program. It is particularly useful in embedded systems, signal handlers, and multi-threaded applications. In this article, we will learn how to use the volatile keyword in C.Volatile Keyword in CWe can use the volatile keyword for different purposes like declaring some global variables, signal handlers, variables across shared threads, etc. When a variable is declared as volatile, it tells the compiler:The compiler must not assume the value of the variable remains constant between accesses, so it should read the value from memory every time it is used.The compiler must not reorder instructions in a way that changes the access order of the volatile variable.Syntax to Use Volatile Qualifier in Cvolatile dataType varName;C Program to Demonstrate the Use of Volatile KeywordThe below program demonstrates the use of volatile keyword in C. C // C Program to Show how to use the Volatile Keyword #include <pthread.h> #include <stdio.h> // Volatile variable to be accessed by multiple threads volatile int volVar = 0; // Mutex for synchronization pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER; // Function to increment the volatile variable void* incValue(void* arg) { for (int i = 0; i < 10; i++) { // Lock the mutex before accessing volVar pthread_mutex_lock(&mtx); volVar++; // Unlock the mutex after modifying volVar pthread_mutex_unlock(&mtx); } return NULL; } int main() { pthread_t t1, t2; // Create two threads to increment volVar pthread_create(&t1, NULL, incValue, NULL); pthread_create(&t2, NULL, incValue, NULL); // Wait for both threads to finish pthread_join(t1, NULL); pthread_join(t2, NULL); // Output the final value of volVar printf("Final value of volVar: %d\n", volVar); return 0; } OutputFinal value of volVar: 20 Comment More infoAdvertise with us Next Article C Program to Show Thread Interface and Memory Consistency Errors A akshitsaxenaa09 Follow Improve Article Tags : C Programs C Language C-Storage Classes and Type Qualifiers C Examples Similar Reads Interesting Facts in C Programming | Set 2 Below are some more interesting facts about C programming: 1. Macros can have unbalanced braces: When we use #define for a constant, the preprocessor produces a C program where the defined constant is searched and matching tokens are replaced with the given expression. Example:C #include <stdio.h 2 min read Thread Management Functions in C In C language, POSIX <pthread.h> standard API (Application program Interface) for all thread related functions. It allows us to create multiple threads for concurrent process flows.To execute the C programs that uses these functions, we may have to use the -pthread or -lpthread in the command 7 min read C Program to Show Thread Interface and Memory Consistency Errors Thread interface and Memory consistency errors are demonstrated in this program. Threads are a way for a program to split itself into two or more concurrently running tasks. This means that a program can perform multiple operations at the same time, rather than having to execute them one after the o 10 min read Parallel Programming in C Parallel programming is a technique that allows multiple computations to be performed simultaneously, taking advantage of multi-core processors and distributed computing systems. Parallel programming can improve the system's performance by dividing the bigger task into smaller chunks and executing t 5 min read dos.h header in C with examples dos.h is a header file of C Language. This library has functions that are used for handling interrupts, producing sound, date and time functions, etc. It is Borland specific and works in compilers like Turbo C Compiler. Below are the functions supported by this library: delay(): The delay() function 4 min read How to Use the Volatile Keyword in C++? In C++, the volatile keyword is used to tell the compiler that the value of the variable declared using volatile may change at any time. In this article, we will learn how to use the volatile keyword in C++. Volatile Keyword in C++We can use the volatile keyword for different purposes like declaring 2 min read Like