Thread: underscore and variable names

  1. #1
    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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

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