{"id":97,"date":"2013-06-18T09:04:42","date_gmt":"2013-06-17T23:04:42","guid":{"rendered":"http:\/\/www.eyemonline.com\/?p=97"},"modified":"2013-06-30T19:22:29","modified_gmt":"2013-06-30T09:22:29","slug":"ugly-code","status":"publish","type":"post","link":"http:\/\/www.eyemonline.com\/?p=97","title":{"rendered":"Ugly Code!"},"content":{"rendered":"<p>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 \u00a0which output itself without referring to the file containing the source code. The following piece of ugly C code is what I came up with.<\/p>\n<p><!--more--><\/p>\n<pre class=\"lang:default decode:true\">#include &lt;stdio.h&gt;\r\nint c=10;int q=34;int p=37;\r\nchar h[]=\"#include &lt;stdio.h&gt;%cint c=10;int q=34;int p=37;%cchar h[]=%c%cs%c;\";\r\nchar b[]=\"%s%cchar b[]=%c%cs%c;%cchar s[1024];char o[1024];%cint main(){sprintf(s,h,c,c,q,p,q);sprintf(o,b,s,c,q,p,q,c,c,c);printf(o,h,b);}%c\";\r\nchar s[1024];char o[1024];\r\nint main(){sprintf(s,h,c,c,q,p,q);sprintf(o,b,s,c,q,p,q,c,c,c);printf(o,h,b);}<\/pre>\n<p>Is an explanation necessary&#8230; I guess so. All variable name are kept as short as possible and formatting for to make the code readable is all but eliminated.<\/p>\n<p>After the necessary includes, I define useful character codes. Carriage return, quote, percent.<\/p>\n<pre>int c=10;int q=34;int p=37;<\/pre>\n<p>Then I craft a format string that will allow the header part of the source code to be output.<\/p>\n<pre>char h[]=\"#include %cint c=10;int q=34;int p=37;%cchar h[]=%c%cs%c;\";<\/pre>\n<p>Then another format string which will be used to output the body of the source code.<\/p>\n<pre class=\"lang:default decode:true\">char b[]=\"%s%cchar b[]=%c%cs%c;%cchar s[1024];char o[1024];%cint main(){sprintf(s,h,c,c,q,p,q);sprintf(o,b,s,c,q,p,q,c,c,c);printf(o,h,b);}%c\";<\/pre>\n<p>Now define a couple of working strings.<\/p>\n<pre class=\"lang:default decode:true\">char s[1024];char o[1024];<\/pre>\n<p>And finally the main function which builds the output string.<\/p>\n<pre class=\"lang:default decode:true\">int main(){sprintf(s,h,c,c,q,p,q);sprintf(o,b,s,c,q,p,q,c,c,c);printf(o,h,b);}<\/pre>\n<p>Build the header string which will result in a copy of itself.<\/p>\n<pre class=\"lang:default decode:true\">sprintf(s,h,c,c,q,p,q);<\/pre>\n<p>Build the body string with %s place holders for head string and the body format string.<\/p>\n<pre class=\"lang:default decode:true\">sprintf(o,b,s,c,q,p,q,c,c,c);<\/pre>\n<p>Output the finished string for the code.<\/p>\n<pre class=\"lang:default decode:true\">printf(o,h,b);<\/pre>\n<p>I don&#8217;t understand how it works either!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 \u00a0which output itself without referring to the file containing the &hellip; <a href=\"http:\/\/www.eyemonline.com\/?p=97\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[19],"tags":[17],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3FgN7-1z","_links":{"self":[{"href":"http:\/\/www.eyemonline.com\/index.php?rest_route=\/wp\/v2\/posts\/97"}],"collection":[{"href":"http:\/\/www.eyemonline.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.eyemonline.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.eyemonline.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.eyemonline.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=97"}],"version-history":[{"count":19,"href":"http:\/\/www.eyemonline.com\/index.php?rest_route=\/wp\/v2\/posts\/97\/revisions"}],"predecessor-version":[{"id":187,"href":"http:\/\/www.eyemonline.com\/index.php?rest_route=\/wp\/v2\/posts\/97\/revisions\/187"}],"wp:attachment":[{"href":"http:\/\/www.eyemonline.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=97"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.eyemonline.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=97"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.eyemonline.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=97"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}