Devious
That "salmon" one hurt my brain.
The IOCCC, as it's familiarly known, is back after a four-year gap, giving the entrants more time to come up with some remarkably devious code. This week, the results for the IOCCC 2024 were announced, with a record 23 winners. It's the first IOCCC in four years, but you shouldn't take that to imply it's as regular as the …
What you have is an array of int containing the values of Unicode characters with values like U+0E0053 (first one), putchar() puts a byte eg 0x53 which is 'S' - the start of 'Season'. The top bits (0x0E00) are ignored as being more than can be stored in a byte.
It increments in the int array until it outputs U+0E002E (0x2E is '.') followed by U+200A (0x0A is newline) and then NUL which putchar() returns thus stopping the loop.
This is all output by after 'main {' having U+0E0041 which is the 'grill' macro (somewhat deviously). This is not visible in most text editors as it has a character value U+0E0041.
Why the squares from 1 .. 10 is not output is because after the U+0E0041 (after 'main {') is another U+0E0041 which expands to ';while(putchar(*salmon++))'. This outputs another NUL byte, which putchar() returns as condition to while(); this being false the body of the while loop never happens.
I suspect that this will not work on a big endian machine (Intel/AMD are little endian).
It might need a tweak if sizeof(int) is not 4.
Phew!
It will work on a big endian machine. By the time that the character is sent to putchar, it's already been converted to an integer in the host's preferred byte order by the compiler. The putchar function will cast that to unsigned char, which will take the least significant 8 bits no matter where the CPU chooses to store them. Programmers sometimes learn that to their detriment when trying to use non-Unicode-aware functions like putchar with Unicode strings.
About four years ago, I was reading some PDP11 machine code for the first time since I wrote it 45ish years ago, and scratching my head. I couldn't figure out WTF I did what I did ... clearly it worked, but it was also clearly wrong. I'd been going over the (seemingly) simple code for eight or ten hours and was getting increasingly frustrated, which is rare for me. I wrote the shit, why couldn't I figure it out? My wife (ever the wise one) suggested I sleep on it. So I took a nap.
I woke an hour or so later with two words on my mind: DEC Tape.
Now, I seriously doubt that even that pile of steam-powered, smoke-belching, carved from solid rock back in the age of Wizards and Grues will ever need anything as weird of DEC Tape again ... So I should excise that block of code (and the others like it, throughout). I should streamline things, make it more efficient, right?
Should I fuck. It fucking works. Is has worked for over four decades. I ain't touching it. I have been called in far too many times to "fix" projects that started like that, "just one, small tweak", to fall for that one ... I HAVE, however, added comments ("notes to my future self") that I should have added in the first place but didn't, because "it's obvious, innit?".
> I could not get that to compile unless I put "int" on the line above "a,b=44,x,".
Check you compiler (gcc?) flags, mine has "--disable-werror" set (on openSUSE 15.6), so I don't have problem with the default int type:
$ gcc -v
Compile your program with:
$ gcc -Wno-error moon.c
> Compile your program with:
> $ gcc -Wno-error moon.c
Maybe this ^^ doesn't solve your problem.
(My) openSUSE 15.6 uses an old version of gcc (7.5, defaulted to c11) which, I don't why, it doesn't consider an error the lack of a variable type at the point of declaration (since c99 this must an error). The solution is to change the standard (tested with gcc 14):
$ gcc -std=c90 moon.c
If you want to learn C, try doing end-user support for a C compiler publisher. I did that in the late 80s for a company that wrote C compilers for a bunch of architectures while I went to school. There's no better teacher than looking at someone else's busted(?) code, divining what they were trying to do, whipping up a corrected version and shipping it back to them on the floppy they mailed to us. Yes, support by mail correspondence. At least until I convinced company to allow me to set up a BBS for support.
I still like looking and these concoctions of unhinged art and teasing out their secrets.
Considering that previously it was required that submissions were under a proprietary license.
But of course it's worse now - it seems the submissions now have no license and the primary files are hosted on github.
Some of them seem trivial enough that it doesn't really matter, but many certainly aren't trivial.