1

I've seen a bunch of functions in Linux code named __foo. What does the double underscore stand for and when should it be used?

Hailei
  • 42,163
  • 6
  • 44
  • 69
EpsilonVector
  • 3,973
  • 7
  • 38
  • 62

1 Answers1

5

It means it's a reserved identifer. Both C++ 03 and C99 standard mentioned about this.

C99:

7.1.3 Reserved identifiers

  • All identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use.
  • All identifiers that begin with an underscore are always reserved for use as identifiers with file scope in both the ordinary and tag name spaces.

C++ 03:

Each name that contains a double underscore (_ _) or begins with an underscore followed by an uppercase letter (2.11) is reserved to the implementation for any use.

You can also refer to:

Community
  • 1
  • 1
Hailei
  • 42,163
  • 6
  • 44
  • 69
  • Reserved to the implementation of what? Like a private function? Why not use a private/not-exported-in-header function? – EpsilonVector May 07 '12 at 06:31
  • The compiler and compiler's library implementation. – Michael Burr May 07 '12 at 06:40
  • imo it's in order to avoid identifer collisions. – Hailei May 07 '12 at 06:40
  • @EpsilonVector, C doesn't have private functions, and sometimes they need to be in the header. They might be variables or functions used to implement the varargs macros, for example, or symbol names used by the internal C++ exception handling system. – bdonlan May 07 '12 at 06:43
  • Note that the Linux kernel sometimes also uses names that the C standard might reserve for "the implementation". So you might want to consider the Linux kernel as part of the implementation as well. Since the kernel developers only care about the GCC compiler, they can be reasonably sure that they're avoiding name collisions with the compiler. – Michael Burr May 07 '12 at 06:48