IOCCC Problems - Anonymous.c (1984)

I try to make sense out of a weirdly written “hello world”

Introduction

IOCCC is a contest where you get to see exactly the opposite of what you’re expected to do in your day to day life: write clear code. The code submitted here is sometimes overwhelming, and I’ll try to make sense of it.

The Original Code

int i;
main()
{
for(;i["]<i;++i)
{--i;}"];read('-'-'-',i+++"hell\
o, world!\n",'/'/'/'));}read(j,i,p){write(j/p+p,i---j,i/i);}

A couple of things here:

  • using just main() was a valid thing until standardization came along. You can still use it: it is automatically assumed to be int.
  • int i is globally declared, and henceforth is equal to zero.

Also, that read() function which has three parameters looks like it could be easily simplified: '-' - '-' is equal to zero, since we’re actually doing a subtraction of two chars, and it is implicitly assigned to int. Similarly '/'/'/' is 1.

So, if we rewrite it and introduce proper formatting, we have:

int i;
int main()
{
for(;i["]<i;++i){--i;}"];
read(0,i+++"hell\o, world!\n",1));
} 
read(j,i,p)
{
write(j/p+p,i---j,i/i);
}

Also note that i["]<i;++i){--i;}"] is actually a array of characters initialized as a string.

Sometimes, it’s good to check what’s happening under the hood to figure out the code. I’m going to do just that. Running ltrace tells you what library calls were being used. Running ltrace on anonymous.c reveals this:

write(0, "h", 1h)                                 = 1
write(0, "e", 1e)                                 = 1
write(0, "l", 1l)                                 = 1
write(0, "l", 1l)                                 = 1
write(0, "o", 1o)                                 = 1
write(0, ",", 1,)                                 = 1
write(0, "w", 1w)                                 = 1
write(0, "o", 1o)                                 = 1
write(0, "r", 1r)                                 = 1
write(0, "l", 1l)                                 = 1
write(0, "d", 1d)                                 = 1
write(0, "!", 1!)                                 = 1
write(0, "\n", 1
)                                = 1
write(0, "", 1)                                  = 1
write(0, "1", 11)                                 = 1
+++ exited (status 0) +++

write() is a Unix system call (you can find more about it by typing man 2 write on any linux system, and what it does is similar to a printf statement, i.e., write(0,”h”,1) is the same as typing printf(“h”), since 0 means stdout and 1 is just the size of the data.

A person on discord helped a lot to understand this code. I thank him here..

Finished Code

#include <sys/unistd.h>
int i;
int main()
{
	for(;i["1234567891011"];)
	{
		write(0,i+++"hell\o, world!\n",1);
	}
}

Copyright (c) 1984, Landon Curt Noll. All Rights Reserved. Permission for personal, educational or non-profit use is granted provided this this copyright and notice are included in its entirety and remains unaltered. All other uses must receive prior permission in writing from both Landon Curt Noll and Larry Bassel.