Click to See Complete Forum and Search --> : Curious question about C -- #pragma and volatile


Concrete Geist
10-18-2001, 09:38 PM
My question is what do they do and how do they work?? All my book tells me is ( for both) "This is beyond the scope of this book"

bwkaz
10-18-2001, 10:04 PM
#pragma is (IIRC) to enable or disable some aspect of the compiler. In some braindead older compilers (Borland Turbo C for DOS ;) ), you used to have to use a #pragma to enable inline assembly -- the line was #pragma inline. In almost all compilers, you can use them to enable or disable warnings -- #pragma warn xxxx and #pragma -warn xxxx, where xxxx is a placeholder for either the warning number or the warning identifier.

volatile tells the compiler that it shouldn't optimize this variable at all. It used to mean that an asynchronous interrupt could change the value of the variable, so don't load it into a register between uses -- make sure the current value always exists in memory, and make sure you read from memory every time the value is needed. I used to use it (back when I programmed for DOS... yech) for like a key-state array that could be modified at any time by the keyboard interrupt (which I installed myself, so it knew where the key-state array was).

Now it's sometimes used for the same thing, but more often it just isn't used, replaced by semaphores and spinlocks and such. So now the access to the variable is just synchronized.

HTH