eonil
eonil

Reputation: 86085

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();

Upvotes: 2

Views: 208

Answers (3)

bdonlan
bdonlan

Reputation: 231303

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.

Upvotes: 4

Kevin
Kevin

Reputation: 1921

This is not standard and only Microsoft Visual Studio (that I know of) even allows the '$' character in identifiers.

So if you ever want your code to be portable (or readable to others), I'd say no.

Upvotes: 1

Jonathan Leffler
Jonathan Leffler

Reputation: 754520

No; it is a non-standard extension in some compilers.

Upvotes: 5

Related Questions