Underscores

This is a discussion on Underscores within the C++ Programming forums, part of the General Programming Boards category; Ok, I know that leading underscores in identifiers are reserved for use by the implementation. But I forget (if I ...

  1. #1
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879

    Underscores

    Ok, I know that leading underscores in identifiers are reserved for use by the implementation. But I forget (if I ever knew to begin with), is there any rule against trailing underscores? And is there any such rule in C?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Well, i've come across a few professionals who use trailing underscores in their code. It's usually used to signify a member variable of a class. Others prefer using 'm_' for that purpose.

    Are there rules for it in C? I doubt it.

    As long as your code is clear and consistent, then there's no prob, imo. I would probably only use it to signify private/protected member variables in classes.

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    It's usually used to signify a member variable of a class. Others prefer using 'm_' for that purpose.
    By trailing underscore I mean:
    void dosomething_();

    Is there any regulation against that?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #4
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    That's what i was referring to, but i've never seen someone using it for function names, only for variables. I don't think that there are any regulations for these kinds of things, but rather programmer preference. If you have a good enough reason for using it, then by all means, go ahead.

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Well, I wouldn't ever use it for either functions or variable names... but since leading underscores are taboo, I thought perhaps trailing ones were too (since they look so weird ). The reason I'm asking is actually because my header inclusion guards are in the form:
    #ifndef HEADER_H_
    #define HEADER_H_

    And I wanted to be sure that the trailing underscore isn't something terrible. Although I've never heard of using a trailing underscore to indicate a member variable... I guess we learn something new every day.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    As far as I know, trailing underscores are not a standards problem - they're not reserved
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Is there any regulation against that?
    Aside from looking weird, no.
    My best code is written with the delete key.

  8. #8
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    Here is a good overview:


    WHAT IS AN IDENTIFIER?

    Before you can do anything in any language, you must know how to name an identifier. An identifier is used for any variable, function, data definition, etc. In the C programming language, an identifier is a combination of alphanumeric characters, the first being a letter of the alphabet or an underline, and the remaining being any letter of the alphabet, any numeric digit, or the underline.

    Two rules must be kept in mind when naming identifiers.

    The case of alphabetic characters is significant. Using INDEX for a variable name is not the same as using index and neither of them is the same as using InDeX for a variable name. All three refer to different variables.
    According to the ANSI-C standard, at least 31 significant characters can be used and will be considered significant by a conforming ANSI-C compiler. If more than 31 are used, all characters beyond the 31st may be ignored by any given compiler.
    WHAT ABOUT THE UNDERLINE?

    The underline can be used as part of a variable name, and adds greatly to the readability of the resulting code. It is used by some, but not all, experienced C programmers. A few underlines are used for illustration in this tutorial. Since most compiler writers use the underline as the first character for variable names internal to the system, you should refrain from using the underline to begin an identifier to avoid the possibility of a name clash. To get specific, identifiers with two leading underscores are reserved for the compiler as well as identifiers beginning with a single underscore and using an upper case alphabetic character for the second. If you make it a point of style to never use an identifier with a leading underline, you will not have a naming clash with the system.
    I agree it is not good practice to start an identifier with an underscore (as mentioned above). But it is legal to do so.

    Just to point out the current texts on C, most point out that it is not recommended to start an identifier with underscore, but do have examples mention that it is valid to start an identifier with an underscore.

    mr. C
    Mr. C: Author and Instructor

  9. #9
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Your post pertains to leading underscores, but this thread is about the use of trailing underscores.

    Code:
    char _identifier; // leading
    char identifier_; // trailing

  10. #10
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>identifiers beginning with a single underscore and using an upper case alphabetic character
    Interesting to know. So now I know why some compiler identifiers start with single and some with double

    >>but this thread is about the use of trailing underscores.
    Well, I suppose the quote was relevant in that it excludes any mention of trailing underscores being illegal.

    Thanks for the replies everyone
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >identifiers with two leading underscores are reserved for the compiler as well as identifiers
    >beginning with a single underscore and using an upper case alphabetic character for the second.
    Not to forget that any identifier with a leading underscore in either the ordinary or tag name spaces at file scope is reserved.
    My best code is written with the delete key.

  12. #12
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>in either the ordinary or tag name spaces at file scope
    Is "name spaces" distinct from "namespace's"? And what are ordinary and tag namespaces? And by file scope, do you mean declared globally?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Is "name spaces" distinct from "namespace's"?
    Yes. And no. And yes. But also no. Kind of. The concept is the same, but a name space is what lets you do this:
    Code:
    struct name {
      int stuff;
    };
    
    int main()
    {
      name name;
    }
    While a namespace is what lets you do this:
    Code:
    namespace my {
      struct name {
        int stuff;
      };
    }
    
    namespace your {
      struct name {
        int stuff;
      };
    }
    
    int main()
    {
      my::name me;
      your::name you;
    }
    So it's kind of the same, but not. Yet it is.

    >And what are ordinary and tag namespaces?
    Code:
    int ordinary;
    
    struct tag {
      int stuff;
    };
    >And by file scope, do you mean declared globally?
    Yes, if you want to be uncouth about it.
    My best code is written with the delete key.

  14. #14
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>but a name space is what lets you do this:
    Whoa, you can do that??...

    Code:
    int ordinary;
     
    struct tag {
       int stuff;
    };
    It took me a long time to realize the possibility that you weren't mocking me. Do you always speak in parables? So the "ordinary name space" (not namespace) is the category of identifier which designates an individual variable, while the "tag name space" is the category of identifier which designates a type or struct?

    So returning to:
    >>but a name space is what lets you do this:
    You're saying that because the 'name' designating the struct type is in the tag name space, it doesn't conflict with the 'name' designating the name object called 'name'? I'd always assumed that it would be illegal, since you can't do it with built-in datatypes like int.

    And coming around in a full circle, about how name space is sort of like namespace but not, based on the assumption that all my interpretation above is correct... it's because both allow you to use the same name for different things as long as they are in different name spaces/namespaces, but namespaces allow you to reuse names within the same name space and name spaces allow you to reuse names within the same namespace?

    Please correct me if I'm wrong

    **P.S. And are there even any other name spaces than ordinary and tag?
    Last edited by Hunter2; 09-12-2004 at 01:40 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  15. #15
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Whoa, you can do that??...
    If not then everyone was right when they said I was a looney.

    >It took me a long time to realize the possibility that you weren't mocking me.


    >Do you always speak in parables?
    Only when there's a possiblity that I might be mocking you.

    >So the "ordinary name space" (not namespace) is the category of identifier which designates an individual variable
    Well, the ordinary name space is the "other" location for everything that doesn't have its own specialized name space, such as struct/union/enum/class tags, members and labels.

    >while the "tag name space" is the category of identifier which designates a type or struct?
    That sounds about right.

    >I'd always assumed that it would be illegal, since you can't do it with built-in datatypes like int.
    But the built-in types are not identifiers, they're keywords.

    >but namespaces allow you to reuse names within the same name space and name spaces allow you to reuse names within the same namespace?
    Now you're starting to word your explanations like I do, and that's frightening.

    >**P.S. And are there even any other name spaces than ordinary and tag?
    Labels, tags (only one name space even though there could have been four), members, and ordinary. Even more confusingly, a namespace is a name space, but a name space is not necessarily a namespace.
    My best code is written with the delete key.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting underscores into spaces?
    By Cactus in forum C Programming
    Replies: 3
    Last Post: 09-24-2005, 12:34 PM
  2. Leading Underscores
    By laserlight in forum C++ Programming
    Replies: 19
    Last Post: 09-04-2005, 02:16 AM
  3. Why Declare in .H but Define in .CPP ?
    By Krak in forum C++ Programming
    Replies: 10
    Last Post: 07-30-2005, 12:36 AM
  4. functions with preceeding underscores
    By codec in forum C Programming
    Replies: 4
    Last Post: 04-28-2004, 10:07 AM
  5. Hungarian Notation
    By gamegod3001 in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 10-13-2001, 11:17 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21