-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?

Joshua
  • 40,822
  • 8
  • 72
  • 132
msc
  • 33,420
  • 29
  • 119
  • 214
  • Does it compile? – alk Nov 05 '17 at 10:38
  • @alk yes it is compile fine. – msc Nov 05 '17 at 10:38
  • 1
    It's legal, but why would you do that? That's a useless name for a variable. – Baum mit Augen Nov 05 '17 at 10:38
  • 2
    I'm downvoting this because it shows no research effort. Searching "C valid variable names" gives the answer. – klutt Nov 05 '17 at 11:04
  • `_3` is a valid identifier in C and C++. Problem is, it is a reserved identifier for use in the global namespace (C++) or for identifiers at file scope (C) - so code outside the implementation (including this example) which uses such identifiers has undefined behaviour. – Peter Nov 05 '17 at 11:35
  • 1
    @Peter - This particular code doesn't have UB. Name hiding and resolution guarantees it. – StoryTeller - Unslander Monica Nov 05 '17 at 11:39
  • Legal in this case, not in others. I would suggest simply never starting names with underscore and *never* use double underscore, then you don't have to worry about the specifics of the rules. – Jesper Juhl Nov 05 '17 at 11:44
  • @alk code compiling is *useless* as a test of correctness. Lots of code with undefined behaviour compiles just fine but is never the less broken (and *will* sometimes blow up in your face). – Jesper Juhl Nov 05 '17 at 11:47
  • C and C++ are different languages. You are asking two questions here which is strongly discouraged on stack overflow. Not that you should have asked at all, with 10k reps one can be expected to do show some research before asking and show that. – too honest for this site Nov 05 '17 at 12:30
  • This is very basic, simple google search yielded the result what you seek for. Wondering someone even consider this as favorite question. – danglingpointer Nov 05 '17 at 14:10

2 Answers2

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

Ricardo Antunes
  • 397
  • 2
  • 11
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.

Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458