- 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
The real WTF is in that in the post's title every word's first letter is capitalized except for "the".
Admin
I've needed to use something like that now and then when something needs to be done between each iteration but not at the beginning or end. For instance (psuedocode):
But, if all he's doing is loop initialization, then yea, that's bad.
Admin
Admin
Are you saying this is wrong?
Run through gcc -E, which only does pre-processing produces:
Admin
Your Delphi example is wrong. It can be just as succinct as your first C example:
In your second example, the begin and end are not much more than the opening and closing braces.
If you are going to use examples like this, at least make them real.
Admin
Missing some of these: {}
Have some of mine. I've got plenty.
{}{}{}{}{}{}{}{}{}{}{}{}
Admin
The word "the" in a title is not capitalized unless it is the first word in the title.
Admin
It depends how you define where pre-processing stops and compilation begins.
(The standard says that compilation behaves as if adjacent string literals are concatenated before pre-processing tokens are converted to normal tokens.)
Admin
DING! DING! DING! We have a winner! Jeez, its kinda scary how many people were confused by that piece of code.
Admin
In response to the first example, I never knew a coder who didn't know "else", but I had to deal with one who didn't know that "else" is not *required* when using "if":
if(condition){
...code
}
else{
;
}
And yes, he felt he had to put a semicolon in there too.
Admin
The real WTF here is that he shouldn't even be using string literals in the first place. What's going to happen if he needs to localize that stuff?
Admin
That isn't true at all.
VB (or BASIC for as far as I can remember) can do the same thing as C...
example:
One line, one instruction per condition:
if condition then DoThis
Multiple lines, multiple instructions per condition:
if condition then
DoThis
DoThisToo
end if
Look Ma! No semicolons or brackets!
Looks cleaner and easier to read to me... but it is simply a matter of opinion.
Let's not go starting language wars unless we actually know the syntax of other languages....
Admin
Considering the nature of this site, the level of basic C knowledge here is just.... wow... and the readiness of people to say it's not possible is pretty "impressive" too... If you don't know what's in the C specification you're bound to either make WTF comments (like the majority of ones here) or WTF code (and compilers... in this case)
It's also amusing that whoever wrote the code, even though it is a WTF, obviously knows more about C than most of the people commenting here...
The most likely cause of this code is using a new (broken) compiler, and just wanting to get the damn thing to build, so going through all the errors and just adding an #ifdef with a quick fix.
Of course, using two if's and no fall-through else is the main WTF... and I'm sure will cause some fun with whoever has to build it with the next different compiler. (Maybe that's how "Mike" came across it?)
Admin
Yes, it does.
Sincerely,
Gene Wirchenko
Admin
Quite frankly, I don't see any real problem with that second snippet. Calling printf 3 times has the desired effect, even if its a bit ugly. I just can't believe that some C compiler doesn't support that type of string concatenation...
Admin
Yeah, this entire thread is worthy of its own WTF. There is nothing wrong with the second snippet.
(a) Literal string concatenation is a standard feature of C
(b) They used that feature, and apparently it didn't work on some 'XYZ' compiler they were using. That's not a WTF.
Working around bugs and missing features in compilers is the order of the day, especially if you are working on embedded systems with weird CPU's and corresponding weird toolchains.
Yes, they could delete the GCC part of the clause, but that would increase the size of their binary in the GCC case because the three strings would be allocated separately in the image. And since this is likely an embeddes system, things like that matters.
If you are going to ridicule people on the internet, please at least make it seem like you yourself are at least somewhat competent. Yes, I am talking about the editors.
WTF, indeed.
Admin
Intentionally left off - I did quote it in its entirety at the end of the post.
Admin
Well, golly. Maybe they didn't use other compilers than GCC and XYZ. No, making it portable to every compiler imaginable is not a good use of your time.
Admin
Cranky!
:)
Admin
I've read all the comments, and I still don't think there's a WTF in the second code snippet. Okay, maybe it could have been written cleaner, but as Froot says: binary size matters in embedded systems. Okay, maybe there's no #else anywhere, but is that really a WTF?
Admin
You young whippersnappers - yer all soft! Back in the day of AppleBasic we had no else block. We'd number our lines and terminate our IF statements with a goto and we LIKED it. Our variables went uphill on both their letters and our strings had a dollar sign to remind us how expensive they were.
There were games written in AppleBasic too, (not that we needed anything more than Lode Runner, Hard Hat Mack, and Rescue Raiders). Turn based strategy like Santa Paravia (how many games today let you go into debt and risk bankruptcy eh?) and full fledged RPG's that not only made you deal with encumbrance but also made charisma into a useful statistic. All that with no stinking else block.
Admin
Instant win! Best part of the thread so far!
Admin
Well, I didn't know that. I've never used the GCC compiler, as all of the compilers I've used are tweaked to match the specific chip. (See my sig.) I had never heard of the double-underscore being reserved until today. In the compiler I'm using now, a SINGLE leading underscore is reserved for the names of C functions being called by the chip's internal OS.
I don't think anyone here's heard of that, either. We just put the #ifndef __FILENAME_H etc. into our coding standard, and we based the standards on another company's standards...
Oh.
Yeah, this is going to get interesting. Excuse me.
Thank you again for the chance to learn from my mistakes.
Admin
The site may be named TheDailyWTF, but here, in my little corner of it, I like to call it "The Code Snippet of the Day." :)
I enjoy these discussions and I'm sure that I, along with a lot of lurkers have learned a lot.
So to those brave souls who do post, wrong or right, I say Post on.
Admin
I was writing BASIC code with elses (And without gotos) at least as early as 1988, and I'm pretty sure I used elses on Commodore PET Basic circa 1980. I call fake.
Admin
Seriously!
WTF!
You need one line:
printf("configuration: %s\n", CONFIG_STRING);
Forget all the #if defined nonsense.
And it works with "%" embedded in CONFIG_STRING.
Unless you happen to be working with a fantastically broken "printf" implementation.
captcha: craptastic (I'll say)
Admin
Regarding double underscores... From the C99 spec (6.10.8, part 4)
"Any other predefined macro names shall begin with a leading underscore followed by an uppercase letter or a second underscore."
then 7.1.3:
"All identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use."
"All identifiers that begin with an underscore are always reserved for use as identifiers with file scope in both the ordinary and tag name spaces."
so for header protection:
__FILENAME_H__ is bad
_FILENAME_H_ is bad
__filename_h_ is bad
_filename_h_ is fine
okay? great.
And really, everybody should be following C99 in terms of this...
Admin
It is pedantic, but pedantic is good. I tested it, and you're right. It's the compiler that does it at compile time, not the preprocessor. I think that only really matters if you're skipping the preprocessing step for some reason. Though it might matter for macro expansion too. Thanks. :-) From now on, I'll refer to it as ANSI compile time string constant concatenation. :-)
Admin
This is not the equivalent. std::string concatenation is obviously a runtime operation, whereas the concatenation of string literals by the compiler is obviously a compile time operation, so it is still useful today and does works in C++. And it is standard.
I think the actual concatenation is done by the compiler itself and not by the preprocessor, because the preprocessor would have to first expand macros and then concatenate the strings, which would needlessly complicate its task.
On the other hand the compiler parser can easily detect that several string literals are following each other and do the concatenation itself.
As for using printf or string concatenation... Bleh. I'd rather avoid printf altogether if I can help it, not for performance reason in this case, but to avoid the ugly and pointless "%s" noise in the source code.
I'd either use puts and compile time string concatenation if it is C, or iostream in C++.
Admin
er..um... what part of "are always reserved" did you miss?
Admin
EAMON!
Plus don't forget to tell them that if you wanted high res graphics, you only got 4 colors!
Admin
I miss those golden years too.
Admin
This construct is not all that uncommon. But "first" needs to be defined as a non-stack based variable (static, etc) or it will be initialized to true every time.
It's almost certain that he DOES want it set to true every time, but regardless, it should not be within the loop.
If your interpretation of the code is true (it's a one time ever initialization), then the if() should be move above the loop.
if my interpretation of the code is true (it's a first element of the loop prefix), then the code in "// Do all loop initialisation here. " should be moved above the for() with the if() and all references to first removed.
Admin
Nah, real pedantry would be pointing out that any "preprocessor" is an implementation detail. The C language does not define a preprocessor. It specifies translation phases, and every conforming implementation must behave as if the implementation performs those phases in the order in which they're specified, as far as any conforming program can determine. An implementation can perform string concatenation in a "preprocessor" or a "compiler" or a "wombat", as long as it occurs in translation phase 6 - after character set conversion and before the conversion of pp-tokens to tokens.
Still, points for knowing about the order of translation phases at all. Many of the C-related comments on TDWTF just provide further evidence that far too few C programmers have actually bothered to learn the language.
Admin
This syntax actually works in a lot of languages, including C: it is perfectly legal to just have an expression as a statement, and conditional operators will only evaluate the right hand term if the left hand term is not enough to figure the result of the expression.
I don't remember stumbling on it ever in any of the C or C++ code I worked with, though. Given the amount of people who enjoy nothing more like packing as much stuff into a single line of code, you'd expect this to be more common.
Admin
That's proper style for titles.
Admin
Actually, I think you'll find a significant majority of C[++] statements are techinically just expressions evaluted for side effects. Something like "x=y;" is just such an example.
But yeah, it's interesting that you don't see "foo() || abort()" around C code.
Admin
You mean "AppleSoft BASIC"? :P I remember doing stuff using both AppleSoft BASIC and Integer BASIC.
Wow - HMM! I have not played that in years! Takes me back to really fun games like Repton and DROL :) Not to mention Lemonade (or Lemonade Stand?), which was written in Integer BASIC...! :) Thanks for the callback!
Peace!
Admin
At least there was some code in the if blocks, at my previous employer (incidentally I 'escaped' after 3 months) the lead developer loved his no-ops.....
OR better still.....
Admin
Thanks for the heads up, I wouldn't ever have guessed :p
Admin
That wasn't really meant for you. ;-)
But people talk about "assignment statements" sometimes, which don't really exist. Probably some of that is just that it's a convenient, if imprecise, term, but probably in part it's not really realizing that's what's going on behind the scenes. I suspect it's quite possible to have a reasonable working understanding of C without at least explicitly realizing that.
Admin
I think it's pretty important that assignment is just another operator and can be used in an expression. It really comes in handy for loop conditions, letting one do things like "while ((c = fgetc(f)) != EOF) { ... }" rather than the awkward "while (1) { c = fgetc(f); if (c != EOF) break; ... }" or the redundant "for (c = fgetc(f); c != EOF; c = fgetc(f)) { ... }"
Admin
Like AppleBasic, I used a version of BASIC back in 1980 that allowed only "IF <condition> THEN GOTO <line number>". Variable names could be only a single-letter ("A") or single-letter$ (A$). If you had more than 26 variables you had to use arrays, hehe. The best part was the interface: old teletype connected to mainframe at 300 baud.
Captcha=giggity...kinda like the sound of a teletype.
Admin
I'm afraid all the guy was doing was your basic loop initialisation, just as easily achieved by placing that code before the actual loop (which saves a variable, a few clock cycles, and makes the whole thing ever-so-slightly easier to understand). Which is why I thought it was "a bit funny", but hey, he owned a whole company and I had barely arrived from university, so who was I to complain?
As for the example you give, I get that a lot lately for some reason, and it is a perfectly valid, if slightly ugly, technique.
Admin
My college roommate did it this way:
if(....)
...
break;
else continue;
...
break;
"Hey John, why isn't this working?" Who needs a textbook when you can guess your way to victory?
Admin
I want to know what compiler "XYZ" was. If it genuinely lacked that feature, well, it's hardly the biggest WTF ever, both GCC and Visual C++ have had features they took time to get right. If it actually had the feature, then the code is more WTF-worthy. ;)
Admin
The WTF in the second part is that the programmer left the standard-dependent version in, when the workaround would work just as well on an standard compiler. It's the same WTF as checking code into version control that has buggy lines commented out.
Admin
The optimal solution would be just
// Can't use string concatenation because we need to support stupid XYZ compiler
printf("configuration: ");
printf( CONFIG_STRING );
printf( "\n");
There's no need to include multiple implementations when there's one that will work in all cases.
Admin
Heh... that's actually required in the coding standards of a project I just weaseled out of
Admin
Colors have nothing to do with resolution. (I love the petty picking apart of others posts on this forum)