using '__' in names

Discussion in 'C Programming' started by Roman Mashak, Aug 17, 2005.

  1. Roman Mashak

    Roman Mashak Guest

    Hello, All!

    I often meet that '_' or '__' is used as prefix to
    functions/macros/variables names. I wonder does it have some strict meaning?
    I didn't find any distinct explanation in C standard or local FAQ.

    With best regards, Roman Mashak. E-mail:
     
    Roman Mashak, Aug 17, 2005
    #1
    1. Advertising

  2. Roman Mashak wrote:
    > I often meet that '_' or '__' is used as prefix to
    > functions/macros/variables names. I wonder does it have some strict meaning?


    If I remember: all symbols that starts with an underscore or contains
    two consecutive underscores are reserved. This means that no portable
    programs can uses this sort of identifiers.

    Daniele
     
    Daniele Benegiamo, Aug 17, 2005
    #2
    1. Advertising

  3. Roman Mashak

    Artie Gold Guest

    Roman Mashak wrote:
    > Hello, All!
    >
    > I often meet that '_' or '__' is used as prefix to
    > functions/macros/variables names. I wonder does it have some strict meaning?
    > I didn't find any distinct explanation in C standard or local FAQ.
    >
    > With best regards, Roman Mashak. E-mail:
    >
    >

    In general, such names are reserved for the implementation. Do *not* use
    names like this in code you write.[1]

    HTH,
    --ag

    [1] There are situations where it's legal, but it's still a Bad Idea.

    --
    Artie Gold -- Austin, Texas
    http://goldsays.blogspot.com (new post 8/5)
    http://www.cafepress.com/goldsays
    "If you have nothing to hide, you're not trying!"
     
    Artie Gold, Aug 17, 2005
    #3
  4. Daniele Benegiamo wrote:
    > Roman Mashak wrote:
    > > I often meet that '_' or '__' is used as prefix to
    > > functions/macros/variables names. I wonder does it have some strict meaning?

    >
    > If I remember: all symbols that starts with an underscore or contains
    > two consecutive underscores are reserved. This means that no portable
    > programs can uses this sort of identifiers.


    Identifiers starting with an underscore are reserved, there is no such
    restriction on identifiers containing consecutive underscores.

    Robert Gamble
     
    Robert Gamble, Aug 17, 2005
    #4
  5. Roman Mashak

    Jack Klein Guest

    On Wed, 17 Aug 2005 11:38:51 +0900, "Roman Mashak" <>
    wrote in comp.lang.c:

    > Hello, All!
    >
    > I often meet that '_' or '__' is used as prefix to
    > functions/macros/variables names. I wonder does it have some strict meaning?
    > I didn't find any distinct explanation in C standard or local FAQ.


    All symbols beginning with two underscores ("__") or one underscore
    followed by a upper case letter ("_A" through "_Z"), are reserved for
    the implementation (compiler, its headers and library) in all
    contexts.

    All symbols beginning with an underscore followed by a lower case
    letter ("_a" through "_z") are also reserved for the implementation at
    file scope.

    If you look at a header supplied by your compiler, you might see that
    it includes something like this at the top:

    #ifndef __STDIO_H__
    #define __STDIO_H__

    /* contents of header */

    #endif

    The point is that the standard reserves some identifiers for the
    implementation so that it can define its own macros, data types,
    internal helper functions, and non-standard extensions that will not
    clash with any identifiers a programmer uses, if the programmer
    understands and follows the rules.

    Unfortunately, the vast majority of C (and C++) programmers do not
    understand the rules. Most of them still define include guard macros
    in their header files like this:

    my_header.h:
    #ifndef __MY_HEADER_H__
    #define __MY_HEADER_H__
    /* ... */
    #endif

    Because they see it in compiler supplied headers and somehow think it
    is the thing to use in headers.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Aug 17, 2005
    #5
  6. Roman Mashak

    Jack Klein Guest

    On Wed, 17 Aug 2005 04:49:31 +0200, Daniele Benegiamo
    <> wrote in comp.lang.c:

    > Roman Mashak wrote:
    > > I often meet that '_' or '__' is used as prefix to
    > > functions/macros/variables names. I wonder does it have some strict meaning?

    >
    > If I remember: all symbols that starts with an underscore or contains
    > two consecutive underscores are reserved. This means that no portable
    > programs can uses this sort of identifiers.
    >
    > Daniele


    That's a mix of incorrect C and incorrect C++.

    See my reply to the OP for the actual C reservations. The double
    underscore "__" is free for any use in C if they are not the first two
    characters.

    While it is off-topic here, C++ reserves symbols with "__" anywhere in
    them, but that's a language of a different color.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Aug 17, 2005
    #6
  7. Robert Gamble wrote:
    > Daniele Benegiamo wrote:
    > Identifiers starting with an underscore are reserved, there is no such
    > restriction on identifiers containing consecutive underscores.


    ANSI have reserved them for compiler writers:

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm/eleme_5.asp
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm/eleme_6.asp


    Not related to underscores, but maybe useful to Roman and others,
    general identifier names have other restrictions (extracted from
    http://www.informit.com/guides/content.asp?g=cplusplus&seqNum=185&rl=1):

    The reserved standard C function names are:
    * "is" followed by a lowercase letter, e.g. "isspace"
    * "mem" followed by a lowercase letter, e.g. "memset"
    * "str" followed by a lowercase letter, e.g. "strcmp"
    * "to" followed by a lowercase letter, e.g. "tolower"
    * "wcs" followed by a lowercase letter, e.g. "wcstof"

    The following macros are reserved:
    * Identifiers that start with E followed by a digit or an uppercase letter
    * Identifiers that start with LC_ followed by an uppercase letter
    * Identifiers that start with SIG or SIG_ followed by an uppercase letter


    Daniele
     
    Daniele Benegiamo, Aug 17, 2005
    #7
  8. Daniele Benegiamo wrote:
    > Robert Gamble wrote:
    > > Daniele Benegiamo wrote:
    > > Identifiers starting with an underscore are reserved, there is no such
    > > restriction on identifiers containing consecutive underscores.

    >
    > ANSI have reserved them for compiler writers:


    Identifiers starting with an underscore are reserved. If an identifier
    contains consecutive underscores, that does not in and of itself make
    it reserved. If it starts with two consecutive underscores then it
    obviously starts with one underscore which is covered in the first part
    of my statement. Read carefully.

    Robert Gamble
     
    Robert Gamble, Aug 17, 2005
    #8
  9. Roman Mashak

    Ben Pfaff Guest

    "Robert Gamble" <> writes:

    > Daniele Benegiamo wrote:
    >> Robert Gamble wrote:
    >> > Daniele Benegiamo wrote:
    >> > Identifiers starting with an underscore are reserved, there is no such
    >> > restriction on identifiers containing consecutive underscores.

    >>
    >> ANSI have reserved them for compiler writers:

    >
    > Identifiers starting with an underscore are reserved. If an identifier
    > contains consecutive underscores, that does not in and of itself make
    > it reserved.


    I think there's some confusion with C++ here, which, unlike C,
    does indeed reserve all identifiers that contain consecutive
    underscores. From C++98:

    $ 17.4.3.1.2 Global names [lib.global.names]
    $
    $ 1 Certain sets of names and function signatures are always
    $ reserved to the implementation:
    $
    $ - Each name that contains a double underscore (__) or begins
    $ with an underscore followed by an upper-case letter (2.11)
    $ is reserved to the implementation for any use.

    --
    "Large amounts of money tend to quench any scruples I might be having."
    -- Stephan Wilms
     
    Ben Pfaff, Aug 17, 2005
    #9
  10. Roman Mashak

    Roman Mashak Guest

    Hello, Jack!
    You wrote on Tue, 16 Aug 2005 22:19:54 -0500:

    JK> All symbols beginning with an underscore followed by a lower case
    JK> letter ("_a" through "_z") are also reserved for the implementation at
    JK> file scope.
    If I unerstand you right, standard also doesn't recommend these type of
    symbols in your own libraries, application etc.? Also I met this in many
    SDK, developed by many respected companies.
    JK> If you look at a header supplied by your compiler, you might see that
    JK> it includes something like this at the top:

    With best regards, Roman Mashak. E-mail:
     
    Roman Mashak, Aug 17, 2005
    #10
  11. Roman Mashak

    Jack Klein Guest

    On 16 Aug 2005 20:09:54 -0700, "Robert Gamble" <>
    wrote in comp.lang.c:

    > Daniele Benegiamo wrote:
    > > Roman Mashak wrote:
    > > > I often meet that '_' or '__' is used as prefix to
    > > > functions/macros/variables names. I wonder does it have some strict meaning?

    > >
    > > If I remember: all symbols that starts with an underscore or contains
    > > two consecutive underscores are reserved. This means that no portable
    > > programs can uses this sort of identifiers.

    >
    > Identifiers starting with an underscore are reserved, there is no such
    > restriction on identifiers containing consecutive underscores.


    Your statement is too broad.

    Every identifier starting with an underscore in the following snippet
    is valid in the application's namespace in the context where it is
    declared:

    #include <stdio.h>

    int main(void)
    {
    int _123 = 0;
    int _abc;
    double _;
    typedef struct _xyz
    {
    int _456;
    int _all;
    } _qrs;
    _qrs _def = { 3, 4 };
    _abc = _def._456;
    _abc *= _def._all;
    _ = _abc / 2.0;
    printf("%f\n", _);
    return _123;
    }

    And it must produce the output "6.000000" on any conforming C
    implementation.

    Here is the actual text of the standard for the two cases where the
    underscore makes identifiers reserved:

    "— All identifiers that begin with an underscore and either an
    uppercase letter or another underscore are always reserved for any
    use.

    — All identifiers that begin with an underscore are always reserved
    for use as identifiers with file scope in both the ordinary and tag
    name spaces."

    Note that leading underscores are specifically not reserved at block
    scope as long as they are not followed by a second underscore or an
    upper case letter.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Aug 17, 2005
    #11
  12. Roman Mashak

    Jack Klein Guest

    On Wed, 17 Aug 2005 13:15:21 +0900, "Roman Mashak" <>
    wrote in comp.lang.c:

    > Hello, Jack!
    > You wrote on Tue, 16 Aug 2005 22:19:54 -0500:
    >
    > JK> All symbols beginning with an underscore followed by a lower case
    > JK> letter ("_a" through "_z") are also reserved for the implementation at
    > JK> file scope.


    Actually, this one is wrong, my mistake in quoting from memory instead
    of looking it up in the standard. All identifiers beginning with an
    '_' are reserved at file scope, so "_0" through "_9" and just plain
    "_" are reserved outside of a block.

    > If I unerstand you right, standard also doesn't recommend these type of
    > symbols in your own libraries, application etc.? Also I met this in many
    > SDK, developed by many respected companies.


    When is says "reserved for the implementation" it means exactly that,
    and your applications and libraries are not part of the
    implementation.

    Third parties who develop libraries for distribution certainly don't
    want their users complaining that including one of their header files
    or linking to one of their libraries causes the compile or link to
    break, or even worse, the program builds but suddenly has "strange"
    errors. Such libraries are often written by the compiler implementer,
    in which case they have a perfect right to use those symbols, or for a
    particular compiler and the vendor has tested it to make sure there
    are no conflicts.

    > JK> If you look at a header supplied by your compiler, you might see that
    > JK> it includes something like this at the top:
    >
    > With best regards, Roman Mashak. E-mail:


    If you do you violate the implementation name space, the C standard
    doesn't define what will happen. Most likely any such identifier that
    you use won't match one used by your implementation and the program
    will work just fine. If the name does clash, you may get the compiler
    or linker errors I mentioned above, or the strange problems.

    Here are three lines from the stdio.h standard header supplied with
    Microsoft Visual C++ 6.0:

    #ifndef _INC_STDIO
    #define _INC_STDIO

    ....and:

    #endif /* _INC_STDIO */

    So what do you think will happen if I use that implementation on this
    source file:

    #include <stdio.h>

    int main(void)
    {
    int _INC_STDIO = 0;
    return _INC_STDIO;
    }

    ???

    Replace the string "_INC_STDIO" with nothing, and see what the
    compiler has to work with after the preprocessor phase.

    The point is, while there are a few cases where you can use a leading
    underscore, it's not worth remembering the rules. Just never define
    any identifier, including macros, in your programs that begin with an
    underscore and you'll never have that particular problem. There is
    absolutely no compelling need or any possible gain to doing so.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Aug 17, 2005
    #12
  13. Roman Mashak wrote on 17/08/05 :
    > I often meet that '_' or '__' is used as prefix to functions/macros/variables
    > names. I wonder does it have some strict meaning? I didn't find any distinct
    > explanation in C standard or local FAQ.


    It has no peculiar meaning, but it belongs to the implementation
    namespace. Users should not have names starting with with _[_A-Z]

    --
    Emmanuel
    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
    The C-library: http://www.dinkumware.com/refxc.html

    "C is a sharp tool"
     
    Emmanuel Delahaye, Aug 17, 2005
    #13
  14. On 16 Aug 2005 20:09:54 -0700, in comp.lang.c , "Robert Gamble"
    <> wrote:

    >Identifiers starting with an underscore are reserved, there is no such
    >restriction on identifiers containing consecutive underscores.


    Yes there is:
    All identifiers that begin with an underscore and either an
    uppercase letter or another underscore are always reserved for any
    use

    ..
    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

    ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
     
    Mark McIntyre, Aug 17, 2005
    #14
  15. Mark McIntyre wrote:
    > On 16 Aug 2005 20:09:54 -0700, in comp.lang.c , "Robert Gamble"
    > <> wrote:
    >
    > >Identifiers starting with an underscore are reserved, there is no such
    > >restriction on identifiers containing consecutive underscores.

    >
    > Yes there is:
    > All identifiers that begin with an underscore and either an
    > uppercase letter or another underscore are always reserved for any
    > use


    See my response to Daniele.

    Robert Gamble
     
    Robert Gamble, Aug 18, 2005
    #15
  16. Mark McIntyre <> writes:
    > On 16 Aug 2005 20:09:54 -0700, in comp.lang.c , "Robert Gamble"
    > <> wrote:
    >
    >>Identifiers starting with an underscore are reserved, there is no such
    >>restriction on identifiers containing consecutive underscores.

    >
    > Yes there is:
    > All identifiers that begin with an underscore and either an
    > uppercase letter or another underscore are always reserved for any
    > use


    I think the reference was to identifiers like foo__bar.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Aug 18, 2005
    #16
  17. Keith Thompson wrote:
    > Mark McIntyre <> writes:
    > > On 16 Aug 2005 20:09:54 -0700, in comp.lang.c , "Robert Gamble"
    > > <> wrote:
    > >
    > >>Identifiers starting with an underscore are reserved, there is no such
    > >>restriction on identifiers containing consecutive underscores.

    > >
    > > Yes there is:
    > > All identifiers that begin with an underscore and either an
    > > uppercase letter or another underscore are always reserved for any
    > > use

    >
    > I think the reference was to identifiers like foo__bar.


    Exactly. I was trying to say that having consecutive underscores alone
    doesn't put it into the reserved category, I thought my original
    wording was pretty clear, I guess I was wrong.

    Robert Gamble
     
    Robert Gamble, Aug 18, 2005
    #17
  18. On 17 Aug 2005 16:13:15 -0700, in comp.lang.c , "Robert Gamble"
    <> wrote:

    >Mark McIntyre wrote:
    >> On 16 Aug 2005 20:09:54 -0700, in comp.lang.c , "Robert Gamble"
    >> <> wrote:
    >>
    >> >Identifiers starting with an underscore are reserved, there is no such
    >> >restriction on identifiers containing consecutive underscores.

    >>
    >> Yes there is:

    >
    >See my response to Daniele.


    I read your original post as relating to identifiers starting with
    underscores, since thats how the sentence begins. If you meant it to
    relate to having two underscores elsewhere in the macro name, that
    wasn't at all clear.
    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

    ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
     
    Mark McIntyre, Aug 18, 2005
    #18
  19. Mark McIntyre wrote:
    > On 17 Aug 2005 16:13:15 -0700, in comp.lang.c , "Robert Gamble"
    > <> wrote:
    >
    > >Mark McIntyre wrote:
    > >> On 16 Aug 2005 20:09:54 -0700, in comp.lang.c , "Robert Gamble"
    > >> <> wrote:
    > >>
    > >> >Identifiers starting with an underscore are reserved, there is no such
    > >> >restriction on identifiers containing consecutive underscores.
    > >>
    > >> Yes there is:

    > >
    > >See my response to Daniele.

    >
    > I read your original post as relating to identifiers starting with
    > underscores, since thats how the sentence begins. If you meant it to
    > relate to having two underscores elsewhere in the macro name, that
    > wasn't at all clear.


    In the first part I used "starting with", in the second I used
    "containing". If I meant identifiers starting with consecutive
    underscores, that is what I would have said. I assumed that one could
    easily deduct that an identifier beginning with multiple underscores
    would be reserved as per the first part of my statement. I don't think
    the statement was really that poorly worded but I will try to be more
    clear going forward.

    Robert Gamble
     
    Robert Gamble, Aug 19, 2005
    #19
  20. Roman Mashak

    Cool Guy Guest

    Robert Gamble <> wrote:

    > I don't think
    > the statement was really that poorly worded but I will try to be more
    > clear going forward.


    Had you not misused the comma, it might've been clearer, IMO.
     
    Cool Guy, Aug 19, 2005
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paddy McCarthy
    Replies:
    3
    Views:
    749
    Anthony J Bybell
    Sep 24, 2004
  2. Bob
    Replies:
    1
    Views:
    418
    Lucas Tam
    Jul 30, 2004
  3. Lewis G. Pringle, Jr.
    Replies:
    0
    Views:
    628
    Lewis G. Pringle, Jr.
    Sep 30, 2003
  4. Craig
    Replies:
    0
    Views:
    472
    Craig
    Feb 9, 2004
  5. Carl
    Replies:
    0
    Views:
    539
Loading...

Share This Page