Usage of underscores as a prefix 
Author Message
 Usage of underscores as a prefix

I've been trying to figure out why underscores are used in front of
typedefs, functions and #defines. It was mentioned to me that it had
something to do with namespaces. Can anyone elaborate?

Thanks,
CG



Sat, 04 Jun 2005 22:37:39 GMT  
 Usage of underscores as a prefix


Quote:
> I've been trying to figure out why underscores are used in front of
> typedefs, functions and #defines. It was mentioned to me that it had
> something to do with namespaces. Can anyone elaborate?

Yes, they are reserved for the implementation (compiler). Don't use them
for your own programs. There are exceptions to this but why bother? Simple
rule: don't write software with leading underscores to avoid namespace
conflicts with toolchain implementations.


Sat, 04 Jun 2005 22:39:25 GMT  
 Usage of underscores as a prefix

Quote:

> I've been trying to figure out why underscores are used in front of
> typedefs, functions and #defines. It was mentioned to me that it had
> something to do with namespaces. Can anyone elaborate?

An example: I define

#define swapint(x,y) do { int tmp; tmp = x; x = y; y = tmp; } while (0)

and now I can exchange two int's by writing for example

   int a, b, x, y, z;
   ...
   swapint (a, b);
   swapint (x, y);
   swapint (x, z);

But now I write

   int a, b, tmp;
   swapint (a, tmp);

I hope you can see where the trouble is: I used the same variable name
as in the macro, so we have a problem. This is solved by defining

#define swapint(x,y) do { int _tmp; _tmp = x; x = y; y =;_tmp } while (0)

Now swapint (a, tmp); will work again.



Sat, 04 Jun 2005 23:46:19 GMT  
 Usage of underscores as a prefix
On Tue, 17 Dec 2002 15:46:19 +0000, Christian Bau

Quote:



>> I've been trying to figure out why underscores are used in front of
>> typedefs, functions and #defines. It was mentioned to me that it had
>> something to do with namespaces. Can anyone elaborate?

>An example: I define

>#define swapint(x,y) do { int tmp; tmp = x; x = y; y = tmp; } while (0)

>and now I can exchange two int's by writing for example

>   int a, b, x, y, z;
>   ...
>   swapint (a, b);
>   swapint (x, y);
>   swapint (x, z);

>But now I write

>   int a, b, tmp;
>   swapint (a, tmp);

>I hope you can see where the trouble is: I used the same variable name
>as in the macro, so we have a problem. This is solved by defining

>#define swapint(x,y) do { int _tmp; _tmp = x; x = y; y =;_tmp } while (0)

>Now swapint (a, tmp); will work again.

Bad explanation + bad code = bad idea.

<<Remove the del for email>>



Sun, 05 Jun 2005 09:57:19 GMT  
 Usage of underscores as a prefix


Quote:
> On Tue, 17 Dec 2002 15:46:19 +0000, Christian Bau



> >> I've been trying to figure out why underscores are used in front of
> >> typedefs, functions and #defines. It was mentioned to me that it had
> >> something to do with namespaces. Can anyone elaborate?

> >An example: I define

> >#define swapint(x,y) do { int tmp; tmp = x; x = y; y = tmp; } while (0)

> >and now I can exchange two int's by writing for example

> >   int a, b, x, y, z;
> >   ...
> >   swapint (a, b);
> >   swapint (x, y);
> >   swapint (x, z);

> >But now I write

> >   int a, b, tmp;
> >   swapint (a, tmp);

> >I hope you can see where the trouble is: I used the same variable name
> >as in the macro, so we have a problem. This is solved by defining

> >#define swapint(x,y) do { int _tmp; _tmp = x; x = y; y =;_tmp } while (0)

> >Now swapint (a, tmp); will work again.
> Bad explanation + bad code = bad idea.

Explanation please, genius. And you better come up with a good one.


Sun, 05 Jun 2005 16:58:15 GMT  
 Usage of underscores as a prefix

Quote:

> An example: I define

> #define swapint(x,y) do { int tmp; tmp = x; x = y; y = tmp; } while (0)

> and now I can exchange two int's by writing for example

>   int a, b, x, y, z;
>   ...
>   swapint (a, b);
>   swapint (x, y);
>   swapint (x, z);

> But now I write

>   int a, b, tmp;
>   swapint (a, tmp);

> I hope you can see where the trouble is: I used the same variable name
> as in the macro, so we have a problem. This is solved by defining

> #define swapint(x,y) do { int _tmp; _tmp = x; x = y; y =;_tmp } while (0)

> Now swapint (a, tmp); will work again.

But now

  int a, _tmp;
  /* ... */
  swapint (a, _tmp);

will not work, so what's the point?

Martin



Sun, 05 Jun 2005 21:41:24 GMT  
 Usage of underscores as a prefix

Quote:

> I've been trying to figure out why underscores are used in front of
> typedefs, functions and #defines.

Hopefully you have not encountered this anywhere outside the standard
C library headers/implementation.

Quote:
> It was mentioned to me that it had something to do with namespaces.
> Can anyone elaborate?

The implementation (compiler, standard C library) sometimes needs to
use identifiers for typedefs, preprocessor macros, etc. internally.
Now, imagine the implementation used symbols that the user could also
use, or vice versa. For example, let's say the standard library's
<stdio.h> header begins with

  #ifndef STDIO_H
  #define STDIO_H

and ends with

  #endif

Such an implementaion would fail to compile this perfectly valid program:

  #include <stdio.h>
  int main (void) { int STDIO_H = 0; return STDIO_H; }

It would also change the semantics this program:

  #define STDIO_H "world"
  #include <stdio.h>
  int main (void) { printf ("Hello %s\n", STDIO_H); return 0; }

In short, internal library symbols must never clash with user symbols.
This can only be guaranteed if they are in different namespaces: All
internal library symbols must start with an underscore, user symbols
are not allowed to.

Martin



Sun, 05 Jun 2005 21:58:49 GMT  
 Usage of underscores as a prefix
* Martin{*filter*}opp
| The implementation (compiler, standard C library) sometimes needs to
| use identifiers for typedefs, preprocessor macros, etc. internally.
| Now, imagine the implementation used symbols that the user could also
| use, or vice versa. For example, let's say the standard library's
|<stdio.h> header begins with
|
|   #ifndef STDIO_H
|   #define STDIO_H

<snip>

| In short, internal library symbols must never clash with user symbols.
| This can only be guaranteed if they are in different namespaces: All
| internal library symbols must start with an underscore, user symbols
| are not allowed to.

It's not entirely clear to me what you mean by `symbols' here, but
identifiers with leading underscores are allowed in some
circumstances: at block scope when followed by a lowercase letter, for
example.  Eschewing leading underscores altogether is safe and avoids
the minor burden of having to remember the actual (slightly less
restrictive) rules, but it isn't strictly necessary.

Jeremy.



Mon, 06 Jun 2005 01:30:45 GMT  
 Usage of underscores as a prefix

Quote:

> It's not entirely clear to me what you mean by `symbols' here, but
> identifiers with leading underscores are allowed in some
> circumstances: at block scope when followed by a lowercase letter,
> for example. Eschewing leading underscores altogether is safe and
> avoids the minor burden of having to remember the actual (slightly
> less restrictive) rules, but it isn't strictly necessary.

Thanks for the correction. In fact, I had assumed that all identifiers
with leading underscores are reserved for any use, but now I see that
I was wrong.

Martin



Mon, 06 Jun 2005 06:15:23 GMT  
 Usage of underscores as a prefix
On Wed, 18 Dec 2002 08:58:15 +0000, Christian Bau

Quote:



>> On Tue, 17 Dec 2002 15:46:19 +0000, Christian Bau



>> >> I've been trying to figure out why underscores are used in front of
>> >> typedefs, functions and #defines. It was mentioned to me that it had
>> >> something to do with namespaces. Can anyone elaborate?

>> >An example: I define

>> >#define swapint(x,y) do { int tmp; tmp = x; x = y; y = tmp; } while (0)

>> >and now I can exchange two int's by writing for example

>> >   int a, b, x, y, z;
>> >   ...
>> >   swapint (a, b);
>> >   swapint (x, y);
>> >   swapint (x, z);

>> >But now I write

>> >   int a, b, tmp;
>> >   swapint (a, tmp);

>> >I hope you can see where the trouble is: I used the same variable name
>> >as in the macro, so we have a problem. This is solved by defining

>> >#define swapint(x,y) do { int _tmp; _tmp = x; x = y; y =;_tmp } while (0)

>> >Now swapint (a, tmp); will work again.
>> Bad explanation + bad code = bad idea.

>Explanation please, genius. And you better come up with a good one.

1.  All you have done is exchange the situation where
        swapint(a,tmp);
will not work for one where
        swapint(a,_tmp);
will not work.  Your response does not address the poster's original
question and your suggestion does not alter the situation in any
material way.  That qualifies as a bad explanation on two counts.

2.  Look at the fourth statement inside your do block.  What does
        y =;_tmp
mean to you?  It requires a diagnostic and if it executes it does not
perform the intended swap.  That's two counts of bad code also.

Sorry if you took offense but you really can't expect to post
misleading info here, even if by accident, without someone mentioning
it.  Wait till you catch Dan Pop's attention.

<<Remove the del for email>>



Mon, 06 Jun 2005 09:38:42 GMT  
 Usage of underscores as a prefix
Some comments to newbies (like me):

It is very common to use leading underscore in typedefs with structs, like:

typedef struct _node {
        int val;
        struct _node *next;

Quote:
} node;

This is allowed due to what someone has said, but it's not good practice.
It's interesting that K&R2 don't do that (see p.146)

Other pages of K&R2 on leading underscores say what people have said here:
pp. 35, 192, 241. Here are some excerpts:

"Don't begin variable names with underscore, however, since library routines
often use such names."

"External identifiers that begin with an underscore are reserved for use by
the (standard) library, as are all other identifiers that begin with an
underscore and an upper-case letter or another underscore."

Ricardo.

