OddThinking

A blog for odd things and odd thoughts.

magicTrick.c

For the C programmers, here’s a little puzzle that I first produced while banging my head against the C syntax at uni. Actually its more like a magic trick.

Start by having a look at the following “code”. It starts out well – and it finishes well. But it gets a bit distracted in the middle.

#include <stdio.h>

int main()
{
    float eight = 8.0;
    float *pEight = &eight;
    float oneEighth = 1.0/*pEight;

    oneEighth += oneEighth;
    // If you are using a C++ compiler, this line will be ignored as a comment.
    // That's a shame because it is the previous line which is buggy!

    #if(this) was Python, this buggy line would be ignored: oneEighth += oneEighth;

    <!-- If this was HTML, however, it would be this buggy line that was ignored:
    oneEighth += oneEighth; -->

    // As you can see, C has a remarkably complicated syntax for a relatively small language.
    // It is too hard to predict, and is full of nasty tricks. I don't like its syntax much. 
    // I wish other languages didn't copy it.
    // In fact, frankly, I think its syntax is a load of #%$@*/;

    printf("The final value of oneEighth = %f\n", oneEighth);
    return 0;
}

So, what will happen when you compile this code? Answer below, so have a stab before reading on.

The answer is surprising – here’s bit where people get dazzled by the magic when I demonstrate it in front of them.

You don’t get any error message at all! This is valid C code! It compiles and it runs. No honestly. I even checked that WordPress didn’t chew it up. You can try it for yourself, later.

But that’s not the weird part! The weird part is what happens when you run it! This is what it prints to stdout:

The final value of oneEighth = 1.000000

Weird! So how could it have successfully doubled the value of oneEighth three times, despite the various commenting standards?

Have you figured it out yet?

I stumbled over this trick before the syntax-colouring was popular; I had a genuine bug in my code caused by the same trick. However, these days, pasting this text into a syntax-colouring editor makes the trick so obvious, I won’t even bother to reveal the solution here.


Comments

  1. Ah, misdirection…

  2. IOCCC. Need I say more?

  3. I think I’ve figured it out. The magic starts here:

    [Editor notes: Here followed a perfectly accurate solution to the puzzle which I have elided (with Alastair’s permission)]

    Which also explains why the output is 1.0.

    Do I win a prize?

  4. In my stylesheet I have

    .spoiler {   
        color: #fff;   
        background-color: #fff;   
    }
    

    Comes in handy in situations like this.

  5. Alastair: some people force fonts and colors to their preferences or surf with Firefox’s View → Page Style → No Style. There is no universal scripting-free client-side solution that I’m aware of, either.

  6. Perhaps I should install this spoiler plugin?

  7. You might be interested in “Hello Polyglots”, a computer program in eight languages – http://ideology.com.au/polyglot/

    Since I last looked, Peter has added colour syntax highlighting. Here it is for ‘C’ – http://ideology.com.au/polyglot/c

  8. Thanks Alan. Polyglots is an classic that I have loved for years – I came across it recently while cleaning up some old files – I had carefully stashed a copy in a subdirectory I haven’t opened since 1995!

    The syntax highlighting is new to me though. It does help solve some mysteries!

    Aristotle also briefly mentioned the IOCCC (International Obfuscated C Code Contest). This has always been good for a bewildering array of innovative ways of confusing the reader. I haven’t checked it recently, but I still nostalge about some of the classic early entries.

  9. Polyglots is freakin’ amazing! Can’t believe I had never seen it before. And the syntax colouring definitely helps.

  10. Julian: Perl is even more fun. Purely syntactic obfus are rare, although the possibilities are endless. The good ones abuse side- or arcane effects instead. Personally I like the ones best where you can guess beforehand what is going to happen, but it’s completely baffling how it works.

    Thinking like a Perl programmer is why I needed a hint from someone on IRC before I caught how yours works, even though my spider sense tingled at the right spot when I first read it. I just shrugged and went to look for the kind of abuse that would be more typical in Perl.

  11. I note that IE6 sometimes causes large paragraph breaks in the code if the window isn’t wide enough. FireFox and Opera don’t.

    This is irrelevant to the magic trick.

  12. this was simple……. [Perfectly accurate solution ellided to preserve the mystery – Julian] thats it! QED.

Leave a comment

You must be logged in to post a comment.