Thread: underscore use in c++

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    34

    underscore use in c++

    Hi just wondering what use the underscore symbol has for example...

    Code:
    _device->SetIndices( _ib );
    is there a reason why the variables begin with an underscore?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    From the compilers perspective, _ is just like a..z, A..Z - you can use it anywhere in a variable name.

    However, there are some rules in the C compiler implementors guides that says that symbols starting with one or two underscores followed by an upper-case is reserved for the compiler. That's not always followed by people writing code, but that's what the C standard says [and thus the C++ standard, as they share these].

    I think MS often uses _ in the beginning of a name to indicate that it's a private member variable (only visible inside the class).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by te5la View Post
    Hi just wondering what use the underscore symbol has for example...

    Code:
    _device->SetIndices( _ib );
    is there a reason why the variables begin with an underscore?
    Some people use it to indicate a private member. There are some stringent rules on the use of the leading underscore in both C and C++, and they are not exactly the same in both languages. In theory you are allowed to use an underscore prefix only if the following character is a lowercase letter. Double underscores are out, as is an underscore followed by an uppercase letter.

    You might think that you should be able to use any name you please if it's not at global scope, but this ignores the issue of macros which expand regardless of scope. The safest thing is to avoid the use of leading underscores entirely. You can still use underscores to mark private members, just place them at the END of the symbol instead of the beginning.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    Registered User
    Join Date
    Jul 2008
    Posts
    34
    Thanks for your help again

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The compiler and its standard library are allowed to:
    1) Define extension keywords and macros (i.e. scope-agnostic things) with any name that starts with a double underscore or an underscore and a capital letter.
    2) Use any name that starts with an underscore and a lowercase letter for variable or type names (i.e. scoped things).

    Therefore, in order not to interfere with the compiler, your program must not:
    1) use the first kind of reserved name (__foo and _Foo) in any way.
    2) define macros or global variables/types with the second kind of reserved name (_foo).

    Not that people care all that much. Here are some of the more blatant violations:
    1) Microsoft loves extension keywords of the second reserved form, e.g. _asm.
    2) I don't even want to start guessing how many projects use a single underscore prefix for their static globals.
    3) X.org's code is littered with function and variable names like _Xsomething.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Just so I am not making a mistake, the following would be valid?

    Code:
    #ifndef HEADER_H
    #define HEADER_H
    
    // blah code
    
    #endif
    I only as due to the no capital after the underscore warning, but im guessing the above is
    ok due to the underscore being between the header file name and the H
    Double Helix STL

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Just so I am not making a mistake, the following would be valid?
    Yes. The underscore followed by capital letter rule is for the beginning of the symbol.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Underscore prefix
    By Petike in forum C Programming
    Replies: 5
    Last Post: 11-23-2008, 11:32 AM
  2. fgets only returns 3 characters
    By Jesdisciple in forum C Programming
    Replies: 88
    Last Post: 09-01-2008, 01:33 PM
  3. underscore and variable names
    By l2u in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 09:57 AM
  4. Leading Underscores
    By laserlight in forum C++ Programming
    Replies: 19
    Last Post: 09-04-2005, 02:16 AM
  5. Reserved namespace and illegal names
    By ^xor in forum C Programming
    Replies: 8
    Last Post: 08-11-2005, 08:38 PM