- 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
Unacceptable solution!!!! How can you name this function "fine"? It's horrible- no XML configuration, no Web Services interface, can't be accessed via RMI, and, the most terrible thing - all done in one layer, you need a proper render layer(characters rendering), processing layer - math, and data layer, remote server that contains character codes, separator codes, etc. This should be combined together using Web Services, and this should be designed as scalable system, better to have few processing layers, and load balancing before them. This will be a real solution
Admin
For the "isn't there a function that already does this" crowd:
I just checked the source for Microsoft's C compiler function xtoa, which is the common working part of itoa(), ltoa(), etc. It is just about identical to the WTF code above, except for the comma stuff. The differences are:
Imagine that! The 'system function' isn't elfin magic at all. It's the same thing you've been despising.
Admin
Ah, yes, apologies, I misread what you intended. That'll teach me to skim when tired.
Yes, you are quite correct that that change would nicely.
Admin
Standard C doesn't have a bool type.
There has been a lot of discussion about 32/64 bit longs, some architectures have 16 bits longs its all a matter of the register size, and if you are seeing straight C nowadays it will either be for hardware drivers or on an embedded system so you are more likely to encounter these shorter registers.
Admin
Maybe I've seen too much bad code in my life, but apart from using a global variable prtbuf instead of passing this as an argument, this function doesn't look that horrible to me.
Admin
It was added in C99.
Admin
Admin
fixed n=-n bug:
[ph] [tehbox] [~] ./x 123
pre : -i=123 neg=0
post: i=0 s=1,2,3
[ph] [tehbox] [~] ./x -123
pre : -i=-123 neg=0
post: i=0 s=-1,2,3
[ph] [tehbox] [~] ./x -128
pre : -i=-128 neg=0
post: i=0 s=-1,2,8
[ph] [tehbox] [~] cat x.c
#include <stdio.h>
void main(int c, char **v) {
//i < 0 && (neg = 1, i = -i);
}
Admin
I hate mandatory invisible default locales. They are a real WTF.
They are the #1 way to make programs that fail more or less inexplicably. For instance there was this one tool program that saved its configuration data by serializing it into XML using a some common library. It also had a default configuration when installed. Only in a different country it always crashed on startup... with a NumberFormatException since the locale-specific decimal separator was different and the configuration had plenty of floating-point numbers.
It is pretty stupid when locale-infested string classes may arbitrarily decide that A and  and Á are actually the same letter and software subtly behaves differently depending on location, with no way to change it apart from creating your own string class. Not to mention the software overhead... in most C++ STL implementations, an innocent-looking '#include <string>' actually brings in around 300k of headers as well.
I would quite prefer this ""wtf"" code.
Admin
http://pastebin.com/m78db4adf
Admin
Did you really, actually make fun of the fact that this algorithm CORRECTLY divides by 10 rather than 1000?
AHAHAHAHA!
Epic fail.
Admin
This post is an embarrassment to the author.
Admin
actually the posted code is a rip from an ANSI C92 string formating function
Admin
Sometimes I am really glad I have my ED instruction. ;-)
Admin
Of course this C might not be ISO, and so might not have the "'" modifier. So, the answer is maybe.
Admin
I miss TopCod3r. He was one of the best things on the site.
Admin
See your doctor if your instruction last longer than 4 hours.
Admin
To the OP:
Apparently you've never actually tried to convert an integer to a C-string before. This is not goofy or "WTF." It is a well-known and elegant algorithm. Look up the wikipedia entry for the function "itoa" http://en.wikipedia.org/wiki/Itoa
Go an implement the commas the way that you described. I will laugh at your result: a base-1000 representation of the number instead of a decimal representation.
Admin
Funny that you should make fun of that code -- because it actually tells me more about you than the guy who wrote it:
you an lack adequate grasp of simple mathematical constructs like modulus
you are not very good at reading and understanding code (it took me about 5 seconds to figure out what the code did and a few seconds more to verify it didn't have any bugs that were very obvious. And I suck at reading code. You must be really, really slow)
Of course, the guy who wrote it should have anticipated that even a short snippet like that might confuse the abnormally obtuse and added a comment that spells out what it does. And I agree the name should reflect better what the function does.
But that still doesn't change the fact that you are an incompetent idiot.
Admin
The only problems here:
Admin
Sure. But since we already can assume it's embedded code, let's also assume the engineer knows the prcoessor is 32-bit, and only 32-bit for now. So therefore all that's missing is a comment to this effect.
Admin
Function is perfectly fine... WTF is this whole thread with 64bit longs, multithreading, %100 instead of %10 etc, not the code pasted by the author.
Cheers to everyone.
Admin
So Jake Vinson is a lead developer and he can't even understand something as simple as formatting an integer value as a string. And he has been at it for 10 years.
The real WTF is what he does as a lead developer. Who on earth would hire someone who after ten years isn't even smart enough to understand a piece of code any college freshman with a programming 101 should be able to grasp.
Where have all these idiots on thedailywtf come from? There used to be smart people here. At least occasionally.
Admin
A few minutes? No, it takes a few seconds to figure out.
Admin
Is that a contraction of "agree would"?
Admin
A sampel solution for how to solve the common bug with INT_MIN in this code. But a common bug in a border case is not a WTF. As you see this code is a bit simpler than the alleged WTF and would still be easier to read even when the commas are added. But it is just a minor bugfix and clean up. Nothin seriosuly wrong with the alleged WTF.
http://clc-wiki.net/wiki/K%26R2_solutions:Chapter_3:Exercise_4
Admin
What's wrong with pointing out stupid mistakes? It might help preventing more of the same in the future.
Perhaps we in Europe's german speaking countries are a little bit more sensitive, but I always find it disturbing to use the term "Nazi" in connection with every-day stuff like spelling or the like. What's wrong with words like "pedant" or "stickler"?
Admin
I do agree about the variables. But single letter transcriptions of certain greek letters are definitely not ok ;o)
Admin
Admin
And in return we USians are seriously considering you for our 51st state (minus Quebec of course)
Admin
Admin
Admin
My version of "perfectly good code" is:
I agree. Crappy code. Especially if you have to waste time trying to figure out what it does. Programmers like this give C a bad name.
Admin
Again: lack of commenting and bad naming hardly makes for a WTF worth of mentioning here.
Maybe Jake is TC. I mean, look at the coincidences: TC is a lead developer (most of the time) that posts absurd WTF solutions as perfectly acceptable solutions while Jake is a team lead that posts perfectly acceptable solutions as WTFs.
And also, since when locale is absolutely mandatory on every piece of code? Last time I checked, locale may or may not be a requirement. Even if this is not embedded code, the coder may have a very good understanding of his target PC and may not needed this to be portable through locations and time (meaning, 64 bit may be overkill for this code). Even though adding locale is trivial to implement, it still will make the code (or the calling code) more confusing and slow. It may even take it to the point where it takes a few minutes to understand it...
Admin
WAOUH
I just found a time machine to travel 10 years in the past. Yeepee!
(and i also found TRWTF: it's Rambaldi)
Admin
this code will run on anything from a $2 microcontroller to a billion dollar supercomputer.
damn it, if its not leaking, slow or giving the wrong answer than WTF is wrong with it?
fuck you style idiots with your hands on your hips complaining about modulus math. you want comments? here's one: step off!
Admin
Admin
I've been programming these kinds of confusing but efficient functions for a few years now. I still have trouble writing and rereading them. However, I do use a few conventions:
Start, end pointers char *s, e; //refers to a string starting at s and ending right before e / ... */ while (s<e) { char c = *s++; }
Pointer, remaining element item; unsigned long count; / ... */ for (;count--;item++) {...}
Though these sort of things may be convoluted, they are C, and they are efficient :)
Admin
Apparently you don't understand how the code works. Yes you DO have to process it one digit at a time.
*--buffer = '0' + (n % 10);
There's no ASCII code for '11' or '100' or '12'. There's only '0' to '9'. If you go modding and dividing by 1000 then the part that converts the number to a character won't work.
Admin
Pursuant to Jake Vinson's suggestion, I restructured the code to use an outer loop that divides by 1000. I understand it's a bit risky to take programming advice from someone who's so junior they don't understand why someone would use the "%" operator in a decimal print routine, but I thought I would see how it went. I do not think the result is an improvement in clarity.
I agree with the other comments that ① the original is a perfectly reasonable piece of code for environments where you don't have a sprintf available; ② it's slightly trickier than it needs to be, probably in order to be fast; ③ a sprintf version would be simpler, if not faster; ④ the original poster is an idiot; ⑤ a one-line comment
would be a major improvement, as would renaming the function "comma_sep".Here's a version using sprintf (and still using statically-allocated 20-byte buffers, to compare apples to apples). As a special bonus, it formats -2,147,483,648 as "-2,147,483,648" instead of "-.,/,),,(-,*,(" (on an ASCII machine).
Admin
Anyway, regarding the article itself, I'll have to join the chorus. This is definitely not a WTF. ‘takes a few minutes to figure out’: Well, I guess that makes me a genius, as I read through the thing in about three seconds. Or the submitter a moron. In all honesty I think the latter is more likely. ‘is trying to accomplish’: Accomplishes. ‘works backwards’: That's inherent in the problem. You simply can't do this working from the other end. Yes, the code has a few minor issues, as previously pointed out by others, but all in all, it's okay. Definitely not a WTF. Oh, and as for the ‘comments defense’: get over yourself. In this case reading the comments would actually have taken more time than reading the code. The art of commenting is also in a large part the art of knowing when to leave them out.
Admin
I do too!
But what REALLY puzzles me is that some writers confuse 'then' and 'than'
It may be understandable for someone for whom English is a second or third language, but this is the kind of basic detail grasped by young kids.
At least, I thought so.
Admin
This is not C++ but that assembly like language called C
Admin
Admin
So what's wrong with variable names? neg stands for negative, n for number, d for digit.
Or may be you prefer code like that:
Admin
Nice function, try calling it with this argument:
Admin
I keyed this in for a stroll down memory lane. Been a while since I used C/C++. The printf statement needs an s instead of an i to format the string, as opposed to what the person above said. Here's a working version for the mildy curious. Now I'll tinker with it to remind myself how C works.
#include<stdio.h>
static char prtbuf[20];
static char *nice_num(long n) { int neg = 0, d = 3; char *buffer = prtbuf; int bufsize = 20;
}
int main(){ printf("the number is %s\n", nice_num(100000)); return 0; }
Here's the output: /tmp/>a.exe the number is 100,000
Admin
You're right, I missed that. So in that case there really is nothing significantly WTF-worthy about this function.
Admin
Admin
It's pretty straighforward, it took me the time to read it to understand ; not a few minutes.