Scary Code

If you code a lot in a week, you’re bound to make some (possibly) amusing typos. Almost every time, the typo is detected by the compiler and an error is issued, but sometimes you manage to (mis)type valid code! And I recently make one of those typo and I started wondering how far we can push this idea in writing really, really, really, really ugly code.

So I typed t-=-1 instead of… I’m not sure what anyway. This is ugly, and results in nothing more than t++ (since --1=1). This mean you can use:

bizarre construct Instead of…
t-=+1 t--
t+=-1 t--
t-=-1 t++
t+=+1 t++

And you’ll end-up with stuff like:

for (int i=0,j=10;
     i+=+1, j-=-1)

Since --1=1, you can write - - - -3 instead of just 3. You have to use spaces because you can’t write --3 as ambiguous with operator--. To maximize confusion, make sure to mix equivalent variants (t+=- - - -1) as much as possible.

Another endless source of amusement is the [ ] operator on arrays. The [ ] that creates the illusion of arrays from pointers (because in C and C++ arrays aren’t much more than just that) is syntactic sugar for dereferencing pointers. Instead of writing *(p+3) you write p[3]. But the logic behind the sugar is that since *(p+3) is p[3], and that *(3+p) is the same as *(p+3), you can write 3[p]!

But complete abomination comes with the sequencing operator comma. In ISO/IEC 9899:1999, § ¶ 2, the C99 standard states that items separated by commas are evaluated from left to right and the entire expression has the value of the last item evaluated. So you can write:

  int j=10;
  while (
         printf("i=%d j=%d\n",i,j),

You can push the logic quite a bit more:

  int i=0;
  while ( (3,5,6,toaster)[i] ) 
    printf(" t=%d",i[toaster]);

Here toaster is a null-terminated array of some sort. And, why not push it even further:

  while ( (3,5,6,blah)[i](j) ) 
    printf(" t=%d",k[toaster]);

Here blah a function pointer array, with functions taking a (presumably) int argument. You could also remove the { } and chain the while-block with commas alone:

  while ( (3,5,6,blah)[i](j) ) 
    printf(" t=%d",k[toaster]),


* *

Of course, you understood that I do not advocate the use of such constructs in (normal) code—quite the opposite! In fact, I make an effort to use the clearest constructs whenever possible. For example, I (almost) never use *ptr++ and favor ptr[offset]. This has the advantages of making clear where you are relative to the base of the array and not destroying the value of ptr. Then again, it always depend on what you’re doing, maybe *ptr++ is quite acceptable. But while ((3,5,6,blah)[i](j)) is pretty much the programming equivalent of seating drunk and naked in the punch bowl at a Christmas party: something you may want to avoid. If not for yourself, for others.

3 Responses to Scary Code

  1. pdq says:

    You interchanged the t variable with the i variable in the first table.

  2. Carl Tessier says:

    There even is an “International Obfuscated C Code Contest” in which these obscure constructs are very common.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: