• (cs)

    I don't know what kind of self-respecting C programmer uses double-parentheses ((when single will do))

  • (cs)

    I think there's a secret message hidden in that code...

  • (unregistered)

    This is one of the times I actually said WTF out loud (well, the full phrase).

  • (unregistered) in reply to Maurits

    <font color="#000099">for</font> (i=0,j=0;i<strlen (buff);i++,j="" color="#000099"><font {="">switch</font>(buff[i]) {
    case <font color="#990000">'+'</font>:
    value[j] = <font color="#990000">' '</font>;
    buff[i] = <font color="#990000">' '</font>;
    break;
    case <font color="#990000">'%'</font>:

    You can't nest <font color="#0000ff"><font face="Courier New">case</font> <font color="#000000">statements in a <font color="#0000ff"><font face="Courier New">for</font> <font color="#000000">loop.</font></font></font></font>
    </strlen>

  • (cs)

    hahahhhahaha!!!! BLIND!

    thanks for reminding me how much i hate C. last time i really hated it was when i had to convert a C funtion to vb.net, and there were no comments at all!!!!!! kill this language now!!!! move over to vb!!!!!!!! (btw if u could use copper wires it would probably make it much easier!!!)

  • (cs) in reply to AndrewVos

    BTW well done bro, this is the funniest site i've ever seen, been coming home and checking my rss for the new WTF's! some ppl are soooo stupid! anyways, tnx for the free laughs!

  • (unregistered) in reply to
    You can't nest case statements in a for loop.


    There's a problem in the HTML. View source and you'll see that it's supposed to say:

    for (i=0,j=0;i<strlen(buff);i++,j++)
    switch
    {
    case '+':

    etc
  • (unregistered)

    oy! it gets worse:

    for (i=0,j=0;i<strlen(buff);i++,j++)

    strlen is O(N) for the length of the buffer, effectively turning this into an O(N^2) algorithm. I'd say it's C written with a VB accent.

  • (unregistered) in reply to

    Of course you can't, but I expect someone would know that '<' is the open tag for html elements so all text after that point until the next tag hasn't been displayed :)

    for(i=0,j=0;i<STRLEN(BUFF);I++,J++)

    switch

  • (unregistered)

    begin(pedantry):
    seems Alex got a little overexcited when posting.. and stated that this was an "ASCII to HEX function".. and unless I'm missing something it is indeed quite the opposite
    end

    Although I must say these things lose some of their impact when they repetitive bits are snipped out :) still a good laugh.

    And just to add some balance to all these silly comments about C being evil, or VB being evil for that matter: Get over it, on both/all sides. Languages for problems, any sufficiently stupid person can write utterly horrific code in any language. The worst cases in some languages are perhaps wors than those in others, just as the most elegent solution for a given problem in one language might be more elegent than the same in another. (or the most efficient, or short, or readable, etc)

    (Don't have an account, but I'm David)

  • (cs)

    I hope he gets paid per line of code. Or wait. I don't. Oh nevermind.

  • (cs) in reply to Razzie

    WTF! A VB programmer coding in C! ;)

  • (unregistered)

    Yeah, this totally looks like a VB programmer trying to write in C.

  • (unregistered)

    I am disturbed by seeing ugly stuff like this in the beautiful C-language.

    Maybe this kind of stuff should be censored when it's in C, and only shown when written in VB (or BASIC V2.0?), so you won't be responsible for making people like me depressed. I feel this WTF is intrusive, and i feel that my personal life and even my will to live has been compromised.

    It is unresposible to publish stuff like this on an open forum, and the poster clearly have not considered the consequenses it could have. To prevent any serious injury in the future (maybe some people will kill themself when seeing this), i'm planning to sue the owner of this website.

  • (cs)

    ah, but when you use the correct compiler and the correct optimizer flags and pray to the correct deity, this code actually compiles to the fastest, most optimized, least memory hungry binary.

  • (cs)

    The full file isn't quite the full file. It's just the section that's posted. Even though I'm sure I could reconstruct it, I want to see it for myself!

    Talk about your massively unrolled loop.

  • (unregistered)

    Please now, let us not condem an entire programming language because some idiot doesn't know how to use it.

    Here is a more reasonable and concise "hard core" bit of 'C' code to ponder...

    If you're really "hard core", you'll probably recognize the following code snippet as Duff's Device. Otherwise, you'll probably not be able to figure out what it does, or how.

       register n = (count + 7) / 8;      /* count > 0 assumed */
       switch (count % 8)
       {
       case 0:        do {  *to = *from++;
       case 7:              *to = *from++;
       case 6:              *to = *from++;
       case 5:              *to = *from++;
       case 4:              *to = *from++;
       case 3:              *to = *from++;
       case 2:              *to = *from++;
       case 1:              *to = *from++;
                          } while (--n > 0);
       }

  • (cs) in reply to Son of Guayo
    Son of Guayo:
    ah, but when you use the correct compiler and the correct optimizer flags and pray to the correct deity, this code actually compiles to the fastest, most optimized, least memory hungry binary.


    Haha. *sob* I would advise you to take a class in assembly language, to avoid becoming a WTF yourself in the future. ;p

    It used to be that with a well-written C program you could compile down to vector jumps with switch statements and judicious use of registers. To be honest I don't know how it would be done in these days of huge caches and long pipelines, but what I have seen is usually just extrapolations on the theme.

    Not... this. Eek.

    I take your point that a compiler could refactor this whole pile into one tiny, tight routine. Except that compiler doesn't exist, and if it did we'd all write shitty code because the compiler'd always clean it up for us. It will, a little, but this is whole levels of shitty beyond that.
  • (cs) in reply to

    :
    And just to add some balance to all these silly comments about C being evil, or VB being evil for that matter: Get over it, on both/all sides. Languages for problems, any sufficiently stupid person can write utterly horrific code in any language. The worst cases in some languages are perhaps wors than those in others, just as the most elegent solution for a given problem in one language might be more elegent than the same in another. (or the most efficient, or short, or readable, etc)

    (Don't have an account, but I'm David)

    I have to agree with David (get an account!! [:)]). I have used both C (and C++ for that matter) and VB (and VB.Net too), and both have their charms.

    If I need something really quickly I will use VB.Net, as it's less likely to break with fast, unverified programming.

    I will also be trying some C# this year I think, just to see how that flows.

    Drak

  • (cs) in reply to Drak

    C# flows very nicely. I used to favour C++, but now i get the convenience of C# (which is about as convenient as VB.Net). When you need to do something more "traditional" (eg: some fun pointer arithmetic to piss off some auditors - modulus based ring buffers are easily hidden) you can use unchecked { ... } blocks for allowing overflow, and unsafe { ... } blocks for use of pointers and other "fun".

    Theres also some other classes and statements for dealing with pointers safely in normal code. unsafe/unchecked blocks require full security permission to execute (for obvious reasons).

    Also my C# tip of 2004: The sealed modifier is used to prevent inheritance of a class. What is undocumented is that sealed can also be used as a modifier to a function, preventing overrides of just that function.

  • (unregistered) in reply to

    What's the point? It seems to me that the effect can be archieved in more readable and as fast/faster code.

  • (cs)

    wow, VB coder with a chip on his shoulder. [8-|]

  • (unregistered) in reply to

    :
    It is unresposible to publish stuff like this on an open forum

    UNRESPONSIBLE? What kind of word is that? Irresponsible please! That's the problem with you C-type programmers, lack of communication skills.

    A humble VB programmer with a penchant for [the English] language.

  • (cs) in reply to im_not_jose

    hehe... vb and c, good for various things, but usually crap if you are trying to do the same thing.

    i stick to C/C++, because I feel it looks a bit better on the resume and if the topic ever arised, I would rather impress an interviewer with knowledge of something OOD.

    but nowadays, the gap is being bridged, 'specially with VB.NET and C#... is there any difference? they are both compiled into MS IL anyways, just a difference coding preference

    but you would have to admit, C++ is slightly better than VB, although the difference is not great in many respects, but its often harder to learn to be a good C coder/

  • (cs)

    Would the following C++ crap be considered a more concise and ugly method? I'm hoping it works, and assuming in, out are char* and len is the length of in. I'm even nice and allocating the output buffer :D

    [code language="c#"]
    

    #define hexdig(c) (c>9)?:('0'+c):(56+c)
    out=alloc(2*len);
    for(i=0;i<len;i++)
    {
     out[2*i]=hexdig(in[i]&15);
     out[2*i+1]=hexdig(in[i]&240);
    }

    [/code]

    Notes:
    ('A'+c-9) is too much code, so i use (56+c)

  • (cs) in reply to phx

    <FONT style="BACKGROUND-COLOR: #efefef">Spot the extra bloody colon.</FONT>

  • (cs) in reply to
    :

    :
    It is unresposible to publish stuff like this on an open forum

    UNRESPONSIBLE? What kind of word is that? Irresponsible please! That's the problem with you C-type programmers, lack of communication skills.

    A humble VB programmer with a penchant for [the English] language.

    Don't forget that a lot of ppl that visit this site don't have English as their native language (i.e. me [:)])

    Anyway - nice piece of code... Guy really should learn:
    1) What a string is in C
    2) What "hex" is
    3) What "ascii" is
    4) What "binary" is
    5) What "strcat" & "strncat" do
    6) What pointers are

    I am really amazed that he already knows the "safe" strncpy function, although he still uses the normal strcpy sometimes... (and no - this is no defending of the code [:D])

    For the ppl that say the compiler can optimize this -ERR- wrong [:)] strcmp is a function from some library, the C compiler doesn't know what it does, so it can't optimize string comparisons - it's not a language construct as in many other languages.
    At least C-compilers are smart enough to generate code that stops evaluating all conditions when || (logical OR) is used and a "true" condition is met somewhere in it (VB doesn't do this, it always evaluates everything) - but still, every loop where a '%' is encountered will trigger a _LOT_ of strcmp's.

    This site should really organize a contest or smth - rewrite the pieces of code published here in the most optimal way in the same language... Would be fun too see the results... [:D]

    PS: The formatting in of these replies really sucks... Half-my-ass HTML and some wannabe BB-code? Wrote a nice parser for the latter - but well - it's C eeh... I mean PHP [:D] [;)]

  • (cs)
    <FONT color=#0000ff>strg[k] = ' ';
    strg[k+1] = 'C';
    strg[k+2] = 'o';
    strg[k+3] = 'p';
    strg[k+4] = 'y';
    strg[k+5] = 'r';
    strg[k+6] = 'i';
    strg[k+7] = 'g';
    strg[k+8] = 'h';
    strg[k+9] = 't';
    strg[k+10] = ' ';</FONT>

    This is just so delightful. strncpy(strg, " Copyright ", 11) would have been so much more concise, and much more readable.

    I'm not even going to comment on the rest. It's just too painfully obvious that this isn't a native C coder.

  • (cs) in reply to KoFFiE

    <FONT size=2>

    For the ppl that say the compiler can optimize this -ERR- wrong Smile strcmp is a function from some library, the C compiler doesn't know what it does, so it can't optimize string comparisons - it's not a language construct as in many other languages
    </FONT>

    Since it's part of the standard C library, the compiler is allowed to know what strcmp() does. And many compilers will actually replace the function call with the actual underlying code - for example, VC++ 6 will, if run with /Oi, inline strcmp().

    <FONT size=2>

    PS: The formatting in of these replies really sucks... Half-my-ass HTML and some wannabe BB-code? Wrote a nice parser for the latter - but well - it's C eeh... I mean PHP Big Smile </FONT>Wink

    Ah, yes. I actually fire up IE for this forum, since the handling under any normal browser is so bad. When place like LiveJournal can get formatting right, and actually understand threading, using phpBB is a bigger WTF than any code yet posted.

  • (cs) in reply to phx
    phx:

    Would the following C++ crap be considered a more concise and ugly method? I'm hoping it works, and assuming in, out are char* and len is the length of in. I'm even nice and allocating the output buffer :D

    [code language="c#"]
    

    #define hexdig(c) (c>9)?:('0'+c):(56+c)
    out=alloc(2*len);
    for(i=0;i<len;i++)
    {
     out[2*i]=hexdig(in[i]&15);
     out[2*i+1]=hexdig(in[i]&240);
    }

    [/code]

    Notes:
    ('A'+c-9) is too much code, so i use (56+c)

    This would not be correct... The ('A'+c-9) should at least be ('A' + c - 10), and the compiler optimizes this anyway - so no need to make your code less clear :) Also, you start with the wrong nibble, and you don't shift it with 4 bytes...

    Code below works (both way algorithms)

    [code language="c#"]

    #define HEX_CHAR(c) ( ((c <= '9') && (c >= '0')) ? (c - '0') : ( ((c >= 'A') && (c <= 'F')) ? (c - 'A' + 10) : -1) )
    #define HEX_DIGIT(c) ( ( (c) < 0x0A) ? ('0' + (c)) : ('A' + (c) - 10) )

    long hex2long(char *str) { long lng = 0; int tmp; while ( (*str) && ( (tmp = HEX_CHAR(*str)) >= 0) ) { lng = (lng << 4) | (tmp&0x0F); // shift 4 bit str++; } return lng; }

    char *dumpHex(char *in, int len) { char *tmp, *out; int i;

    if (len < 0) len = strlen(in);
    out = malloc( (2*len) + 1); // spacer for end '\0'
    tmp = out;
    
    for(i = 0; i < len; i++) {
        *tmp = HEX_DIGIT( (in[i] >> 4) & 0x0F ); tmp++;
        *tmp = HEX_DIGIT(in[i] & 0x0F); tmp++;
    }
    *tmp = '\0';
    
    //printf("HEX:\n%s", out);
    //free(out);
    return out; // free it yourself function -> dangerous!! :)
    

    }

    [/code]

  • (unregistered)

    I left the best out: After posting the code to Alex i found this code right after the hex to ascii conversion:

    <FONT face="Courier New">out = popen(SENDMAIL_PATH,"w");
    fprintf(out,"From: xxxx\n");
    fprintf(out,"To: xxxxx\n");
    fprintf(out,"Subject: Feedback-Error\n\n");
    fprintf(out,"Hexcode [%s] ist dem Programm nicht bekannt!\n\n",hexstr);
    pclose(out);
    kontaktformular = "nein";
    show_error("../../fehler/feedback/index.html","<br>Es ist ein Fehler aufgetreten und direkt an unsere Technik weitergeleitet worden!<br><br>Bitte versuchen Sie es später noch einmal.<br>");
    exit(1);</FONT>

  • (cs)
    KoFFiE:
    [image] Bellinghman wrote:

    <FONT size=2>

    </FONT>

    Since it's part of the standard C library, the compiler is allowed to know what strcmp() does. And many compilers will actually replace the function call with the actual underlying code - for example, VC++ 6 will, if run with /Oi, inline strcmp().

    Inlining is a rather simple method to optimize something. Optimizing by predicting what will happen is something completely different, and extremely complicated, certainly if you start doing this on strings...

    Indeed. However, trying to optimise round a function call is extremely difficult. If the code can pull the content of the function call inline, then it can (not necessarily will) be able to do a much better job. It need no longer push registers that the function never overwrote anyway. Register colouring can work. If it works really, [li] really hard, it might throw away stuff that turns out not to be required.

    (Though I think it's on a hiding to nothing with this code.)

    As far as the strings thing is concerned - well, all the compariosons are against short fixed strings. There is a chance that the intrinsic code could actually just do two byte comparisons rather than a loop.

    Still, the original code is a total mess. I'd recommend the author look at 'Unicode Demystified' . It contains code that addresses much more complex requirements, with much simpler code, and it might inspire him to write C, rather than VB using C syntax.

  • (cs)

    Just because some doofus managed to convert his VB script into compilable C, it doesn't make him a C programmer.  [:P]

    Dead giveaway: "j = j + 2;"?  wtf?  That's like wearing women's underwear.  [:$]

    Anyway, I think the maximum wtf density is attained in the 256-clause if statement that is a tautology.  [:@]

    I can't stand to read any more. [+o(]

  • (cs) in reply to KoFFiE

    KoFFiE:

    you don't shift it with 4 bytes...

    Probably the dumbest part...

    I can see myself debugging that for hours and not noticing too ;)

  • (cs) in reply to

    :

    Please now, let us not condem an entire programming language because some idiot doesn't know how to use it.

    Agreed ...

    :

    If you're really "hard core", you'll probably recognize the following code snippet as Duff's Device.

    Well, this all depends on how you define "hard core". I presume Duff's Device is one of those old C chestnuts that a certain breed of C programmer takes great pleasure in discussing? That must make me not "hard core". Oh well.

    :

    Otherwise, you'll probably not be able to figure out what it does, or how.

    Speak for yourself (whoever you are, "anonymous"). I agree that it looks confusing, but it doesn't take that long to work out. If this code compiles, then this must mean that the switch/case statements aren't treated by a C compiler as a construct in the same way the do ... while is. The do ... while compiles independently of the switch/case, and the switch/case is then interpreted as a jump into a particular position in that compiled code - this makes sense, given the default fall through behaviour of C. The switch statement is executed only once, meaning that the first run through the loop is only partial. Given that the address of the destination (to) isn't incremented in the way that the address of the source (from) is, it is probably some kind of serial device. So the net result is that count values are copied from memory starting at location from, to a serial device of some description. I presume they are copied in blocks of eight (first block may be less than eight to give exactly count values overall) for performance reasons?

  • (cs) in reply to Bellinghman
    Bellinghman:

    <FONT size=2>

    For the ppl that say the compiler can optimize this -ERR- wrong Smile strcmp is a function from some library, the C compiler doesn't know what it does, so it can't optimize string comparisons - it's not a language construct as in many other languages
    </FONT>

    Since it's part of the standard C library, the compiler is allowed to know what strcmp() does. And many compilers will actually replace the function call with the actual underlying code - for example, VC++ 6 will, if run with /Oi, inline strcmp().

    <FONT size=2>

    PS: The formatting in of these replies really sucks... Half-my-ass HTML and some wannabe BB-code? Wrote a nice parser for the latter - but well - it's C eeh... I mean PHP Big Smile </FONT>Wink

    Ah, yes. I actually fire up IE for this forum, since the handling under any normal browser is so bad. When place like LiveJournal can get formatting right, and actually understand threading, using phpBB is a bigger WTF than any code yet posted.

  • (cs) in reply to fluffy

    grr, fucking piece of shit forum software.

    This isn't phpBB.  phpBB doesn't suck NEARLY this bad.  Actually I rather like phpBB, after making a few simple modifications to it anyway.

    Anyway, this is written in ASP, not PHP.  That should be your first clue that this isn't phpBB.

  • (cs) in reply to fluffy

    Actually its in ASPX. The .Net variant of ASP. Which in itself means nothing as the editing control is javascript/vbscript I guess.

    Drak

  • (unregistered)

    Let me tell you, as a Visual Basic enthusiast it really pains me that so many of the posts here are in my programming language of choice.

    The reason most bad code is in Visual Basic, is because most of all code is in Visual Basic.

    "To the first approximation" all programmers are VB programmers.  All other languages are in the noise level (or as they put in on "The West Wing" last week "within the margin of error...of not having any support at all")

     

  • (cs) in reply to

    Booger...Anonymous again.   That last one was me.

    Isn't this site supposed to auto-logon?

  • (cs)

    Get the IEView extension for Firefox.  It will save you a ton of frustration with this forum.  You only need to use IE if you're quoting though. [:)]

  • (cs) in reply to fluffy

    fluffy:
    grr, fucking piece of shit forum software.

    This isn't phpBB.  phpBB doesn't suck NEARLY this bad.  Actually I rather like phpBB, after making a few simple modifications to it anyway.

    Anyway, this is written in ASP, not PHP.  That should be your first clue that this isn't phpBB.

    Ah, thanks for the clarification. I'd ended up trying to find out what the heck BB was (it's very briefly mentioned, with no explanation in the site FAQ) and arrived at the phpBB pages. I then made the assumption that this was using that.

    I have a friend who works for a certain very large company that happen to run, among other things, a certain two web applications.. One is driven by Python. The other by ASP. One is incredibly successful, and always responsive. The other is godawful slow. They're looking to rewrite the ASP one into Python.

    And yes, you probably use one of them frequently.

  • (unregistered)

    DavidM, you're on target about Duff's Device. Efficiency is why it does the "blocks of eight" thing with the mod-eight part done first: That way, it only has to check to see if it's done on every eighth byte.

    The best part about the strncpy() issue is that in the two places where he should be using strncpy() but isn't, he is using strncpy() for essentially the same thing, on the line immediately preceeding the horrible unrolled gunk.

    Between that, and "i = i + 2", and a few other items (not least the 256 case sensitive strcmp()s instead of two isxdigit()s), and strcmp()ing "25" etc. when he should've strtol()'d the string once and switched on the integer) -- this poor guy is not a C programmer.

  • (cs) in reply to

    That's a wild assumption. Could be that everything this person ever did was in C, and that all the training he/she ever got was from one of those "teach yourself C in an afternoon" books. This snippet could as easily be a small sampling of hundreds of megabytes of equally-bad source code.

    Not a competent C programmer? No. Not a good C programmer? Definitely not. Not a C programmer? Not enough data to draw a conclusion.

  • (cs) in reply to Stan Rogers

    You can add 'Not a competent programmer' to that list, and leave out the C. Anyone with some feeling and competence for/in programming could think of a better way to do this, regardless of the language they program in.

    Drak

  • (unregistered)
    Alex Papadimoulis:

    It's even worse than the Worst Possible VB Solution. And it's in C!



    The language has nothing to do with crappy code.
  • (cs) in reply to
    :
    You can't nest <font color="#0000ff"><font face="Courier New">case</font> <font color="#000000">statements in a <font color="#0000ff"><font face="Courier New">for</font> <font color="#000000">loop.</font></font></font></font>

    You can. You may not want to - but you can:

    
    void main()
    {
    	int i;
    	int count = 10;    /* or any arbitrary value */
    	switch(count)
    	{
    		case 0: for(i = 0; i< 20; i++){
    		case 1: count ++;
    		case 2: count++;
    		case 3: count++;
    		}
    	}
    }
    

    Compiles and works just fine. It's the same as Duff's device mentioned elsewhere just - probably - not as useful. Remember that a switch{ case: } construct is just a structured "goto" in C.

  • (cs)

    Duff's Device
    Basically, it's loop unrolling (as done by any optimising compiler), which means the repeat condition gets checked less often, and there are less code jumps for the same number of iterations. It only works if you know how many times it's looping beforehand, but it can make a big difference if the repeated operation is small and done a lot of times (as in here).

  • (unregistered) in reply to

    :
    <FONT color=#000099>for</FONT> (i=0,j=0;i<STRLEN color="#000099" (buff);i++,j=""><FONT {="">switch</FONT>(buff[i]) {
    case <FONT color=#990000>'+'</FONT>:
    value[j] = <FONT color=#990000>' '</FONT>;
    buff[i] = <FONT color=#990000>' '</FONT>;
    break;
    case <FONT color=#990000>'%'</FONT>:

    You can't nest <FONT color=#0000ff><FONT face="Courier New">case</FONT> <FONT color=#000000>statements in a <FONT color=#0000ff><FONT face="Courier New">for</FONT> <FONT color=#000000>loop.</FONT></FONT></FONT></FONT>
    </STRLEN>

     

    It's just badly posted, look at the C source at the end of the post to see the correct version.

    It seems this code attempts to change some string into a verbal form of some sort

  • (unregistered)

    ArmchairDissident, I think that's not what he meant. There was a left

    angle bracket in the for loop header, which resulted in the following:

    for (i=0,j=0;iswitch(buff[i]) {
        //...the horror, the horror...
    }

    Notice

    where it says "iswitch" -- that "i" should be followed by a less-than,

    and then the rest of the line. The HTML formatting script screwed up. Among

    other things, the for's close-paren and opening curly brace are

    missing, as well as a newline. In the original code the switch was not inside the for-loop header. But that reader

    mistakenly thought otherwie.s

    You are, of course, correct about arbitrary case placement and Duff's Device.



    Allow me to say, by the way, that this commenting software is

    staggeringly, mind-numbingly bad. It's ass to use, it enforces bizarre,

    difficult-to-read font choices, and it fills the comments page with

    meaningless and distracting visual noise. The person or persons who

    designed it were criminally incompetent. No slightest thought was given

    to usability or utility. There is more to good interface design than

    spraying brightly-colored crap around the screen.

Leave a comment on “Who's Hard-Core Now?”

Log In or post as a guest

Replying to comment #:

« Return to Article