Search code examples
c++cspecial-charactersidentifier

Is it safe using `$` as an identifier in C/C++?


Is it safe to use $ character as part of identifier in C/C++? Like this,

int $a = 10;
struct $b;
class $c;

void $d();

Solution

  • No. The C standard only guarantees the use of uppercase and lowercase English letters, digits, _, and Unicode codepoints specified using \u (hex-quad) or \U (hex-quad) (hex-quad) (with a few exceptions). Specific compilers may allow other characters as an extension; however this is highly nonportable. (ISO/IEC 9899:1999 (E) 6.4.2.1, 6.4.3) Further note that the Unicode codepoint method is basically useless in identifiers, even though it is, strictly speaking, permitted (in C99), as it still shows up as a literal \uXXXX in your editor.