By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,412 Members | 917 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,412 IT Pros & Developers. It's quick & easy.

_ and __ significance

P: n/a
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 __ .

Sep 25 '06 #1
Share this Question
Share on Google+
18 Replies


P: n/a

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 __ .
There is no particulat meaning in using _ or __ except that of a
general convention used to identify system variables/functions by
single underscore and metadata identified by double underscored.

-kondal

Sep 25 '06 #2

P: n/a

kondal wrote:
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 __ .

There is no particulat meaning in using _ or __ except that of a
general convention used to identify system variables/functions by
single underscore and metadata identified by double underscored.

-kondal
Thanks for your reply. But ould you please explain metadata in a bit
detail.

Ankush

Sep 25 '06 #3

P: n/a
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 __ .
Someone else will probably quote the exact legalese, but in general,
you shouldn't use names that begin with underscores because these names
can be reserved for various uses by your compiler/implementation.
Underscores inside names are often used to make identifiers more
readable - e.g. NUM_ITEMS instead of NUMITEMS.

Regards,
Bart.

Sep 25 '06 #4

P: n/a
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
Sep 25 '06 #5

P: n/a
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 __ .
More specific answers to your query have been given elsethread, but one
thing you might want to keep in mind is that the underscore character is
allowable in a C identifier.

While this is an obvious statement, it highlights the fact that any
conventions and standards are just that; conventions we've placed on the
use of characters in C identifiers.
Sep 25 '06 #6

P: n/a
g.*******@gmail.com wrote:
>
kondal wrote:
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 __ .
There is no particulat meaning in using _ or __ except that of a
general convention used to identify system variables/functions by
single underscore and metadata identified by double underscored.

-kondal

Thanks for your reply. But ould you please explain metadata in a bit
detail.
The point though, according to the rules of the language,
is that identifiers which are prefixed by _ or __
are "reserved identifiers" with some exceptions,
and that you should generally avoid using them.

--
pete
Sep 25 '06 #7

P: n/a
On 25 Sep 2006 03:45:15 -0700, "kondal" <ko******@gmail.comwrote in
comp.lang.c:
>
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 __ .

There is no particulat meaning in using _ or __ except that of a
general convention used to identify system variables/functions by
single underscore and metadata identified by double underscored.
You are completely wrong. There is no "general convention". There is
a namespace reserved for the implementation by the C language
standard.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Sep 26 '06 #8

P: n/a

Jack Klein wrote:
On 25 Sep 2006 03:45:15 -0700, "kondal" <ko******@gmail.comwrote in
comp.lang.c:

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 __ .
There is no particulat meaning in using _ or __ except that of a
general convention used to identify system variables/functions by
single underscore and metadata identified by double underscored.

You are completely wrong. There is no "general convention". There is
a namespace reserved for the implementation by the C language
standard.
I used the phrase 'general convention' because C language doesn't stop
me in using a underscore in variables. How can you say it is reserved
namespace only for the C language standard and usable only by the C
language implementors.

-kondal

Sep 26 '06 #9

P: n/a

g.*******@gmail.com wrote:
kondal wrote:
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 __ .
There is no particulat meaning in using _ or __ except that of a
general convention used to identify system variables/functions by
single underscore and metadata identified by double underscored.

-kondal

Thanks for your reply. But ould you please explain metadata in a bit
detail.

Ankush
metadata is nothing but 'data describing data'. It is useally used for
data processing systems/protocols where you have data that can fit to a
structure. This structure is dynamically created using another
sturcture which is called metadata.

Its like XML describes the data and XML scheme definition (I suppose
that is what it is called) describes the XML.

-kondal

Sep 26 '06 #10

P: n/a
kondal said:

