Ugly Code!

My first job out of Uni was as a programmer for a small company which manufactured network switches. One of the other programmers there challenged me to write a program  which output itself without referring to the file containing the source code. The following piece of ugly C code is what I came up with.

Is an explanation necessary… I guess so. All variable name are kept as short as possible and formatting for to make the code readable is all but eliminated.

After the necessary includes, I define useful character codes. Carriage return, quote, percent.

Then I craft a format string that will allow the header part of the source code to be output.

Then another format string which will be used to output the body of the source code.

Now define a couple of working strings.

And finally the main function which builds the output string.

Build the header string which will result in a copy of itself.

Build the body string with %s place holders for head string and the body format string.

Output the finished string for the code.

I don’t understand how it works either!

This entry was posted in Projects and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.