Thread: Difference Betn Volatile,static,extern variables

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    28

    Smile Difference Betn Volatile,static,extern variables

    hi,
    Can somebody explain me exact difference between volatile ,static and extern variables.
    when to use these variables?I know use of extern but dont know diff betn extern and volatile.
    please give me a small example which is using volatile variable.

    Thank You,
    Nitin

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    What does your C book have to say about them?

    static and extern are to do with scope.
    - extern - somewhere else
    - static - local to this module

    volatile and const are to do with how the value can be changed.
    - const means diagnose attempts to modify the variable
    - volatile means that the compiler should not assume the variable maintains it's value between reads of that variable.

    > please give me a small example which is using volatile variable.
    extern const volatile int clock;
    Now you tell me what you think that means.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    28
    extern const volatile int clock;
    this means clock is defined as volatile somewhere else i.e in other source and its value could get changed by some external process(program) whose control is not under our program.

    The const volatile keyword is confusing me,please give me explaination,if my above description is wrong.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well if volatile wasn't there, would you know what const means?

    It means the same thing if you put volatile in there.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 09-30-2008, 02:12 AM
  2. esbo's data sharing example
    By esbo in forum C Programming
    Replies: 49
    Last Post: 01-08-2008, 11:07 PM
  3. Creating local variables in a program?
    By fl0at in forum C Programming
    Replies: 5
    Last Post: 01-04-2008, 07:34 PM
  4. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  5. static variables
    By Luigi in forum C++ Programming
    Replies: 4
    Last Post: 04-24-2003, 07:13 PM