-1

Considering, i have declared variable name as a numeric with underscore.

#include <stdio.h> int main() { char _3 = 'c'; printf("%c\n",_3); } 

I wonder, It's working fine in C and C++. So,is it valid?

10
  • Does it compile? Commented Nov 5, 2017 at 10:38
  • @alk yes it is compile fine. Commented Nov 5, 2017 at 10:38
  • 1
    It's legal, but why would you do that? That's a useless name for a variable. Commented Nov 5, 2017 at 10:38
  • 2
    I'm downvoting this because it shows no research effort. Searching "C valid variable names" gives the answer. Commented Nov 5, 2017 at 11:04
  • 1
    @Peter - This particular code doesn't have UB. Name hiding and resolution guarantees it. Commented Nov 5, 2017 at 11:39

2 Answers 2

8

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).

Sign up to request clarification or add additional context in comments.

Comments

5

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.

4 Comments

_c is not in the global name-space, though.
@Olaf - _c? Not sure I follow you on this
Whatever it is in C++, C has nothing like a "global name-space" as the snippet from the standard show. And _c is at function/block scope here. For C++, it is similar, just more generally phrased.
@Olaf - I've made a clear separation of the two quotes, and explicitly marked the bastardized term. So I really have no clue what issue you are pointing at

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.