- Feature Articles
- CodeSOD
- Error'd
-
Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Admin
At least that way, typos become compiler errors.
Admin
Except when they don't. Which, as with most stringified things, will happen quite often.
Admin
Isn't this just the stringy equivalent of this?
Admin
I didn't know a '$' could be used in a variable names in C/C++ (I assume). Last time I saw a '$' in a variable names was in PL1 (circa. 1970's).
Silver start to TDWTF for teaching something new.
Admin
Its a little worse than #defines because this only produces a warning:
const char $$B[] = "$$B"; char *ptr = $$B; *ptr = 'x'; printf("%s\n", ptr);
Admin
They're not allowed by default by the ISO C or ISO C++ standards, but the standards do allow for identifiers to contain implementation-defined characters in them. Some compilers such as GCC allow them as an extension, depending on the target architecture.
Admin
There is a benefit to this approach: you can't document strings, but you can document variables.
Admin
That is so true.
In future, I must revise my approach to writing documents. I shall no longer use the deprecated "noun" or "verb" things, which are hideously stringified.
"Variables?" That's the ticket.
Admin
I choose to believe the first three constants are involved in a ridiculously convoluted approach to adding the BEL character to something and the rest was added by people just going along with it.
Admin
Well, at least that won't compile...
Admin
"I didn't know a '$' could be used in a variable names in C/C++ (I assume). Last time I saw a '$' in a variable names was in PL1 (circa. 1970's)."
It can't. A C++ variable name, like a C variable name, can only be letters, digits and underscores, and can't start with a digit. Either their compiler allows nonstandard identifiers or there's something bogus here.
Admin
Ah, it's the compiler. Found this little gem on Microsoft's documentation web site: "The dollar sign $ is a valid identifier character in the Microsoft C++ compiler (MSVC)."
Admin
This isn't as useless as it seems. IDEs and cross-reference tools can find uses of a variable.
Admin
... and grep or similar can find both.
Admin
I suspect that it's there to make porting old programs in other languages to C easier since it may eliminate the need to change variable names. Of course, if you use this feature, you've just made your program utterly non-portable, which I expect is not a downside to Microsoft.
Admin
When used with meaningful strings, this approach is sometimes actually useful.
const char zzz[]="Something interesting and maybe long and difficoult...";
If I'm not too wrong, this way zzz is actually a pointer. You can assign it to other variables (pointers, of course), and then (if needed) a check for equality is the check of a pointer, probably faster than a string with random length. Also, checking for equality of the pointers assures that something was really meant to be set to zzz, and it's not the case that two strings have the same content for random reasons. The utility of all of this, of course, depends on what the program should do...
Admin
Apparently "$" in C/C++ variable names is legal in many cases: https://stackoverflow.com/a/18033976/11026
Admin
Cool! I'm going to start naming my variables $1000, $1000000, $19999, etc. - and claim my code is worth more!
Admin
$$BILLSYALL
Admin
Come compilers collapse duplicate literals, some don't. Some compilers have strings with duplicate values point to the same buffer, some don't.....
Admin
My favorite in the genre of "magic numbers bad named constants good!" was a cow-orker's test code that contained this:
const int SIXTY_SECONDS = 120;
Admin
(effectively) #warnings as errors...
seriously, my CI/CD pipelines along with my Git commit hocks will refuse to process any project which does not have this set to true. (means of setting and detecting depend on compiler/environment,,,
Admin
That is why "Compliance Mode" should always be turned on. (Note for NERW projects it is on by default, for old projects it is off. Also /sdl should always be on.
Admin
Let's hope that code isn't meant to generate PHP code, where the double $ would be used for a variable variable name...