Like Tree2Likes
  • 2 Post By ledow

Volatile Variable.

This is a discussion on Volatile Variable. within the C Programming forums, part of the General Programming Boards category; Hi Friends, I am Nagavardhan Reddy, just now i started learning C Language. Can any one tell me what exactly ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    1

    Volatile Variable.

    Hi Friends,

    I am Nagavardhan Reddy, just now i started learning C Language.
    Can any one tell me what exactly is Volatile qualifier.

    Where and when exactly we use volatile qualifier.

    What happen if i choose all variables in a program declared as Volatile.


    Regards,
    Naga Vardhan Reddy.

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Hey friend, learn to use Google. Is your first idea really to make an account on a random forum to ask a simple thing about C syntax that you could probably find thousands of explanations on?
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    I agree, it doesn't take much to Google what it is. I'll save you that effort: "volatile" means that the variable could change under the compiler's nose without it noticing so it should NEVER try to be clever and think it knows what it last put in there. This has a very niche usage, in multithreaded environments or programs that interact directly on a low-level with the machine (where, for example, some types of memory could be changed by, for example, an expansion card and the compiler should never assume that it HASN'T changed).

    What you probably won't find on a Google is "What happen if i choose all variables in a program declared as Volatile?". The answer is that your program will run slower. Because it's completely unnecessary, and stops the compiler doing all sorts of tricks that it usually does. You're basically saying "EVERY variable in my program needs extra special care". The number of volatile's that you're likely to use unless you're programming a device driver or an operating system is approximately zero. If you NEED one, you'll know about it. Otherwise, they just destroy performance of your program.

    I have never used a volatile variable in my life, even though I know exactly when they should be used (basically, shared memory or memory-mapped i/o). I'd be incredibly surprised if even most C/C++ programmers had used them, to be honest. You can see lots of them in things like the Linux kernel source code and device drivers because those people know what they are doing. Until you have a need to use them (and, trust me, you'll know), don't. They just destroy a large number of optimisations that ALL programs can benefit from.
    QuantumPete and TheBigH like this.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Difference between global variable and volatile
    By karthik537 in forum C Programming
    Replies: 4
    Last Post: 02-29-2012, 11:28 AM
  2. Guaranteed read from volatile variable
    By cyberfish in forum C++ Programming
    Replies: 17
    Last Post: 08-15-2011, 10:00 AM
  3. Static Volatile Help
    By edesign in forum C Programming
    Replies: 2
    Last Post: 04-19-2009, 05:17 PM
  4. synchronization and volatile
    By George2 in forum C++ Programming
    Replies: 21
    Last Post: 01-04-2008, 08:31 AM
  5. volatile??
    By jacktibet in forum C Programming
    Replies: 2
    Last Post: 05-29-2003, 04:46 PM

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