Difference between global variable and volatile

This is a discussion on Difference between global variable and volatile within the C Programming forums, part of the General Programming Boards category; Hello Guys, Am looking for a clear explanation with example to differentiate between global and volatile variables. This was part ...

  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,276
    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,490
    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.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 06: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21