<snip>
I used the phrase 'general convention' because C language doesn't stop
me in using a underscore in variables.
It does, however, warn you off from using them at the beginning of your
identifier names. In 1977, a 19-year-old trackside fire marshal - Jansen
van Vuuren - followed the "general convention" (anyone coming? No, okay, so
it must be safe to cross, right?), and crossed Kyalami carrying a fire
extinguisher. In so doing, he invaded racing-car space. Tom Pryce's car
struck him a second or two later. Both Pryce and van Vuuren were killed.

Stay out of implementation namespace.

How can you say it is reserved
namespace only for the C language standard and usable only by the C
language implementors.
4.1.2: "All external identifiers that begin with an underscore are reserved.
All other identifiers that begin with an underscore and either an
upper-case letter or another underscore are reserved."

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Sep 26 '06 #11

P: n/a

Richard Heathfield wrote:
kondal said:

<snip>
I used the phrase 'general convention' because C language doesn't stop
me in using a underscore in variables.

It does, however, warn you off from using them at the beginning of your
identifier names. In 1977, a 19-year-old trackside fire marshal - Jansen
van Vuuren - followed the "general convention" (anyone coming? No, okay, so
it must be safe to cross, right?), and crossed Kyalami carrying a fire
extinguisher. In so doing, he invaded racing-car space. Tom Pryce's car
struck him a second or two later. Both Pryce and van Vuuren were killed.

Stay out of implementation namespace.

How can you say it is reserved
namespace only for the C language standard and usable only by the C
language implementors.

4.1.2: "All external identifiers that begin with an underscore are reserved.
All other identifiers that begin with an underscore and either an
upper-case letter or another underscore are reserved."

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Thank you. Can you tell me which C compiler is close to C spec? I
generally use gcc and it didn't give me any warning.

-kondal

Sep 26 '06 #12

P: n/a
kondal said:
>
Richard Heathfield wrote:
>kondal said:
<snip>
>>
How can you say it is reserved
namespace only for the C language standard and usable only by the C
language implementors.

4.1.2: "All external identifiers that begin with an underscore are
reserved. All other identifiers that begin with an underscore and either
an upper-case letter or another underscore are reserved."

Thank you. Can you tell me which C compiler is close to C spec? I
generally use gcc and it didn't give me any warning.
C compilers are only required to give diagnostic messages for syntax errors
and constraint violations. Invading implementation namespace is neither of
those, so no message is required.

You have *had* your warning, right there in 4.1.2: "Don't. Do. This." You
shouldn't need another.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Sep 26 '06 #13

P: n/a

Richard Heathfield wrote:
kondal said:

Richard Heathfield wrote:
kondal said:
<snip>
>
How can you say it is reserved
namespace only for the C language standard and usable only by the C
language implementors.

4.1.2: "All external identifiers that begin with an underscore are
reserved. All other identifiers that begin with an underscore and either
an upper-case letter or another underscore are reserved."
Thank you. Can you tell me which C compiler is close to C spec? I
generally use gcc and it didn't give me any warning.

C compilers are only required to give diagnostic messages for syntax errors
and constraint violations. Invading implementation namespace is neither of
those, so no message is required.

You have *had* your warning, right there in 4.1.2: "Don't. Do. This." You
shouldn't need another.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
OK, I understand.
It does, however, warn you off from using them at the beginning of your
identifier names.

My curiosity is to know which C compiler gives the warning. If it is
defined only in C spec and not used by the compilers then what is the
use? I do not want to initiate any argument here, it could just be that
nobody cares about it.

I've seen lot of source codes which use underscores for structure names
and variables. as I said gcc (v 3.2.2) doesn't produce any warning.

-kondal

Sep 26 '06 #14

P: n/a
kondal said:
>
Richard Heathfield wrote:
>It does, however, warn you off from using them at the beginning of your
identifier names.

My curiosity is to know which C compiler gives the warning.
No, the *Standard* warns you off from using leading underscores.
If it is
defined only in C spec and not used by the compilers then what is the
use?
The C compilers DO use identifiers with leading underscores, and that's why
you shouldn't.