--
  Ricardo Fabbri
  {*filter*}netic Vision Research Group - USP
  http://www.*-*-*.com/ ~rfabbri



Mon, 06 Jun 2005 08:24:16 GMT  
 Usage of underscores as a prefix


Quote:
> Some comments to newbies (like me):

> It is very common to use leading underscore in typedefs with structs,
> like:

> typedef struct _node {
>      int val;
>      struct _node *next;
> } node;

Common? Why? There is no need. The struct namespace is separate from
typedefs. E.g. it quite common do to this:

typedef struct Node
{
    int val;
    struct Node *pNext;

Quote:
} Node;

Now you can declare the object as per your preference via

struct Node foo;
- or -
Node bar;

This can also help C++ programmers feel more at home in a C only
situation.



Mon, 06 Jun 2005 22:20:42 GMT  
 Usage of underscores as a prefix

Quote:

> > It is very common to use leading underscore in typedefs with structs,
> > like:

> > typedef struct _node {
> >      int val;
> >      struct _node *next;
> > } node;

> Common? Why? There is no need. The struct namespace is separate from
> typedefs.

If so, How to explain code from many respected software? Examples:

http://www.*-*-*.com/ %2B%2Fgtk/gtklist.h&...

Or, from the ImageMagick package (www.imagemagick.org):

typedef struct _Image
{

  ...

  struct _Image
     *previous,
     *list,
     *next;

Quote:
} Image;

This is even worse since there's a leading underscore before a capital letter.

Ricardo.

--
  Ricardo Fabbri
  {*filter*}netic Vision Research Group - USP
  http://www.*-*-*.com/ ~rfabbri



Mon, 06 Jun 2005 22:42:53 GMT  
 Usage of underscores as a prefix


Quote:
>> > It is very common to use leading underscore in typedefs with structs,
>> > like:

>> > typedef struct _node {
>> >      int val;
>> >      struct _node *next;
>> > } node;

>> Common? Why? There is no need. The struct namespace is separate from
>> typedefs.

> If so, How to explain code from many respected software? Examples:

I don't have to explain anyone else's code, C distinguishes struct/union
tag name spaces from typedefs. You can use the same stinking name for the
tag *and* the typedef! End of story. I don't care if it is common to put
un-needed leading underscores on struct tags - you don't need to. Now I'm
so angry I'm going to go have a soda.

Quote:
> http://cvs.gnome.org/bonsai/cvsblame.cgi?file=gtk%2B%2Fgtk/gtklist.h&...
> &root=/cvs/gnome

> Or, from the ImageMagick package (www.imagemagick.org):

> typedef struct _Image
> {

>   ...

>   struct _Image
>      *previous,
>      *list,
>      *next;
> } Image;

> This is even worse since there's a leading underscore before a capital
> letter.

This example is not much different from your original one and the leading
underscore is still un-needed and not recommended.


Tue, 07 Jun 2005 01:33:30 GMT  
 Usage of underscores as a prefix
[regarding underscore-as-prefix, as in]

Quote:
>>> typedef struct _node node;

>> Common? Why? There is no need. The struct namespace is separate from
>> typedefs.



Quote:
>If so, How to explain code from many respected software?

There are any number of possible explanations; the two most likely
are:

 - perhaps the respect is undeserved; or
 - perhaps the software is part of the C implementation on some
   particular system.

Quote:
>Or, from the ImageMagick package (www.imagemagick.org):
>typedef struct _Image { ... } Image;
>This is even worse since there's a leading underscore before a capital letter.

Indeed, this is squarely in the implementor's namespace, and
the ImageMagick folks are not "implementors".

They are, however, caught in something of a bind (which nonetheless
does not justify the use of names like _Image).  They are neither
the implementor, writing the C compiler and libraries for a single
system; nor are they (always) the end-user, writing the final target
program to run on a single system.  Instead, they occupy the position
of a third-party library vendor: they write code that the end-user
will use, and sit in a position halfway between "implementor" and
"end-user".

The C standards do not reserve any name-space for such people.

This is not really a failure of the C standards, which do not *need*
to make such a reservation.  Instead, a third-party-library vendor
can simply reserve their own namespace within the wider "user"
namespace, and announce that to their own end-users.  Given C's
rather primitive name-spaces, it might have been nice if the C
standards had given some sort of guidelines -- but in any case,
third-party vendors should not step on "implementation" names unless
they are willing to limit themselves to implementations on which
their third-party libraries happen to work anyway.
--
In-Real-Life: Chris Torek, Wind River Systems (BSD engineering)
Salt Lake City, UT, USA (4039.22'N, 11150.29'W)

(you probably cannot email me -- spam has effectively killed email)



Tue, 07 Jun 2005 02:29:42 GMT  
 
 [ 19 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Prefixing names with an underscore

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

3. Underscore Prefixes

4. Underscore Prefixes

5. Underscore Usage In Identifiers

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

7. MEM Usage and CPU Usage

8. custom control prefix (asp or fwd)

9. __gc prefix

10. Infix to Prefix

11. Prefix problem

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

 

 
Powered by phpBB® Forum Software