using '__' in names

R

Roman Mashak

Hello, All!

I often meet that '_' or '__' is used as prefix to
functions/macros/variables names. I wonder does it have some strict meaning?
I didn't find any distinct explanation in C standard or local FAQ.

With best regards, Roman Mashak. E-mail: (e-mail address removed)
 
D

Daniele Benegiamo

Roman said:
I often meet that '_' or '__' is used as prefix to
functions/macros/variables names. I wonder does it have some strict meaning?

If I remember: all symbols that starts with an underscore or contains
two consecutive underscores are reserved. This means that no portable
programs can uses this sort of identifiers.

Daniele
 
A

Artie Gold

Roman said:
Hello, All!

I often meet that '_' or '__' is used as prefix to
functions/macros/variables names. I wonder does it have some strict meaning?
I didn't find any distinct explanation in C standard or local FAQ.

With best regards, Roman Mashak. E-mail: (e-mail address removed)
In general, such names are reserved for the implementation. Do *not* use
names like this in code you write.[1]

HTH,
--ag

[1] There are situations where it's legal, but it's still a Bad Idea.
 
R

Robert Gamble

Daniele said:
If I remember: all symbols that starts with an underscore or contains
two consecutive underscores are reserved. This means that no portable
programs can uses this sort of identifiers.

Identifiers starting with an underscore are reserved, there is no such
restriction on identifiers containing consecutive underscores.

Robert Gamble
 
J

Jack Klein

Hello, All!

I often meet that '_' or '__' is used as prefix to
functions/macros/variables names. I wonder does it have some strict meaning?
I didn't find any distinct explanation in C standard or local FAQ.

All symbols beginning with two underscores ("__") or one underscore
followed by a upper case letter ("_A" through "_Z"), are reserved for
the implementation (compiler, its headers and library) in all
contexts.

All symbols beginning with an underscore followed by a lower case
letter ("_a" through "_z") are also reserved for the implementation at
file scope.

If you look at a header supplied by your compiler, you might see that
it includes something like this at the top:

#ifndef __STDIO_H__
#define __STDIO_H__

/* contents of header */

#endif

The point is that the standard reserves some identifiers for the
implementation so that it can define its own macros, data types,
internal helper functions, and non-standard extensions that will not
clash with any identifiers a programmer uses, if the programmer
understands and follows the rules.

Unfortunately, the vast majority of C (and C++) programmers do not
understand the rules. Most of them still define include guard macros
in their header files like this:

my_header.h:
#ifndef __MY_HEADER_H__
#define __MY_HEADER_H__
/* ... */
#endif

Because they see it in compiler supplied headers and somehow think it
is the thing to use in headers.
 
J

Jack Klein

If I remember: all symbols that starts with an underscore or contains
two consecutive underscores are reserved. This means that no portable
programs can uses this sort of identifiers.

Daniele

That's a mix of incorrect C and incorrect C++.

See my reply to the OP for the actual C reservations. The double
underscore "__" is free for any use in C if they are not the first two
characters.

While it is off-topic here, C++ reserves symbols with "__" anywhere in
them, but that's a language of a different color.
 
D

Daniele Benegiamo

Robert said:
Daniele Benegiamo wrote:
Identifiers starting with an underscore are reserved, there is no such
restriction on identifiers containing consecutive underscores.

ANSI have reserved them for compiler writers:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm/eleme_5.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm/eleme_6.asp


