EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Volatile in C

Home » Software Development » Software Development Tutorials » C Programming Tutorial » Volatile in C

Volatile in C

Introduction to Volatile in C

A volatile keyword in C is nothing but a qualifier that is used by the programmer when they declare a variable in source code. It is used to inform the compiler that the variable value can be changed any time without any task given by the source code. Volatile is usually applied to a variable when we are declaring it. The main reason behind using volatile keyword is that it is used to prevent optimizations on objects in our source code. Therefore, an object declared as volatile can’t be optimized because its value can be easily changed by the code. As we have seen what is Volatile in C. Similarly, we will see the syntax used to represent a volatile object in code. But keep in mind that the value of volatile keyword can’t be changed by the program explicitly.

Syntax

volatile data_type variable_name ;
volatile data_type  *variable_name ;

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Explanation: In the above declaration volatile keyword is mandatory to be used then data_type means any data type it can be wither integer, float, or double. Finally, the name of the variable as per our choice. As both the declarations are correct we can use any of the above to declare a volatile variable.

For Example :

volatile int x ;
volatile int *a;

How Does Volatile Keyword work in C?

Now let’s see how does a volatile keyword works in C programming code through some coding examples with a brief explanation. In the below two codes we will see how does the program changes when we use volatile keyword in declaring a variable as compared to the non-volatile keyword. We will see how the efficiency of the code changes when we use volatile and how quickly we can apply this functionality in our code. Volatile is used in C programming when we need to go and read the value stored by the pointer at the address pointed by the pointer. If you need to change anything in your code that is out of compiler reach you can use this volatile keyword before the variable for which you want to change the value.

Examples to Implement Volatile in C

Here is the sample code to demonstrate the working of volatile keyword:

Example #1

Without using keyword Volatile:

Code:

#include<stdio.h> // C header file for standard input and output
int a = 0 ; // initilaizing and declaring the integer a to value 0.
int main ()  // main class
{
if ( a == 0 )  //  This condition will be true
{
printf ( " a = 0  \n " ) ;
}
else                        // Else part will be optimized
{
printf ( " a ! = 0  \n " ) ;
}
return 0 ; // returning value
}

Output:

Without using keyword Volatile

Explanation: In the above code, we have declared an integer variable with value 0 assigned to it. Then in the main class, we have set the if condition which will hold true until and unless the value of variable a is 0. As you can see the output will always be 0 as the condition will always remain true so that that code won’t move to the else part as it will ignore the else part. But things will change when we will add keyword volatile to the declaration of integer variable a. Let’s have a look at the other code.

Popular Course in this category
C Programming Training (3 Courses, 5 Project)3 Online Courses | 5 Hands-on Projects | 34+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,617 ratings)
Course Price

View Course

Related Courses
C++ Training (4 Courses, 5 Projects, 4 Quizzes)Java Training (40 Courses, 29 Projects, 4 Quizzes)

Example #2

With using keyword Volatile:

Code:

#include<stdio.h>
volatile int a ;    /* volatile Keyword used before declaration of integer variable a */
int main() // main class
{
a = 0 ;   // initializing the integer value to 0
if (a == 0)  // applying if condition
{
printf ( " a = 0 \n " ) ;
}
else// Now compiler never optimize else part because the variable is declared as volatile
{
printf ( " a ! = 0  \n " ) ;
}
return 0 ;
}

Output:

integer variable

Explanation: In the above code, we have declared a volatile integer variable a. Then in the main class, we have set two things one is the value of integer variable is 0 and second is the if condition which will hold true until and unless the value of variable a is 0. As you can see the output will always be 0 as the condition will always remain true because the variable is declared as volatile. Therefore, the compiler won’t optimize the else part of code because of the volatile keyword used before integer. So the compiler will know that the variable can change anytime.hence, it will read the else part as the final executable code and display the result.

Example #3

Here is another C programming code to demonstrate the working of volatile keyword in C:

Code:

#include <stdio.h>
int main (void)                 // main class declaration in the code
{
const volatile int local_value = 25 ; // declaring constant volatile integer variable with assigned value
int *ptr = ( int* ) &local_value ;
printf ( " The initial value of the local_value is  : %d \n ", local_value ) ;
*ptr = 195 ;  // value to the pointer
printf ( " The modified value of the local_value is: %d \n ", local_value ) ;
return 0 ;
}

Output:

working of volatile keyword

