• (cs)

    Ahh you youngsters probably don't remember the for-while loop. The good olde days.

  • José Rodrigues (unregistered)

    This code ain't for no boundary overrun prevention, nossir! It's actually a very clever, ingenious, way to add a NOP on the program, so that the snipped become 32bit-aligned!

  • FromCanada (unregistered)

    I always use these when I only want my code to run for a while.

  • (cs)

    I see the article contains 138 characters in total. Neat. This comment has even fewer.

  • (cs)

    It took me a few moments to understand the "Gordon's c/w came up w/ this".

  • Kardec (unregistered)

    It's too long this way! Just take away some vowels, nobody will notice. :-)

  • (cs)

    I once wrote this for loop:

    for (byte b = 0; b <= 255; b++) {
    // code
    }
    
  • SomeC++Dude (unregistered) in reply to José Rodrigues

    Re:José Rodrigues

    a good compiler would optimize that instruction out

  • Zapp Brannigan (unregistered) in reply to SlyEcho
    SlyEcho:
    I once wrote this for loop:
    for (byte b = 0; b <= 255; b++) {
    // code
    }
    
    I haven't used C for a while so I had to think for a second. That's nice.
  • (cs) in reply to SomeC++Dude

    "a good compiler would optimize that instruction out"

    A bad programmer would forget to use the good compiler's optimization flags.

  • Patriot (unregistered)

    Good thing you abbreviated co-worker and with, the story was so long it started to get boring.

  • Flanged Magnet (unregistered) in reply to Zapp Brannigan

    You remembered that only fools assume bytes are 8 bits?

  • fist-poster (unregistered)

    while (1); or any other number probably didn't work, but 0 appeared to correct the problem.

  • ledhund (unregistered) in reply to Zapp Brannigan

    noob question: the byte is signed, right?

  • fanguad (unregistered)

    tldr

  • ross (unregistered) in reply to ledhund
    ledhund:
    noob question: the byte is signed, right?
    It's probably not signed. But it is always going to be less than or equal to 255. Since the range of values is 0~255
  • (cs)

    Heh, I had a boundary overflow problem once or twice with Delphi code.

    I wrote this apparently simple snippet of code:

    for i:=start to end do // code

    which however appeared to freeze everything, as if start > end when the loop is started it will apparently run forever...or for very long, depending on what i is (an Integer however will take pretty long...). Why? Because Delphi implies only a precise equality as the loop ending condition. No "greater or equal" here. The solution was to either replace the for loop with a "while (condition) do" construct, or explicitly checking for the start>end condition before the if.

  • (cs)

    What are you doing? Rdng TDWTF n lol @ code

  • (cs) in reply to ledhund
    ledhund:
    noob question: the byte is signed, right?
    Thinking here. I never did do C, but I did teach C++ once on TV.

    The byte is NOT signed, but it will overflow (the b++ overflows the byte), so it should go to 0. Infinite loop.

    Nice. Jim.

  • ledhund (unregistered) in reply to ross
    jimlangrunner:
    ledhund:
    noob question: the byte is signed, right?
    Thinking here. I never did do C, but I did teach C++ once on TV.

    The byte is NOT signed, but it will overflow (the b++ overflows the byte), so it should go to 0. Infinite loop.

    Nice. Jim.

    doh! I didn't read the = so imagined the overflow already at 127++

  • Zapp Brannigan (unregistered) in reply to Flanged Magnet
    Flanged Magnet:
    You remembered that only fools assume bytes are 8 bits?
    Also old farts like me. But then it's not an exclusive or.
  • blank (unregistered)

    tl;dr

  • (cs) in reply to Kiss me I'm Polish
    Kiss me I'm Polish:
    It took me a few moments to understand the "Gordon's c/w came up w/ this".
    I'm scared that I read and understood it without even realizing they were abbreviated. o_o
  • Ryan (unregistered) in reply to SlyEcho

    I did that once. Took me far too long to figure out why it wouldn't end.

  • lolcoder (unregistered) in reply to lolwtf
    lolwtf:
    Kiss me I'm Polish:
    It took me a few moments to understand the "Gordon's c/w came up w/ this".
    I'm scared that I read and understood it without even realizing they were abbreviated. o_o

    Reminds me of this fortune: [Temperature 13 C][16:28:13 01-April-09][/home/adam] [adam@owl]$ fortune -m "f u cn" %% (fortunes) f u cn rd ths, u cn gt a gd jb n cmptr prgrmmng.

  • (cs) in reply to Flanged Magnet
    Flanged Magnet:
    You remembered that only fools assume bytes are 8 bits?

    Don't be silly. Eight bits is a dollar.

  • (cs) in reply to SlyEcho
    SlyEcho:
    I once wrote this for loop:
    for (byte b = 0; b <= 255; b++) {
    // code
    }
    

    I once saw this while loop:

    int n = 0;
    while(n >= 0) n++;
    

    G++ with -O3 turned it into an infinite loop. It's not even a bug.

  • (cs) in reply to SlyEcho

    My favourite is always this one - I keep finding that my finigers typo it!

    for(int i=0; i<end; i++); 
    {
        // code
    }</pre>
    
  • (cs)

    At first I thought I was looking at a for...while loop. But it turns out the ; substitutes for the body of the while loop, so it's exactly the same as:

    for (int i=0;i<max; i++) {
       ...
    } 
    
    while(0) {}</pre>
    

    Plus this way it works in Java as well as C.

  • Ardax (unregistered) in reply to C4I_Officer

    Um, no. While the terminating condition of a for loop is strict equality in Delphi, the runtime also checks that Start <= End. If Start > End, then it will skip the loop altogether. Also, End is only evaluated once for for loops in Delphi (e.g. If End is really a function call, the function is only called once at the first check.). For a while loop, the condition is evaluated for every iteration.

    Ergo, checking for Start > End before entering a for loop doesn't do anything unless End is a function call with a side effect, and while loops have subtly different semantics that can make a difference.

  • WillowAnne (unregistered) in reply to amischiefr

    the e in olde: extra character!

  • (cs)

    I call Shenanigans. The

    while(0)
    was very obviously and poorly Photoshopped in.

  • (cs)

    It's too long to re-tweet.

  • (cs) in reply to Ardax
    Um, no. While the terminating condition of a for loop is strict equality in Delphi, the runtime also checks that Start <= End. If Start > End, then it will skip the loop altogether. Also, End is only evaluated once for for loops in Delphi (e.g. If End is really a function call, the function is only called once at the first check.). For a while loop, the condition is evaluated for every iteration.

    Delphi 7, d'oh. And it just froze when ran: debugging the loop revealed that the index variable was merrily stepping its way up to MAXINT and then from -MAXINT...go figure.

    Start and End were variables in my case, and I used the default project options. It sure made me go "WTF" when I realized it, and chose the easiest workaround, since I really needed the index variable inside the loop, and felt that increasing them myself just made the whole code look uglier.

  • a guy (unregistered) in reply to Pim

    Everybody's routing through Twitter now.

  • CoyneT (unregistered)

    One of the nastiest practical jokes I ever read about was how to totally confuse C programmers. To do this, you bury this DEFINE in some ob

  • CoyneT (unregistered)

    scure header file:

    #DEFINE do if

  • (cs)

    I once did this...

        #define EverAndEver ;;

    Then you can do...

        for (EverAndEver) {  
            }

    A coworker saw it and added the comment:

        for (EverAndEver) {  // Hallelujah, Hallelujah!
            }
  • Jim (unregistered)

    Actually, if you count all new lines its 142 characters, and if this was encoded in windows format, its more like 149 characters. There's the real WTF.

  • Random guy (unregistered) in reply to Evo
    Evo:
    I once saw this while loop:
    int n = 0;
    while(n >= 0) n++;
    

    G++ with -O3 turned it into an infinite loop. It's not even a bug.

    Hey, it works just as coded. :-)

  • (cs) in reply to Random guy
    Random guy:
    Evo:
    I once saw this while loop:
    int n = 0;
    while(n >= 0) n++;
    

    G++ with -O3 turned it into an infinite loop. It's not even a bug.

    Hey, it works just as coded. :-)
    Only on slow machines.

  • SCJP (unregistered) in reply to MrEricSir
    MrEricSir:
    while(0) {}

    Plus this way it works in Java as well as C.

    No. In Java it doesn't. Boolean values in Java are really true or false, not 0 or !0.

  • (cs)

    If you put a newline before the while(0); it will be immediately obvious what this code does. But when you first look at it, it does make you go "huh".

    It is a fair criticism of C/C++ that non-standard indentation can mislead you as to what the code is doing. This code is a good example of that.

  • José (unregistered)

    Ouch... yet another python fanatic

  • Cbuttius (unregistered)

    This was also posted on April 1st but there is no such thing as a for...while construct and they are two separate loops in the code.

  • pbi (unregistered)

    This is C++ mate :). Strictly, you can only declare variables in the beginning of a code block / function and not in the head of a for loop (for (int i = 0 ...). Thats wtf :P

  • pbi (unregistered) in reply to pbi
    pbi:
    This is C++ mate :). Strictly, you can only declare variables in the beginning of a code block / function and not in the head of a for loop (for (int i = 0 ...). Thats wtf :P

    EDIT: ..Strictly speaking, you can only declare variables ....

  • Da' Man (unregistered)
    ugg boots:
    ...
    And your Captcha was..?
  • AB (unregistered)

    I'm sure the performance boost was amazing too

  • wbkang (unregistered) in reply to pbi

    C99 allows this.

Leave a comment on “A Common C Dilemma”

Log In or post as a guest

Replying to comment #:

« Return to Article