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.
Best Answer
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.
C99 Rationale
The C99 Rationale says:
And the relevant part of the rationale for §6.2.1 Scopes of identifiers is:
See also P J Plauger The Standard C Library (1992) for an extensive discussion of name space rules and library implementations. The book refers to C90 rather than any later version of the standard, but most of the implementation advice in it remains valid to this day.