6

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?

Gabriel Southern
  • 9,602
  • 12
  • 56
  • 95
  • 1
    Non-standard keywords must use leading underscores. But that's a rule that got added to the language too late to allow compilers to drop the non-standard usage without breaking a lot of existing code. – Hans Passant Jan 07 '13 at 22:17

4 Answers4

10

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.

Paul R
  • 208,748
  • 37
  • 389
  • 560
  • 1
    That really doesn't explain `__volatile__`, though; I'm guessing it might be something to do with the rule against using the `volatile` type qualifier in the Linux kernel... – SamB Aug 20 '15 at 16:51
4

Because of -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.

DigitalRoss
  • 143,651
  • 25
  • 248
  • 329
3

In addition to Paul R's response;

Given that you haven't defined any of the below names:

  • asm and volatile are language keywords. How do you know? They have no underscore before or after them, hence they are keywords in the language.

  • _something_ (one underscore) is a reserved compiler keyword, type, macro, etc.

  • __asm__, __volatile__ and __whatever_else__ (two underscores) are standard library keywords, macros, etc.

Knowing this will help you find out what things mean in the future, what to google and where to find the references to them.

gustaf r
  • 1,224
  • 9
  • 14
  • 2
    this is incorrect; the trend is to define new language keywords with leading underscore and title case (`_Bool`, `_Alignof`) and provide lower case variants as macros (`bool`, `alignof`); standard macros are upper case and surrounded by two underscores (`__FILE__`, `__STDC_VERSION__`); there are also special cases (`__func__` - which is not a macro, but a predefined identifier, `__alignof_is_defined` which is a macro); compiler and libc are free to do whatever they want but of course should follow the rules for reserved identifiers as specified by the ISO standard – Christoph Jan 07 '13 at 23:22
  • 1
    @Christoph, you're right, these aren't facts, just usually very common and therefore a help when trying to find out what something is. But again, you're right. It's nasty if you want the whole truth: http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier/228797#228797 – gustaf r Jan 07 '13 at 23:55
0

__asm__ is an alternative of asm. They mean the same thing. I can find the relevant information in GCC documentation.

For volatile and __volatile, 1 and 2 say that it means "If your assembly statement must execute where you put it". But I browse GCC documents 3 and fails to find such a explanation. I don't know which answer is correct either: the answer given in 2 or answer given by Paul R.

Jingguo Yao
  • 7,320
  • 6
  • 50
  • 63