What is the difference between volatile
and __volatile__
in C code compiled with gcc?
I was looking in the Linux source code and I noticed that some places use __asm__ __volatile__
others use asm volatile
and others use __asm__ volatile
.
I have not seen __volatile__
used without __asm__
, while I have seen volatile
used in a variety of other places.
Is there any difference between what __volatile__
and volatile
do? If so what is it? Or if not is there a reason that __volatile__
is used sometimes?
This is just for backward compatibility with legacy code. Keywords that were added to C in later life, such as inline
, volatile
, asm
, etc, have underscore prefix/suffix versions (__inline__
, __asm__
, __volatile__
, etc) so that you can use them in legacy code that already uses the unadorned names as types/functions/variables/whatever. There is a command line switch which controls this, -ansi
- when you compile with gcc -ansi ...
only the underscore versions of these newer keywords are recognised.
-ansi
Although a real -ansi
program would not use asm(),
there needs to be a way to include an asm macro in a header even when the program itself is built with -ansi
. (With -ansi
, gcc doesn't include extensions which conflict with strict ISO C, like new keywords.)
A properly namespaced alternative __asm__
was necessary for asm
, which could not be defined by the system in -ansi
mode. Now, this wasn't strictly necessary with volatile,
which has been a keyword for a long time, but perhaps by inertia someone also (and unnecessarily) made a __volatile__
(reserved system name format) to go with volatile
, even if the program would have been legal -ansi
either way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With