Not related to underscores, but maybe useful to Roman and others,
general identifier names have other restrictions (extracted from
http://www.informit.com/guides/content.asp?g=cplusplus&seqNum=185&rl=1):

The reserved standard C function names are:
* "is" followed by a lowercase letter, e.g. "isspace"
* "mem" followed by a lowercase letter, e.g. "memset"
* "str" followed by a lowercase letter, e.g. "strcmp"
* "to" followed by a lowercase letter, e.g. "tolower"
* "wcs" followed by a lowercase letter, e.g. "wcstof"

The following macros are reserved:
* Identifiers that start with E followed by a digit or an uppercase letter
* Identifiers that start with LC_ followed by an uppercase letter
* Identifiers that start with SIG or SIG_ followed by an uppercase letter


Daniele
 
R

Robert Gamble

Daniele said:
ANSI have reserved them for compiler writers:

Identifiers starting with an underscore are reserved. If an identifier
contains consecutive underscores, that does not in and of itself make
it reserved. If it starts with two consecutive underscores then it
obviously starts with one underscore which is covered in the first part
of my statement. Read carefully.

Robert Gamble
 
B

Ben Pfaff

Robert Gamble said:
Identifiers starting with an underscore are reserved. If an identifier
contains consecutive underscores, that does not in and of itself make
it reserved.

I think there's some confusion with C++ here, which, unlike C,
does indeed reserve all identifiers that contain consecutive
underscores. From C++98:

$ 17.4.3.1.2 Global names [lib.global.names]
$
$ 1 Certain sets of names and function signatures are always
$ reserved to the implementation:
$
$ - Each name that contains a double underscore (__) or begins
$ with an underscore followed by an upper-case letter (2.11)
$ is reserved to the implementation for any use.
 
R

Roman Mashak

Hello, Jack!
You wrote on Tue, 16 Aug 2005 22:19:54 -0500:

JK> All symbols beginning with an underscore followed by a lower case
JK> letter ("_a" through "_z") are also reserved for the implementation at
JK> file scope.
If I unerstand you right, standard also doesn't recommend these type of
symbols in your own libraries, application etc.? Also I met this in many
SDK, developed by many respected companies.
JK> If you look at a header supplied by your compiler, you might see that
JK> it includes something like this at the top:

With best regards, Roman Mashak. E-mail: (e-mail address removed)
 
J

Jack Klein

Identifiers starting with an underscore are reserved, there is no such
restriction on identifiers containing consecutive underscores.

Your statement is too broad.

Every identifier starting with an underscore in the following snippet
is valid in the application's namespace in the context where it is
declared:

#include <stdio.h>

int main(void)
{
int _123 = 0;
int _abc;
double _;
typedef struct _xyz
{
int _456;
int _all;
} _qrs;
_qrs _def = { 3, 4 };
_abc = _def._456;
_abc *= _def._all;
_ = _abc / 2.0;
printf("%f\n", _);
return _123;
}

And it must produce the output "6.000000" on any conforming C
implementation.

Here is the actual text of the standard for the two cases where the
underscore makes identifiers reserved:

"— 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."

Note that leading underscores are specifically not reserved at block
scope as long as they are not followed by a second underscore or an
upper case letter.
 
J

Jack Klein

Hello, Jack!
You wrote on Tue, 16 Aug 2005 22:19:54 -0500:

JK> All symbols beginning with an underscore followed by a lower case
JK> letter ("_a" through "_z") are also reserved for the implementation at
JK> file scope.

Actually, this one is wrong, my mistake in quoting from memory instead
of looking it up in the standard. All identifiers beginning with an
'_' are reserved at file scope, so "_0" through "_9" and just plain
"_" are reserved outside of a block.
If I unerstand you right, standard also doesn't recommend these type of
symbols in your own libraries, application etc.? Also I met this in many
SDK, developed by many respected companies.

When is says "reserved for the implementation" it means exactly that,
and your applications and libraries are not part of the
implementation.

Third parties who develop libraries for distribution certainly don't
want their users complaining that including one of their header files
or linking to one of their libraries causes the compile or link to
break, or even worse, the program builds but suddenly has "strange"
errors. Such libraries are often written by the compiler implementer,
in which case they have a perfect right to use those symbols, or for a
particular compiler and the vendor has tested it to make sure there
are no conflicts.
JK> If you look at a header supplied by your compiler, you might see that
JK> it includes something like this at the top:

With best regards, Roman Mashak. E-mail: (e-mail address removed)

If you do you violate the implementation name space, the C standard
doesn't define what will happen. Most likely any such identifier that
you use won't match one used by your implementation and the program
will work just fine. If the name does clash, you may get the compiler
or linker errors I mentioned above, or the strange problems.

Here are three lines from the stdio.h standard header supplied with
Microsoft Visual C++ 6.0:

#ifndef _INC_STDIO
#define _INC_STDIO

....and:

#endif /* _INC_STDIO */

So what do you think will happen if I use that implementation on this
source file:

#include <stdio.h>

int main(void)
{
int _INC_STDIO = 0;
return _INC_STDIO;
}

???

Replace the string "_INC_STDIO" with nothing, and see what the
compiler has to work with after the preprocessor phase.

The point is, while there are a few cases where you can use a leading
underscore, it's not worth remembering the rules. Just never define
any identifier, including macros, in your programs that begin with an
underscore and you'll never have that particular problem. There is
absolutely no compelling need or any possible gain to doing so.
 
E

Emmanuel Delahaye

Roman Mashak wrote on 17/08/05 :
I often meet that '_' or '__' is used as prefix to functions/macros/variables
names. I wonder does it have some strict meaning? I didn't find any distinct
explanation in C standard or local FAQ.

It has no peculiar meaning, but it belongs to the implementation
namespace. Users should not have names starting with with _[_A-Z]

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"C is a sharp tool"
 
M

Mark McIntyre

Identifiers starting with an underscore are reserved, there is no such
restriction on identifiers containing consecutive underscores.

Yes there is:
All identifiers that begin with an underscore and either an
uppercase letter or another underscore are always reserved for any
use

..
 
R

Robert Gamble

Mark said:
Yes there is:
All identifiers that begin with an underscore and either an
uppercase letter or another underscore are always reserved for any
use

See my response to Daniele.

Robert Gamble
 
K

Keith Thompson

Mark McIntyre said:
Yes there is:
All identifiers that begin with an underscore and either an
uppercase letter or another underscore are always reserved for any
use

I think the reference was to identifiers like foo__bar.
 
R

Robert Gamble

Keith said:
I think the reference was to identifiers like foo__bar.

Exactly. I was trying to say that having consecutive underscores alone
doesn't put it into the reserved category, I thought my original
wording was pretty clear, I guess I was wrong.

Robert Gamble
 
M

Mark McIntyre

See my response to Daniele.

I read your original post as relating to identifiers starting with
underscores, since thats how the sentence begins. If you meant it to
relate to having two underscores elsewhere in the macro name, that
wasn't at all clear.
 
R

Robert Gamble

Mark said:
I read your original post as relating to identifiers starting with
underscores, since thats how the sentence begins. If you meant it to
relate to having two underscores elsewhere in the macro name, that
wasn't at all clear.

In the first part I used "starting with", in the second I used
"containing". If I meant identifiers starting with consecutive
underscores, that is what I would have said. I assumed that one could
easily deduct that an identifier beginning with multiple underscores
would be reserved as per the first part of my statement. I don't think
the statement was really that poorly worded but I will try to be more
clear going forward.

Robert Gamble
 
C

Cool Guy

Robert Gamble said:
I don't think
the statement was really that poorly worded but I will try to be more
clear going forward.

Had you not misused the comma, it might've been clearer, IMO.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

tolower() and toupper() 8
re-definition of standard types 9
get random number in range [10..50] 14
array of size 1 10
typical practise for #include's 2
buffer overflow 20
lint 4
question about 'static' definitions 3

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top