underscore and variable names

This is a discussion on underscore and variable names within the C++ Programming forums, part of the General Programming Boards category; Hello I read somewhere that its not recommented to start variable names with underscores such as _list, _some_member, etc.. However, ...

  1. #1
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630

    underscore and variable names

    Hello

    I read somewhere that its not recommented to start variable names with underscores such as _list, _some_member, etc..

    However, I read somewhere else that there is nothing wrong with that..

    Im used to write variable names with underscores (especially class members) so I write _member instead m_member..

    So what is the proper/right way?

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    54
    For class members it's fine. For other variables you should avoid it because compilers tend to use names beginning with _ for things you shouldn't mess around with. For example (my) stdio.h contains:
    Code:
    __MINGW_IMPORT FILE _iob[];	/* An array of FILE imported from DLL. */
    
    #endif /* __DECLSPEC_SUPPORTED */
    
    #define stdin	(&_iob[STDIN_FILENO])
    #define stdout	(&_iob[STDOUT_FILENO])
    #define stderr	(&_iob[STDERR_FILENO])
    If you happened to make a variable named _iob in your program, then stdin, stdout and stderr would stop working in the scope of that variable.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    A huge combination of names beginning with _ are reserved for use by the system.
    Generally speaking, it's much simpler to avoid their use altogether in your code.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Variable names starting with _
    By edesign in forum C Programming
    Replies: 3
    Last Post: 06-05-2009, 03:47 AM

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