16

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

tshepang
  • 12,111
  • 21
  • 91
  • 136
Gili
  • 86,244
  • 97
  • 390
  • 689
  • 3
    @Zifre: I believe he is asking about the c++ standard specifically as not all compilers are 100% standard compliant and some have their own extensions which may make it legal. – Kevin Loney Jun 01 '09 at 21:09
  • 3
    The C99 standard explicitly allows a platform to define additional legal identifier characters. The Decus C compiler for both PDP11 and VAX allowed $, because most system calls included a $ in their name. "The $ is reserved to Digital". I don't have older C standards or the C++ standard at hand to check if that would have been compliant... – RBerteig Jun 01 '09 at 21:13
  • 8
    @Zifre: Was your comment any more necessary? And since you need to ask, no, he could not simpl try it in his favorite compiler, because that would tell him whether the compiler allowed it, not whether the language did. In C++, that's an important distinction. – jalf Jun 01 '09 at 21:13

7 Answers7

16

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

Kevin Loney
  • 7,483
  • 3
  • 28
  • 33
  • 3
    That's silly that it is allow in VS. MS *always* has to make its own extensions to the standard... – Zifre Jun 01 '09 at 21:03
  • 15
    Every major C++ compiler has a good handful of extensions. MS is not unique in that. – jalf Jun 01 '09 at 21:14
  • 11
    GCC also allows dollar signs. – Max Lybbert Jun 01 '09 at 23:10
  • 2
    You're mising Appendix E - (normative) "Universal-character-names for Identifiers" [extended-id]. However, this adds more letters (either accented or from different scripts), not symbols like $. – MSalters Jun 02 '09 at 12:50
  • Not only does C99 add letters, it also adds digits other than 0-9. See http://www.algonet.se/~afb/d/universalalphas/ (yes, it's for the D language, but D language leverages the C99 standard for its identifiers). – Tim Čas May 16 '11 at 22:11
  • 2
    Note that this is true for C++03 only. C++11 allows universal character names and implementation defined characters in identifiers. – Arne Mertz Apr 17 '13 at 08:28
  • note: it is not allowed to start with underscore. it's the reserved "namespace" for compiler extensions and standard library writers from the time before namespaces. – Alexander Oh Jul 22 '13 at 07:49
  • 1
    @Alex that is not completely correct: starting underscores are allowed if they are not followed by a uppercase letter and if they are not used as a name in global namespace. see **§17.6.4.3.2**: "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 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.**" – Arne Mertz Jul 24 '13 at 09:00
14

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

Nathaniel Flath
  • 15,477
  • 19
  • 69
  • 94
  • 4
    FYI: Although illegal by the standard gcc usually allows them, see http://gcc.gnu.org/onlinedocs/gcc/Dollar-Signs.html#Dollar-Signs – lothar Jun 01 '09 at 21:54
  • 3
    This is to allow backward compatability. It should not be used for any modern code. – Martin York Jun 02 '09 at 00:40
13

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.

Nick Westgate
  • 3,088
  • 2
  • 34
  • 41
Arne Mertz
  • 24,171
  • 3
  • 51
  • 90
8

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.

Artyom
  • 31,019
  • 21
  • 127
  • 215
1

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.

MSalters
  • 173,980
  • 10
  • 155
  • 350
0

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

Zifre
  • 26,504
  • 11
  • 85
  • 105
0

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

David Thornley
  • 56,304
  • 9
  • 91
  • 158