Explanation: In the above code, you can see we have declared a constant volatile variable of an integer data type with name local_value and we allocated the value 25 to it. Then we have declared the pointer of integer data type in which we are storing the address value of “local_value”. In addition, we are printing the old value then we are printing the modified value on the screen. This modification is possible only because of the volatile keyword we have used in the declaration of the variable.

Conclusion

volatile plays an important role in C programming as the compiler can’t guess about the value. The main reason behind using volatile is that it can change value any time a user wants it to be changed or when another thread is running but using the same variable.

Recommended Articles

This is a guide to Volatile in C. Here we discuss an introduction to Volatile in C along with syntax, working and respective examples for better understanding. You can also go through our other related articles to learn more –

  1. Infinite Loop in C 
  2. C Keywords
  3. Hexadecimal in C 
  4. Java Identifiers

C Programming Training (3 Courses, 5 Project)

3 Online Courses

5 Hands-on Projects

34+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
C Programming Tutorial
  • Advanced
    • Constructor in C
    • Encapsulation in C
    • C Storage Classes
    • Static Keyword in C
    • File Handling in C
    • Queue in C
    • Hexadecimal in C 
    • typedef in C
    • Memory Allocation in C
    • Linked List in C
    • Volatile in C
    • Tokens in C
    • Expression in C
    • Regular Expression in C
    • Error Handling in C
    • Types of Errors in C
    • Preprocessor in C
    • Preprocessor Directives in C
    • fscanf() in C
    • #Pragma in C
    • #ifndef in C
    • #undef in C
    • Macros in C
  • Basic
    • Introduction to C
    • What is C
    • Career in C Programming
    • Advantages of C
    • How to Install C
    • Best C Compilers
    • Data Types in C
    • Variables in C
    • C Keywords
    • C Command
    • Command Line Arguments in C
    • C Literals
    • Constants in C
    • Unsigned Int in C
    • String in C
  • Pointers
    • Pointers in C
    • Null pointer in C
    • Function Pointer in C
    • Double Pointer in C
    • Void Pointer in C
    • Const Pointer in C
    • Dangling Pointers in C
    • Pointer Arithmetic in C
  • Operators
    • C Operators
    • Arithmetic Operators in C
    • Relational Operators in C
    • Assignment Operators in C
    • Logical Operators in C
    • Conditional Operator in C
    • Modulus Operator in C
    • Ternary Operator in C
    • Address Operator in C
    • Unary Operator in C
    • Operators Precedence in C
    • Left Shift Operator in C
  • Control Statement
    • Control Statements in C
    • If Statement in C
    • If-else Statement in C
    • Else if Statement in C
    • Nested if Statement in C
    • #else in C
    • Structure Padding in C
    • Nested Structure in C
    • Continue Statement in C
    • Break Statement in C
    • Switch Statement in C
    • Goto Statement in C
  • Loops
    • Loops in C
    • For Loop in C
    • While Loop in C
    • Do While Loop in C
    • Nested Loop in C
    • Infinite Loop in C 
  • Function
    • Math Functions in C
    • Hashing Function in C
    • Recursive Function in C
    • Power Function in C
    • fputs in C
    • C puts() Function
    • fprintf() in C
    • fseek() in C
    • Stderr in C
    • ASCII Value in C
    • strcat() in C
    • Inline Function in C
    • sizeof() in C
    • Function Prototype in C
    • C ftell()
  • Array
    • Arrays in C Programming
    • 2-D Arrays in C
    • 3D Arrays in C
    • Multidimensional Array in C
    • Array Functions in C
    • Strings Array in C
  • Sorting
    • Sorting in C
    • Heap Sort in C
  • C programs
    • Patterns in C Programming
    • Star Patterns in C
    • Number Patterns in C
    • Swapping in C
    • Reverse Number in C
    • Palindrome in C Program
    • Factorial in C
    • Fibonacci Series in C
    • Square Root in C
    • Random Number Generator in C
    • Prime Numbers in C
    • Escape Sequence in C
    • Reverse String in C
    • Leap Year Program in C
    • Anagram Program in C
    • Strong Number in C
    • String Concatenation in C
    • C Programming Matrix Multiplication
    • Decimal to Octal in C
    • Expression Evaluation in C
    • Decimal to Hexadecimal in C
  • Interview question
    • C Programming Interview Questions

Related Courses

C Programming Training Course

C++ Training Course

Java Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - C Programming Training (3 Courses, 5 Project) Learn More