extern int ether_hostton (__const char *__hostname, struct ether_addr *__addr)
__THROW;
I found the above function definition in /usr/include/netinet/ether.h on a Linux box.
Can someone explain what the double underscores mean in front of const (keyword), addr (identifier) and at last __THROW.
In C, symbols starting with an underscore followed by either an upper-case letter or another underscore are reserved for the implementation. You as a user of C should not create any symbols that start with the reserved sequences. In C++, the restriction is more stringent; you the user may not create a symbol containing a double-underscore.
Given:
The
__const
notation is there to allow for the possibility (somewhat unlikely) that a compiler that this code is used with supports prototype notations but does not have a correct understanding of the C89 standard keywordconst
. Theautoconf
macros can still check whether the compiler has working support forconst
; this code could be used with a broken compiler that does not have that support.The use of
__hostname
and__addr
is a protection measure for you, the user of the header. If you compile with GCC and the-Wshadow
option, the compiler will warn you when any local variables shadow a global variable. If the function used justhostname
instead of__hostname
, and if you had a function calledhostname()
, there'd be a shadowing. By using names reserved to the implementation, there is no conflict with your legitimate code.The use of
__THROW
means that the code can, under some circumstances, be declared with some sort of 'throw specification'. This is not standard C; it is more like C++. But the code can be used with a C compiler as long as one of the headers (or the compiler itself) defines__THROW
to empty, or to some compiler-specific extension of the standard C syntax.Section 7.1.3 of the C standard (ISO 9899:1999) says:
See also What are the rules about using an underscore in a C++ identifier; a lot of the same rules apply to both C and C++, though the embedded double-underscore rule is in C++ only, as mentioned at the top of this answer.
The underscore in __const means that this keyword is a compiler extension and using it is not portable (The const keyword was added to C in a later revision, 89 I think). The __THROW is also some kind of extension, I assume that it gets defined to some __attribute__(something) if gcc is used, But I'm not sure on that and too lazy to check. The __addr can mean anything the programmer wanted it to mean, It's just a name.
Names with double leading underscores are reserved for use by the implementation. This does not necessarily mean they are internal per se, although they often are.
The idea is, you're not allowed to to use any names starting with
__
, so the implementation is free to use them in places like macro expansions, or in the names of syntax extensions (e.g.__gcnew
is not part of C++, but Microsoft can add it to C++/CLI confident that no existing code should have something likeint __gcnew;
in it that would stop compiling).To find out what these specific extensions mean, i.e.
__const
you'll need to consult the documentation for your specific compiler/platform. In this particular case, you should probably consider the prototype in the documentation (e.g. http://www.kernel.org/doc/man-pages/online/pages/man3/ether_aton.3.html) to be the function's interface and ignore the__const
and__THROW
decorations that appear in the actual header.By convention in some libraries, this indicates that a particular symbol is for internal use and not intended to be part of the public API of the library.