Celebrating, Not Cursing, Convoluted C Code

Nearly every engineer has suffered through the misery of fixing someone else's poorly written code. No experience could be worse, except maybe sticking pins in your eyes.

A winning entry in the Intl. Obfuscated C Code Challenge. The source code is in the shape of pi and is full of pi-ity? The program prints the value of e to a number of decimal places.
Source: ioccc.org

When you think about it, anyone can create a useless scribble of code. But creating a working program that actually masks what it does and how it does it with instructions that are intentionally misleading is a pretty amazing feat.

Entries must be a complete valid C program that works and is documented, but is not maintainable. So challenging is the competition that the people who win aren't hacks, but true craftsmen/women of the C language.

Here is an example of one of the past winners, which judges said was "The best one-line entry ever submitted."

Go ahead, test your own code-cracking skills! (Note: Judges suggest that you may want to compile the program on a UN*X system, or at least use a C implementation that fakes it, as very few people are able to determine what this program does by visual inspection.)

The IOCCC, which is the oldest-running contest on the Internet, was created by Larry Bassel and Landon Curt Noil in 1984. Noil, who is now a resident astronomer at Cisco, says that at that time he had been coding in C for a decade or so.

"Larry and I were working at National Semiconductor and the two of us were having to debug two pieces of code that were not only really bad, but it was obvious that someone had actually put a lot of effort into producing garbage," says Noil.

After a heroic effort, Noil posted a flame online ranting about how the pair was trying to fix the code and wondering if anyone could possibly code anything worse. “We kind of put this thing out there tongue-and-cheek and to our surprise a number of people actually submitted code that made us laugh. So we decided to acknowledge the winners and post their entries online and that was the start of the IOCCC.”

The IOCCC used to be run annually, but that frequency became too difficult for the judges' schedules, so they now run it "whenever someone bugs us to do one." Winners of the 22nd annual competition were announced on April 1, 2014, and at this time the date for the next one is not yet confirmed.

Deciphering poorly written code is a great way to gain a deeper understanding of the subtleties of the C language. To that end, we're pleased to announce that the judging team of the IOCCC will be speaking at the 2015 Embedded Systems Conference in Silicon Valley where they will discuss some of the best and worst C programs.

But that's not all: Attendees will be encouraged to participate in a rip-roaring-fun, no-holds-barred live competition involving both beer and bacon: Teams will be given a C program to examine (including compiling it to look at any warning messages and the like) and then explain to the judges how it works. Extra credit will be given for teams that are able to convert the obfuscated code into a well-documented program that is readable and explainable.

Want to join in the merry-making and meet other awesome C programmers? Sign up for our EE Times Life weekly newsletter (must be registered) for updates on ESC 2015. And drop me a line if you'd like a personal update from me on ESC 2015 at karen.field@ubm.com.

@kfield: If you teach people how to write obfuscated code just for fun, chances they will use it one day - maybe they are being taken off a project, maybe they are getting fired, who knows?

Now if you teach them how NOT to do things, but how to do them better and more cleanly, then I am 100% in agreement.

I have seen some code where a subsequent maintainer added the comment 'the f*****r who wrote this should be shot', and indeed it was truly unreadable. And also the opposite, where the code was elegantly written and comments just enough to it was simple to understand, and also admire. I know which coder I would hire.

@yog-sohoth: I don't view it as a waste of time, as the article points out, the winners are incredibly talented programmers and if this content is used as a teaching tool (which we plan do to at our event next year) it's an extremely powerful way to learn: By seeing how NOT to do things and why. Plus, we'll teach programmers techniques for fixing poorly written code, which I'm sure we've all run into at some point in our careers!!!