Look, it's very simple.

You stay over here. The compiler writer stays over there. You don't get in
his way, and he won't get in yours. Easy.
I do not want to initiate any argument here, it could just be that
nobody cares about it.
Well, I care about not using identifiers that the implementation uses,
because I would like my programs to work. If you don't need your programs
to work, there is no need for you to care.
I've seen lot of source codes which use underscores for structure names
and variables. as I said gcc (v 3.2.2) doesn't produce any warning.
As I've said, it doesn't have to. You had your warning already, in the C
Standard. If you can't be bothered to pay attention to that warning, why
would you bother to pay any attention to gcc's warning, were it to give
one?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Sep 26 '06 #15

P: n/a
kondal wrote:
OK, I understand.
I don't think so.
My curiosity is to know which C compiler gives the warning. If it is
defined only in C spec and not used by the compilers then what is the
use?
How would a compiler know when to give a warning? Let's say you
#include <stdio.hin your program and that header declares names such
as _iobuf, how would the compiler distinguish between code that you
wrote and code from the standard header?
I've seen lot of source codes which use underscores for structure names
and variables. as I said gcc (v 3.2.2) doesn't produce any warning.
The code of your standard library is not a good example to follow. As
mentionned before, those who write <stdio.hand other standard library
headers are allowed to use names that begin with underscores. You are
not allowed to use those same names. In this way, you don't conflict
with them.

Regards,
Bart.

Sep 26 '06 #16

P: n/a
Richard Heathfield wrote:
kondal said:
>Richard Heathfield wrote:
[...]
>I've seen lot of source codes which use underscores for structure names
and variables. as I said gcc (v 3.2.2) doesn't produce any warning.

As I've said, it doesn't have to. You had your warning already, in the C
Standard. If you can't be bothered to pay attention to that warning, why
would you bother to pay any attention to gcc's warning, were it to give
one?
Well, not everyone has committed every paragraph of the Standard to
memory (or can tease out the meaning in all the paragraphs if they had!)
In this case one would not know they have violated the Standard, even
if they were the type of person who might otherwise pay attention to a
diagnostic.

It does not follow that accidental ignorance of some paragraphs
indicates that the coder is more likely to ignore an honest-to-gods
diagnostic.

Simply put, even coders familiar with a good chunk of the Standard may
just know that an underscore is a valid identifier character and that
they've seen the identifier used in some specific cases. This might
lead to the incorrect assumption that this is merely a naming convention
(much like commenting styles, or brace placement, or Hungarian notation).

Perhaps I'm admitting to some terrible sin, but I will never commit the
entire Standard to memory, and know that even if I did I might come up
with some creative misunderstandings on implementation.

So, it follows that I will continue to discover new aspects of the
language and correct incorrect assumptions over time. Since I sometimes
try to participate in Real Life, and only partly spend my time with C,
this is a reasonable limitation I've learned to accept!

This does not mean that I would necessarily ignore a diagnostic.
Sep 26 '06 #17

P: n/a
Clever Monkey said:

<snip>
>
So, it follows that I will continue to discover new aspects of the
language and correct incorrect assumptions over time. Since I sometimes
try to participate in Real Life, and only partly spend my time with C,
There's your problem right there! ;-)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Sep 26 '06 #18

P: n/a
On 25 Sep 2006 23:50:39 -0700, in comp.lang.c , "kondal"
<ko******@gmail.comwrote:
>It does, however, warn you off from using them at the beginning of your
identifier names.

My curiosity is to know which C compiler gives the warning.
None, to my knowledge.
>If it is
defined only in C spec and not used by the compilers then what is the
use?
Compiler and C standard library writers can use it in their code.
>I've seen lot of source codes which use underscores for structure names
and variables. as I said gcc (v 3.2.2) doesn't produce any warning.
Its not required to.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Sep 27 '06 #19

This discussion thread is closed

Replies have been disabled for this discussion.