Thread: Difference between global variable and volatile

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    53

    Difference between global variable and volatile

    Hello Guys,

    Am looking for a clear explanation with example to differentiate between global and volatile variables. This was part of an interview question where the guy asked:

    consider two programs: 1.c 2.c, where:

    1.c: int a=10;
    2.c: extern int a; printf(a);

    Now, the value should be 10 only. Also, in case later if the value for a is changed in 1.c program then also 2.c will print the same value. Then how does volatile different from global variables?

    Thanks for your answers. Not sure if the question is described clearly, let me know if any doubts.

    Regards,
    Karthik

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Global variables are regular variables that exist in a program. You have to write code to change the value of the variable. Even if they're in two different .c files, you have to somewhere, in 1.c or 2.c, do something like a = 42; to change it's value. Whatever it is that you do, you did it, in your program.

    The volatile keyword means something outside your program may change the value of your variable. This is commonly used for lower level programming and embedded systems, when you have memory-mapped variables that correspond to hardware registers. An external interrupt, serial data, A/D converter, etc may change the value of that variable, so that between the following code could print any number:
    Code:
    volatile int x;
    x = 42;
    printf("%d", x);  // could print 17 or any other number if the value of x was changed externally
    Also, volatile variables can't really be optimized by the compiler. Since the value may be changed by something other than your program, the compiler can never make any assumptions about it's value or usage, thus can't make any decisions to optimize.

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    1
    But volatile can also be modified by you and your code and similarly a global variable can also be used as a variable to monitor external events so how does both are different?

  4. #4
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by amritpal.singh View Post
    similarly a global variable can also be used as a variable to monitor external events
    Not reliably (afaik).
    Suppose, you want a loop to continue when a global variable has a certain value.
    If the compiler finds that nothing in your program is changing that variable, it would simply make the loop an infinite one.
    But it can't do so, if you make the variable volatile.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Whether something is global (or not) is a scope issue.

    Whether something is volatile (or not) is a type qualifier issue.

    They're independent concepts in C. You can add the volatile qualifier independent of the scope of the variable.

    If you want something to chew on, consider this.
    extern const volatile int clock;
    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. Guaranteed read from volatile variable
    By cyberfish in forum C++ Programming
    Replies: 17
    Last Post: 08-15-2011, 09:00 AM
  2. Replies: 8
    Last Post: 09-27-2010, 04:11 PM
  3. Static Local Variable vs. Global Variable
    By arpsmack in forum C Programming
    Replies: 7
    Last Post: 08-21-2008, 03:35 AM
  4. Difference Betn Volatile,static,extern variables
    By nitinmhetre in forum C Programming
    Replies: 3
    Last Post: 12-26-2006, 07:13 AM
  5. Static global variable acting as global variable?
    By Visu in forum C Programming
    Replies: 2
    Last Post: 07-20-2004, 08:46 AM