Yet Another Attempt at Obfuscated C

I had discovered the International Obfuscated C Coding Contest (IOCCC) some time in the late 1980’s in the pages of a printed technical magazine. At the time, my C skills were still in their larval stage, but I was very intrigued by the handful of entries that had been presented.

In later years, I tried my hand at other obfuscated coding contests, each time usually on a whim. I managed to irritate a few people with an entry in an obfuscated Ruby coding contest and I managed to see print in The Perl Journal for one of that publication’s obfuscated Perl coding contests. It saw print again in the book Games, Diversions, and Perl Culture which collected many articles from TPJ.

I have not been nearly as successful when trying to attain fame and fortune in the IOCCC, though. I had tried years ago with an entry that was comprised of preprocessor abuse and a tiny VM that implemented a ROT13 encoder/decoder. The main() function of that program was:

I don’t think anyone found it either particularly fitting nor even mildly humorous. I believe that both preprocessor abuse and tiny VM’s are now frowned upon in more recent incarnations of the contest.

When I saw the announcement for the 2019 IOCCC, I thought about taking a swing at the contest again. This time, as I tinkered with a few bits of code, I thought I’d create some code that would “draw” something on terminal screens with fixed-size fonts.

I experimented a bit with some code I’ve used since the 8-bit BASIC era to draw circles and ovals. I accidentally noticed that an oval that I had “drawn” in a text-buffer looked a lot like the letter C. That’s when the idea for my entry solidified. My entry would simple display the letter C on a blank terminal screen drawn in multiple occurrences of the letter C.

Here’s the code:

Here’s the output:

I had included this text with the entry:

Why I Think This Program is Obfuscated:

I have used two techniques for obfuscation. The first is that I have normalized all constants and constant-expressions used in the draft version of the source to hard constants that shouldn’t make a lot of sense.

The second technique that I used was to leverage the sin() and cos() functions to develop the output. I don’t see these a lot in code that I run across, but I honestly need to do more research to see how frequently they’ve been used in past IOCCC entries.

The program develops a text buffer and displays it with a single output line.

What This Program Does:

The program plots points on a very small 2-dimensional Cartesian plane using a single character as the equivalent of a pixel. The formula used in the second for-loop was one that I had used in the 1980’s to draw ellipses and circles on the screens of various 8-bit computers.

The formula generally went like this:

  Loop variable i from 0 to 359
      Let x = cos(i * pi / 180.0 ) * radius + x_offset_center
      Let y = sin(i * pi / 180.0 ) * radius + y_offset_center
      Plot x, 
  End Loop

The variable i represents 0 to 359 degrees. The multiplication by pi / 180.0 is to convert the resulting value from radians into degrees so that each iteration of the loop draws the next contiguous portion of the ellipse. The x and y axes on most computers are of different lengths. The values used for the radius may be scaled a bit differently.

In the submitted C program, I chose to draw a partial ellipse, omitting the section of the ellipse that makes the ellipse look like my favorite letter of the alphabet. The second for loop does not extend to the entire 0 to 359 degree range that a full ellipse would. I have also normalized the pi/180.0 constant to the float value 0.017453.

The other hard-coded values in the code were based on some sizing preferences that I normally would have set as #define constants, but their purposes are less obvious when normalized to their numeric constant values already in the code.

Alas, this entry also did not place among the myriad of really cool entries. So went my second attempt at the contest. I’ll really have to step up my game before I feel up to entering a future IOCCC.