26 Comments

Comments RSS
  1. jigar

    wov…i have experience of 2 years in embedded still the concept of Volatile Variable in C language is not clear in my mind….thanks for help….

    • Rasmi Ranjan Nayak

      @Jigar: You are welcome. I am glad that, the article helped you in understanding the concept.

  2. Lawerence Leverance

    My spouse and i still can’t quite feel that I could be one of those reading through the important suggestions found on your site. My family and I are truly thankful for the generosity and for offering me the possibility to pursue my personal chosen profession path. Thanks for the important information I got from your blog.

    • Rasmi Ranjan Nayak

      Lawerence Thanks A Lot from bottom of my heart……… I am glad that, my article did help you.

    • Rasmi Ranjan Nayak

      Lawerence Thanks A Lot from bottom of my heart……… I am glad that, my article did help you.

  3. Erik Bawany

    I’m pleased I found this article. Your information is thought-provoking and most interesting. I agree with the points you have made here in this writing.

  4. Nidhi

    This article is really helpful.i didnt get such a nice description on this Topic.. Thanks a lot..

  5. Abhishek

    How does volatile intialise the memory. Plz any tell me about intialising the memory

    • Rasmi Ranjan Nayak

      Hello Abhishek,
      As far as my knowledge when a variable is declared as volatile (to prevent aggressive compiler optimization) a special memory location is reserved.
      Suppose,
      int a = 0; //Suppose a is stored in location 0x2000
      abc:
      if(a == 0)
      //Do something
      else
      //Do Nothing
      goto abc;
      In above case compiler will check the value only once and makes sure that it will never ever gonna change then else part will be optimized aggressively.
      _____
      Next,

      volatile int a = 0; //Suppose a is stored in location 0x2000
      int b = 0;
      abc:
      if(a == 0)
      //Do something
      b = a * a;
      else
      //Do Nothing
      goto abc;
      Every time compiler will go to location 0x2000 and check what is the value of ‘a’ and then it will proceed. In the above, if the “if-condition” satisfies then when control reaches to the statement
      b = a (/*1st a*/) * a (/*2nd a*/);
      compiler will go to 0x2000 location and will check the the value of a twice.
      Because (1st a != 2nd a) // Might be possible.
      Suppose if 1st a = 0, before executing 2nd a suppose the value of a got changed by some external interrupt.

      I think the above helps you. If anymore question then reply me

  6. ReddyKiran

    Master… your Article on “Volatile” is really good…thank you!! 🙂

  7. Venkat

    This artical is very helpful for me. thanks alot

  8. Ali

    hi can u plz give me the ans what is the
    use of volatile and static together in coding

    • Rasmi Ranjan Nayak

      Hello Mr. Khan,
      Actually I could not able to understand ur question properly.
      But still I will ans what I understood..
      Why volatile:
      Short Ans:-Where you have don’t have faith of value of a volatile variable. (Read Article once again).
      Why Static:
      Short Ans:- When you want your variable needs to be initialized once then you need static.
      See the example,
      main(){/*Pseudo code*/
      static int x;//x = 0 by default
      printf(“%d\n”, x++);
      main();/*It is a bad coding practice to call main()*/
      /*This example main() is called, only for understanding purpose*/
      }

      You can visit
      http://stackoverflow.com/questions/3509219/does-the-order-of-static-and-volatile-keywords-matter

  9. Tejbir

    Thanx a lot sir very very helpful

  10. Mani

    I understood the the concept of volatile . I want to get clear one more doubt from you that.

    We know that Structure and Union are almost same . But why we are not using Union ??????

    Please answer this.

    • Rasmi Ranjan Nayak

      Hi Mani, Glad this article helped u in understning the concept.
      We do use “Union” in our programs.
      take an example like,
      union{
      struct{
      int a:1;
      int b:1;
      int c:1;
      int d:1;
      }
      int total;
      };
      In this case if you assign total = 0xF;
      then a, b, c,d all will hold “1”.
      So, here you can use a, b, etc according to your usage in real time.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: