Underscore Prefixes 
Author Message
 Underscore Prefixes

As a newbie to C and C++, is there any rhyme or reason to the practice of using
one or multiple underscore prefixes to variable, function, or macro names?  I
realize they are used to avoid namespace collisions during compiling and
linking, but is there a guide to its practice?

Thanks in advance,

Jason Burton



Sat, 26 May 2001 03:00:00 GMT  
 Underscore Prefixes
Be careful about using underscores. Their used extensively by the run time
code and could cause problems.
Programming syntax is not a good subject to bring up. Its sort of like
religion only worse with programmers. 8-)) Each has his or her on
prefrences.
good habit to get into is to document your syntax in both the header and
code files so others will know what its all about.

--
URL: http://otool.hypermart.net

In Memory: http://otool.hypermart.net/inmemory.htm


Quote:
>As a newbie to C and C++, is there any rhyme or reason to the practice of
using
>one or multiple underscore prefixes to variable, function, or macro names?
I
>realize they are used to avoid namespace collisions during compiling and
>linking, but is there a guide to its practice?

>Thanks in advance,

>Jason Burton



Sun, 27 May 2001 03:00:00 GMT  
 Underscore Prefixes
The ANSI C++ standard covers usage of underscores. Use of double
underscores is reserved for compiler vendors, and there are some other
restrictions in the standard. Other than that, it's up to your preferences.
The only real recommendation is don't use the header files as an
example of good practice; they aren't.


Quote:
>As a newbie to C and C++, is there any rhyme or reason to the practice of
using
>one or multiple underscore prefixes to variable, function, or macro names?
I
>realize they are used to avoid namespace collisions during compiling and
>linking, but is there a guide to its practice?

>Thanks in advance,

>Jason Burton



Sun, 27 May 2001 03:00:00 GMT  
 Underscore Prefixes

Quote:

> As a newbie to C and C++, is there any rhyme or reason to the practice of using
> one or multiple underscore prefixes to variable, function, or macro names?  I
> realize they are used to avoid namespace collisions during compiling and
> linking, but is there a guide to its practice?

Can't vounch for C, but in C++ you should never use
any underscore prefixes.  The use of two underscores
anywhere in the identifier is reserved for the implemenation.
The use of a leading under followed by an upper case
letter is likewise reserved.   The implmenetnation is
free to polute the global namespace with other symbols
beginning with underscore, so use of these is at your
own peril.

So, if you need to make use of a system symbol with
leading underscores that's OK (like the _MAX macro in VC++),
but don't use them when you create your own variables.



Sun, 27 May 2001 03:00:00 GMT  
 Underscore Prefixes
Oh.

I guess the lesson learned here is, don't look at code in order to learn from
it, eh?  It also helps to have a little insight into the history and standards
of the language.  At least now I know what the underscores are for (only about a
bazillion other quirks to go).

Thanks to all of you for the great info!  It's good to know newbies are still
welcome in some newsgroups.

Thanks again,

Jason Burton

Quote:

>Be careful about using underscores. Their used extensively by the run time
>code and could cause problems.
>Programming syntax is not a good subject to bring up. Its sort of like
>religion only worse with programmers. 8-)) Each has his or her on
>prefrences.
>good habit to get into is to document your syntax in both the header and
>code files so others will know what its all about.

>--
>URL: http://otool.hypermart.net

>In Memory: http://otool.hypermart.net/inmemory.htm



>>As a newbie to C and C++, is there any rhyme or reason to the practice of
>using
>>one or multiple underscore prefixes to variable, function, or macro names?
>I
>>realize they are used to avoid namespace collisions during compiling and
>>linking, but is there a guide to its practice?

>>Thanks in advance,

>>Jason Burton

>The ANSI C++ standard covers usage of underscores. Use of double
>underscores is reserved for compiler vendors, and there are some other
>restrictions in the standard. Other than that, it's up to your preferences.
>The only real recommendation is don't use the header files as an
>example of good practice; they aren't.



>>As a newbie to C and C++, is there any rhyme or reason to the practice of
>using
>>one or multiple underscore prefixes to variable, function, or macro names?
>I
>>realize they are used to avoid namespace collisions during compiling and
>>linking, but is there a guide to its practice?

>>Thanks in advance,

>>Jason Burton


>> As a newbie to C and C++, is there any rhyme or reason to the practice of
using
>> one or multiple underscore prefixes to variable, function, or macro names?  I
>> realize they are used to avoid namespace collisions during compiling and
>> linking, but is there a guide to its practice?

>Can't vounch for C, but in C++ you should never use
>any underscore prefixes.  The use of two underscores
>anywhere in the identifier is reserved for the implemenation.
>The use of a leading under followed by an upper case
>letter is likewise reserved.   The implmenetnation is
>free to polute the global namespace with other symbols
>beginning with underscore, so use of these is at your
>own peril.

>So, if you need to make use of a system symbol with
>leading underscores that's OK (like the _MAX macro in VC++),
>but don't use them when you create your own variables.



Sun, 27 May 2001 03:00:00 GMT  
 Underscore Prefixes

Quote:

>As a newbie to C and C++, is there any rhyme or reason to the practice of using
>one or multiple underscore prefixes to variable, function, or macro names?  I
>realize they are used to avoid namespace collisions during compiling and
>linking, but is there a guide to its practice?

For the most part, names that begin with the underscore are reserved for the
implementation, and the actual rules are complicated enough I find it
easiest just to think of them as reserved for the entire part. If you still
want to know, consult the C and C++ Standards. As I recall, the C++ rules
are somewhere in Chapter 17 of the C++ Standard.

Obscure fact: If you #include <string.h>, then names beginning with str[a-z]
and mem[a-z] are reserved to the implementation. There are some other such
names and headers which I don't remember offhand. This particular rule is
unfortunate, IMO, and I bet most programs violate it in one place or
another.

--
Doug Harrison



Sun, 27 May 2001 03:00:00 GMT  
 Underscore Prefixes
Don't look at code to learn how to code *unless* the code
has been constructed for that purpose by a good coder.

What coders get up to when they think nobody is watching
is often pretty stupendous. I once reviewed code where
a guy had a nice little for loop. The loop manged to allocate,
initialize, and link up a doubly linked list. And he did it with
no body in the loop, all the code between the brackets in
the for() statement.  And he was proud of this.  Then I asked
him to make a little change, and document it. Then he was
no longer proud of it.
--
Dan Evens
(Standard disclaimers etc. No spam please.)



Quote:
> I guess the lesson learned here is, don't look at code in order to learn
from
> it, eh?  It also helps to have a little insight into the history and
standards
> of the language.  At least now I know what the underscores are for (only
about a
> bazillion other quirks to go).



Mon, 28 May 2001 03:00:00 GMT  
 Underscore Prefixes
This rule has another reason: compilers re-edit and augment the source
code with additional code. Sometimes, they have to insert new variables
into it (temporaries, inline function variables). In order to avoid name
clashes with existing identifiers, they affix an underscore (or two) to
all compiler-added variables.

Danny    

Quote:


> >As a newbie to C and C++, is there any rhyme or reason to the practice of using
> >one or multiple underscore prefixes to variable, function, or macro names?  I
> >realize they are used to avoid namespace collisions during compiling and
> >linking, but is there a guide to its practice?

> For the most part, names that begin with the underscore are reserved for the
> implementation, and the actual rules are complicated enough I find it
> easiest just to think of them as reserved for the entire part. If you still
> want to know, consult the C and C++ Standards. As I recall, the C++ rules
> are somewhere in Chapter 17 of the C++ Standard.

> Obscure fact: If you #include <string.h>, then names beginning with str[a-z]
> and mem[a-z] are reserved to the implementation. There are some other such
> names and headers which I don't remember offhand. This particular rule is
> unfortunate, IMO, and I bet most programs violate it in one place or
> another.

> --
> Doug Harrison




Mon, 28 May 2001 03:00:00 GMT  
 Underscore Prefixes
There seems to be two uses for underscores: One--for global variables in
the C run-time library. Second--for the stuff that's outside ANSI C
standard. For example, there's a standard function strcpy(...). MS has
another one that can decide whether the type of parameters is char or
WCHAR. This function is called _tcscpy (actually, it's a macro.)
Basically, whatever is compiler-vendor-specific comes out prepended with
underscores. I've also seen double-underscores, but what these are used
for I don't know--can't see the rule here. I'd say--don't use them.


Mon, 28 May 2001 03:00:00 GMT  
 Underscore Prefixes
The sad truth is that most developers aren't qualified to be doing this for
a living. Just about every developer I've ever dealt with after 16+ years in
the field has been marginal at best. If you think what you wrote about below
was bad, I've seen things that no words can describe. Forgetting about all
the technical and design shortcomings you see in most people's code (I could
go on about this ad nauseam), I often wonder just what people are thinking
sometimes. For instance, the guy who logged the message "deleting log" and
then deletes the log. Or the guy who attempts to write "can't open file" to
the file which failed to open. Or the guy who wants to calculate the number
of screen pages ahead of time based on the number of records in some file.
At one record per screen line and, say, 20 lines per screen, you simply
divide the number of records by 20 (adding 1 if required). If you have a
1000 records for instance, it's 50 pages. Grade 4 math? Well, this
"university grad" instead sets some variable "i" to 1000, then starts a
loop, He subtracts 1 from "i" and after 20 times, he adds 1 to a counter.
The loop then continues and when "i" finally reaches zero, voila, the
counter has 50 in it. It's unreal. I've seen code with comments in it
stating that "I'm not sure if the following code is really required but I'm
putting it in anyway - it seems to work". Perhaps the funniest response I
ever heard to all this though was from a colleague I once worked with. After
showing me some code with an endless loop immediately followed by some more
code which would never be executed since it followed the endless loop (which
had no exit mechanism whatsoever), and included the unbelievable comments
"this code will never be executed", my friend said he understood why. Very
simple. If the loop is long enough and the target machine is fast enough,
the centrifugal force may cause the loop to exit and the code will finally
get a chance to run. You gotta love it.
Quote:

>Don't look at code to learn how to code *unless* the code
>has been constructed for that purpose by a good coder.

>What coders get up to when they think nobody is watching
>is often pretty stupendous. I once reviewed code where
>a guy had a nice little for loop. The loop manged to allocate,
>initialize, and link up a doubly linked list. And he did it with
>no body in the loop, all the code between the brackets in
>the for() statement.  And he was proud of this.  Then I asked
>him to make a little change, and document it. Then he was
>no longer proud of it.
>--
>Dan Evens
>(Standard disclaimers etc. No spam please.)



>> I guess the lesson learned here is, don't look at code in order to learn
>from
>> it, eh?  It also helps to have a little insight into the history and
>standards
>> of the language.  At least now I know what the underscores are for (only
>about a
>> bazillion other quirks to go).



Mon, 28 May 2001 03:00:00 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. Underscore Prefixes

2. Usage of underscores as a prefix

3. Prefixing names with an underscore

4. When is it ok to prefix names with an underscore

5. What does it mean when a member variable or function are prefixed with underscore?

6. custom control prefix (asp or fwd)

7. __gc prefix

8. Infix to Prefix

9. Prefix problem

10. Prefix increment and %= operator produces STRANGE result.

11. prefix vs postfix

12. Prefix notation

 

 
Powered by phpBB® Forum Software