Question

What does the C++ standard say about using dollar signs in identifiers, such as Hello$World? Are they legal?

Was it helpful?

Solution

A c++ identifier can be composed of any of the following: _ (underscore), the digits 0-9, the letters a-z (both upper and lower case) and cannot start with a number.

There are a number of exceptions as C99 allows extensions to the standard (e.g. visual studio).

OTHER TIPS

They are illegal. The only legal characters in identifiers are letters, numbers, and _. Identifiers also cannot start with numbers.

In C++03, the answers given earlier are correct: they are illegal. In C++11 the situation changed however:

The answer here is "Maybe":
According to §2.11, identifiers may consist of digits and identifier-nondigits, starting with one of the latter. identifier-nondigits are the usual a-z, A-Z and underscore, in addition since C++11 they include universal-character-names (e.g. \uBEAF, \UC0FFEE32), and other implementation-defined characters. So it is implementation defined if using $ in an identifier is allowed. VC10 and up supports that, maybe earlier versions, too. It even supports identifiers like こんばんは.

But: I wouldn't use them. Make identifiers as readable and portable as possible. $ is implementation defined and thus not portable.

Not legal, but many if not most of compilers support them, note this may depend on platform, thus gcc on arm does not support them due to assembly restrictions.

The relevant section is "2.8 Identifiers [lex.name]". From the basic character set, the only valid characters are A-Z a-z 0-9 and _. However, characters like é (U+00E9) are also allowed. Depending on your compiler, you might need to enter é as \u00e9, though.

They are not legal in C++. However some C/C++ derived languages (such as Java and JavaScript) do allow them.

Illegal. I think the dollar sign and backtick are the only punctuation marks on my keyboard that aren't used in C++ somewhere (the "%" sign is in format strings, which are in C++ by reference to the C standard).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top