This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

difference between volatile and const

I would like to know the difference between volatile and const.Pls. explain it with an example.

Parents
  • There's no easy way to make a direct comparison between volatile and const, although I suppose in some sense they are exact opposites.

    Modifying a variable definition with const means that the compiler will flag as an error any attempt to directly modify it later in the code. So, something like this:

    void somefunc(void) {
       const int x = 5;
       x = 7;
    }
    

    will generate an error on compilation since you're trying to modify a value you've told the compiler will remain constant.

    The volatile keyword is especially important in interrupt-driven and multithreaded applications. It tells the compiler to always fetch the latest value of a variable from memory, and never assume that it won't change. It really has alot to do with compiler optimizations. For instance, if I have something like this:

    int somefunc(void) {
       int y;
    
       y = 7;
       return (y * 2);
    }
    

    an optimizing compiler could rightfully make some very easy assumptions and just return the value 14 all the time without any calculation, etc. On the other hand, if I declare y as "volatile" then the compiler will generate code to load the latest value from memory and do the calculation. This is because I'm telling the compiler "something else might change this value in between when I set it to 7 and when the function returns."

Reply
  • There's no easy way to make a direct comparison between volatile and const, although I suppose in some sense they are exact opposites.

    Modifying a variable definition with const means that the compiler will flag as an error any attempt to directly modify it later in the code. So, something like this:

    void somefunc(void) {
       const int x = 5;
       x = 7;
    }
    

    will generate an error on compilation since you're trying to modify a value you've told the compiler will remain constant.

    The volatile keyword is especially important in interrupt-driven and multithreaded applications. It tells the compiler to always fetch the latest value of a variable from memory, and never assume that it won't change. It really has alot to do with compiler optimizations. For instance, if I have something like this:

    int somefunc(void) {
       int y;
    
       y = 7;
       return (y * 2);
    }
    

    an optimizing compiler could rightfully make some very easy assumptions and just return the value 14 all the time without any calculation, etc. On the other hand, if I declare y as "volatile" then the compiler will generate code to load the latest value from memory and do the calculation. This is because I'm telling the compiler "something else might change this value in between when I set it to 7 and when the function returns."

Children