Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > using '__' in names

Reply
Thread Tools

using '__' in names

 
 
Roman Mashak
Guest
Posts: n/a
 
      08-17-2005
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: http://www.velocityreviews.com/forums/(E-Mail Removed)


 
Reply With Quote
 
 
 
 
Daniele Benegiamo
Guest
Posts: n/a
 
      08-17-2005
Roman Mashak wrote:
> 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
 
Reply With Quote
 
 
 
 
Artie Gold
Guest
Posts: n/a
 
      08-17-2005
Roman Mashak wrote:
> 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 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.

--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com (new post 8/5)
http://www.cafepress.com/goldsays
"If you have nothing to hide, you're not trying!"
 
Reply With Quote
 
Robert Gamble
Guest
Posts: n/a
 
      08-17-2005
Daniele Benegiamo wrote:
> Roman Mashak wrote:
> > 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.


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

Robert Gamble

 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      08-17-2005
On Wed, 17 Aug 2005 11:38:51 +0900, "Roman Mashak" <(E-Mail Removed)>
wrote in comp.lang.c:

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

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
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
 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      08-17-2005
On Wed, 17 Aug 2005 04:49:31 +0200, Daniele Benegiamo
<(E-Mail Removed)> wrote in comp.lang.c:

> Roman Mashak wrote:
> > 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


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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
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
 
Reply With Quote
 
Daniele Benegiamo
Guest
Posts: n/a
 
      08-17-2005
Robert Gamble wrote:
> 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/de...tm/eleme_5.asp
http://msdn.microsoft.com/library/de...tm/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/conte...Num=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
 
Reply With Quote
 
Robert Gamble
Guest
Posts: n/a
 
      08-17-2005

Daniele Benegiamo wrote:
> Robert Gamble wrote:
> > 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:


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

 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      08-17-2005
"Robert Gamble" <(E-Mail Removed)> writes:

> Daniele Benegiamo wrote:
>> Robert Gamble wrote:
>> > 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:

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

--
"Large amounts of money tend to quench any scruples I might be having."
-- Stephan Wilms
 
Reply With Quote
 
Roman Mashak
Guest
Posts: n/a
 
      08-17-2005
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 Removed)


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting 'flat' gate level names to hierarchical names Paddy McCarthy VHDL 3 09-24-2004 05:34 PM
table field names vs. display names Bob ASP .Net 1 07-30-2004 05:06 PM
Matching attribute names to element names in a different path Carl XML 0 04-01-2004 01:15 PM
WSDL- Mapping Application Defined Names to XML Names Craig XML 0 02-09-2004 04:14 PM
XSL rules applying to XSD (XML schema) defined type names (as opposed to node names) Lewis G. Pringle, Jr. XML 0 09-30-2003 10:34 PM



Advertisments