'$' in C identifiers?

S

SaGS

Hi all,

Either I'm missing something big here, or there's a problem with the C
compiler in .NET 2003; and with the C compiler in Visual Studio 6 SP5 too
....

Consider the following 2 lines:

#define TEST_$$$ 0x05
static test_$$$[] = { TEST_$$$ };

The compiler works as if '$' were a valid character to be used for an
identifier, and signals no warnings/errors.

From what I know, during preprocessing it should define the symbol "TEST_"
as the token sequence "$" "$" "$" "0x05" (I put the quotes to mark how I
consider the source text should be broken into tokens), so the second line
becomes

"static" "test_" "$" "$" "$" "[" "]" "=" "{" "$" "$" "$" "0x05" "$" "$"
"$" "}" ";"

(with lots of syntax errors).

What I'm missing here? Or is there a known bug with the C compiler?
 
B

Bo Persson

SaGS said:
Hi all,

Either I'm missing something big here, or there's a problem with the C
compiler in .NET 2003; and with the C compiler in Visual Studio 6 SP5 too
...

Consider the following 2 lines:

#define TEST_$$$ 0x05
static test_$$$[] = { TEST_$$$ };

The compiler works as if '$' were a valid character to be used for an
identifier, and signals no warnings/errors.

Dollar signs are not allowed at all in a C/C++ program. It is not defined
what happens if it is present anyway.


Bo Persson
 
S

SaGS

Bo Persson said:
SaGS said:
Hi all,

Either I'm missing something big here, or there's a problem with the C
compiler in .NET 2003; and with the C compiler in Visual Studio 6 SP5 too
...

Consider the following 2 lines:

#define TEST_$$$ 0x05
static test_$$$[] = { TEST_$$$ };

The compiler works as if '$' were a valid character to be used for an
identifier, and signals no warnings/errors.

Dollar signs are not allowed at all in a C/C++ program.

Yes, I agree.
It is not defined
what happens if it is present anyway.

Actually, the preprocessor grammar says that any non-whitespace character
that is not part of an reserved word/ identifier/ operator/ etc is
considered a separate token. And, should'n the compiler give an error if it
finds invalid characters?
Bo Persson

Merry Christmas to all!
 
B

Bo Persson

SaGS said:
Bo Persson said:
SaGS said:
Hi all,

Either I'm missing something big here, or there's a problem with the C
compiler in .NET 2003; and with the C compiler in Visual Studio 6 SP5 too
...

Consider the following 2 lines:

#define TEST_$$$ 0x05
static test_$$$[] = { TEST_$$$ };

The compiler works as if '$' were a valid character to be used for an
identifier, and signals no warnings/errors.

Dollar signs are not allowed at all in a C/C++ program.

Yes, I agree.
It is not defined
what happens if it is present anyway.

Actually, the preprocessor grammar says that any non-whitespace character
that is not part of an reserved word/ identifier/ operator/ etc is
considered a separate token.

Technically the dollar isn't even recognized as a character...
And, should'n the compiler give an error if it
finds invalid characters?

It does, if you select "Disable Language Extensions". Seems like allowing $
is yet another MS extension!


Bo Persson
 
S

SaGS

Bo Persson said:
...
It does, if you select "Disable Language Extensions". Seems like allowing $
is yet another MS extension!
...

Yes, you're right here. What irritates me is that I haven't found this
documented; maybe it's an "unintended MS extension" (= bug)!

Further investigation shows a little mess about invalid characters. From the
C/C++ perspective, "@" and "`" (backquote) are in the same situation as "$":
these are the only characters in the source charset (7-bit ASCII) that are
not used. (Exception: "@" is used by the MS-specific preprocessor operator
"@#").

But:
(a) If they appear as-is: "@" and "`" always give "error C2018: unknown
character 'hexnumber'", while "$" gives C2018 only in ANSI mode, and is
accepted as (part of) an identifier with MS Extensions.

(b) "$" and "@" can be inserted in an identifier, even in ANSI-compatible
mode, using Unicode Character Names:

int \u0024;

creates a variable named "$". "`" cannot be specified with an UCN (UCN are
only accepted for extended characters, "$" and "@"; this is documented).

(c) As a side-note, error messages regarding identifiers "\u0024" and
"\u0040" refer to "operator `$'" and "operator `@'"!!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top