g.*******@gmail.com wrote:
I have seen many variables or structures declared as _ or __ prefixed .
Can anyone explain the significance of _ or __ particularly . I mean ,
I wanted to know the convention for using _ and __ .
C has a problem: When you #include a header like <stdio.h>
your program suddenly acquires declarations of functions like
printf() and fopen() -- which is what you wanted -- but it
usually also acquires declarations of some of the private
details of the standard library. For example, <stdio.hmust
declare FILE* as a type, and quite often has to declare a FILE
struct to do so. Many <ctype.himplementations declare special
arrays that describe the attributes of different character values.
And so on: C's problem is that it is difficult to declare all the
"public" stuff properly without making some of the "private" stuff
visible at the same time.
Why is that a problem? Some of the "private" stuff needs
names -- the names of the elements in a FILE struct, or of the
special <ctype.harrays, for example -- and chaos will result if
those names collide with others that the programmer has chosen for
his own purposes. If a hypothetical <ctype.hdid this:
extern char lower[1+256];
#define tolower(c) lower[1+(c)]
there would be trouble if your program started out with
#include <ctype.h>
int higher = 1;
int lower = -1;
because `lower' is being used to refer to two different things
and the uses can't be resolved contextually.
C "solves" this problem by dividing programmers into two
groups: Those who write the C implementation and those who use
C to write other things. The Standard then reserves one family
of identifiers for use by the implementors, and another family
for the users: The implementors may not use `lower' lest it clash
with an identifier a user might choose, and the users must not
use `_lower' because that's a name reserved for implementors' use.
(This is a slightly simplified version of affairs; the actual
situation is somewhat more involved. Roughly speaking, though,
users should not declare identifiers that start with underscores
and implementors should not declare identifiers that start with
letters -- there's a long list of exceptions and special cases,
but it's hardly worth trying to remember them all.)
So: When you see `extern struct _io _iob[3];' in a system
header this is *not* an encouragement to use names like _io and
_iob in your own code. Rather, it's the implementor staying out
of your way by using special names for the things he needs to
give names to.
--
Eric Sosman
es*****@acm-dot-org.invalid