Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is _ (single underscore) a valid C++ variable name?

With gcc 4.7.2 this compiles just fine for me:

int main()
{
  int _ = 1;
  return 0;
}

Can I expect this to compile in general? I've read the answers about underscores as prefixes. But what if the underscore isn't prefixing anything?

like image 295
Alec Jacobson Avatar asked Sep 05 '13 22:09

Alec Jacobson


People also ask

Is _ a valid variable name in C?

A variable name can only have letters (both uppercase and lowercase letters), digits and underscore. The first letter of a variable should be either a letter or an underscore. There is no rule on how long a variable name (identifier) can be.

Is _ a valid variable name?

Variable name may not start with a digit or underscore, and may not end with an underscore. Double underscores are not permitted in variable name. Variable names may not be longer than 32 characters and are required to be shorter for some question types: multiselect, GPS location and some other question types.

Is underscore valid in C?

Rules for writing identifierAn identifier can be composed of letters (both uppercase and lowercase letters), digits and underscore '_' only. The first letter of identifier should be either a letter or an underscore.

Can a variable in C start with _?

In C/C++, a variable name can have alphabets, numbers and underscore( _ ) character.


3 Answers

Yes, from The C++ Programming Language, 4th Edition:

A name (identifier) consists of a sequence of letters and digits. The first character must be a letter. The underscore character, _, is considered a letter.

like image 108
Rag Avatar answered Sep 22 '22 20:09

Rag


According to Stroustrup (3rd edition, section 4.9.3), an identifier consists of a sequence of letters and digits. The first character must be a letter. The underscore character is considered a letter.

So, yes, it should be portable.

like image 28
Wayne McGee Avatar answered Sep 22 '22 20:09

Wayne McGee


Yes, _ is a valid identifier, since it meets the syntax requirements. Basically, an identifier consists of an underscore or letter, followed by zero or more other characters that can be underscores, letters or digits. (As of C++ 2011, identifiers can also contain universal character names and other implementation-defined characters, but that's not relevant to your question.)

But it's probably not one that you should use in your own code, unless you're very careful.

As this answer says, quoting the 2003 C++ standard:

Each name that begins with an underscore is reserved to the implementation for use as a name in the global namespace.

Your _ variable isn't in the global namespace, so you're safe in this case, but it's a good idea to avoid defining any identifiers starting with an underscore.

Also, I believe GNU gettext (which provides support for localized messages) uses the name _ for its own purposes. (It was arguably a bad idea for GNU gettext do to this, but it was a convenient choice.)

like image 43
Keith Thompson Avatar answered Sep 22 '22 20:09

Keith Thompson