3 = 'c'` valid in C and C++?

This question already has an answer here:

  • What are the rules about using an underscore in a C++ identifier? 5 answers
  • Can an underscore be used anywhere in an identifier in C? 4 answers

  • All variable names must begin with a letter of the alphabet or an underscore. So yes, it is valid, except if you place it on file scope. (Be careful with double underscore though, it is reserved for the compiler internal use)

    Yet, I wouldn't recommend having variables with names like that, since it may be confusing for the reader.

    From the C++ 2003 standard:

    17.4.3.1.2 Global names [lib.global.names]

    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 uppercase letter (2.11) is reserved to the implementation for any use.
  • Each name that begins with an underscore is reserved to the implementation for use as a name in the global namespace.165
  • 165) Such names are also reserved in namespace ::std (17.4.3.1).


    It's valid in any scope other than global scope1.

    C++17 - n4659 / [lex.name]

    In addition, some identifiers are reserved for use by C++ implementations and shall not be used otherwise; no diagnostic is required.

  • Each identifier that begins with an underscore is reserved to the implementation for use as a name in the global namespace .
  • The standard library actually has an example of it in namespace scope, inherited from boost : The placeholders for std::bind .

    And C has similar phrasing:

    C11 - n1570 / 7.1.3 Reserved identifiers

    Each header declares or defines all identifiers listed in its associated subclause, and optionally declares or defines identifiers listed in its associated future library directions subclause and identifiers which are always reserved either for any use or for use as file scope identifiers.

  • 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.
  • Though the choice of scopes is more limited.


    1 - Not a normative term, just a bastardization of the terms used by the two standards.

    链接地址: http://www.djcxy.com/p/40318.html

    上一篇: C ++:标识符规则不起作用

    下一篇: 3 ='c'在C和C ++中有效吗?