Meaning of double underscore in the beginning

13,266

Solution 1

From GNU's manual:

In addition to the names documented in this manual, reserved names include all external identifiers (global functions and variables) that begin with an underscore (‘_’) and all identifiers regardless of use that begin with either two underscores or an underscore followed by a capital letter are reserved names. This is so that the library and header files can define functions, variables, and macros for internal purposes without risk of conflict with names in user programs.

This is a convention which is also used by C and C++ vendors.

Solution 2

Names with leading double underscore are reserved for internal use by the implementation (compiler/standard library/etc.). They should never appear in your code. The purpose of this reserved namespace is to give the system headers names they can use without potentially clashing with names used in your program.

Solution 3

ISO 9899:2011

7.1.3 Reserved identifiers

Each header declares or defines all identifiers listed in its associated subclause, and optionally declares or defines identifiers listed in its associated future library directions subclause and identifiers which are always reserved either for any use or for use as file scope 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.

Share:
13,266

Related videos on Youtube

pythonic
Author by

pythonic

Updated on June 03, 2022

Comments

  • pythonic
    pythonic almost 2 years

    In the standard library (glibc) I see functions defined with leading double underscores, such as __mmap in sys/mman.h. What is the purpose? And how can we still call a function mmap which doesn't seem to be declared anywhere. I mean we include sys/mman.h for that, but sys/mman.h doesn't declare mmap, it declares only __mmap.

    • wkl
      wkl almost 12 years
      Double underscores: stackoverflow.com/questions/224397/… (question says C++, but the __ is from the ANSI C standard as well). Also in my version of sys/mman.h, mmap is defined.
    • R.. GitHub STOP HELPING ICE
      R.. GitHub STOP HELPING ICE almost 12 years
      __mmap is not declared anywhere by glibc. No idea what you're talking about.
    • Jonathan Wakely
      Jonathan Wakely almost 12 years
      Are you looking at kernel headers, not glibc headers?
  • Jeff T.
    Jeff T. about 7 years
    I like your answer which is kind to me.