{"id":100,"date":"2005-10-06T18:47:51","date_gmt":"2005-10-06T08:47:51","guid":{"rendered":"http:\/\/www.somethinkodd.com\/oddthinking\/?p=100"},"modified":"2006-01-07T17:24:31","modified_gmt":"2006-01-07T06:24:31","slug":"magictrickc","status":"publish","type":"post","link":"https:\/\/www.somethinkodd.com\/oddthinking\/2005\/10\/06\/magictrickc\/","title":{"rendered":"magicTrick.c"},"content":{"rendered":"<p><!-- UnMarkedDown_2_01132526438--><\/p>\n<p>For the C programmers, here&#8217;s a little puzzle that I first produced while banging my head against the C syntax at uni.  Actually its more like a magic trick.<\/p>\n<p>Start by having a look at the following &#8220;code&#8221;. It starts out well &#8211; and it finishes well. But it gets a bit distracted in the middle.<\/p>\n<pre><code>#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    float eight = 8.0;\r\n    float *pEight = &amp;eight;\r\n    float oneEighth = 1.0\/*pEight;\r\n\r\n    oneEighth += oneEighth;\r\n    \/\/ If you are using a C++ compiler, this line will be ignored as a comment.\r\n    \/\/ That's a shame because it is the previous line which is buggy!\r\n\r\n    #if(this) was Python, this buggy line would be ignored: oneEighth += oneEighth;\r\n\r\n    &lt;!-- If this was HTML, however, it would be this buggy line that was ignored:\r\n    oneEighth += oneEighth; --&gt;\r\n\r\n    \/\/ As you can see, C has a remarkably complicated syntax for a relatively small language.\r\n    \/\/ It is too hard to predict, and is full of nasty tricks. I don't like its syntax much. \r\n    \/\/ I wish other languages didn't copy it.\r\n    \/\/ In fact, frankly, I think its syntax is a load of #%$@*\/;\r\n\r\n    printf(\"The final value of oneEighth = %f\\n\", oneEighth);\r\n    return 0;\r\n}\r\n<\/code><\/pre>\n<p>So, what will happen when you compile this code? Answer below, so have a stab before reading on.<\/p>\n<p><more><\/more><\/p>\n<p>The answer is surprising &#8211; here&#8217;s bit where people get dazzled by the magic when I demonstrate it in front of them.<\/p>\n<p>You don&#8217;t get any error message at all!  This is valid C code! It compiles and it runs. No honestly. I even checked that WordPress didn&#8217;t chew it up. You can try it for yourself, later.<\/p>\n<p>But that&#8217;s not the weird part!  The weird part is what happens when you run it! This is what it prints to stdout:<\/p>\n<pre><code>The final value of oneEighth = 1.000000\r\n<\/code><\/pre>\n<p>Weird! So how could it have successfully doubled the value of <code>oneEighth<\/code> three times, despite the various commenting standards?<\/p>\n<p>Have you figured it out yet? <\/p>\n<p>I stumbled over this trick before the syntax-colouring was popular; I had a genuine bug in my code caused by the same trick. However, these days, pasting this text into a syntax-colouring editor makes the trick so obvious, I won&#8217;t even bother to reveal the solution here.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A magic trick for C programmers to try,<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_s2mail":"","footnotes":""},"categories":[28,33,34],"tags":[],"class_list":["post-100","post","type-post","status-publish","format-standard","hentry","category-doubleplus-geek","category-puzzle-solving","category-software-development"],"_links":{"self":[{"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/posts\/100","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/comments?post=100"}],"version-history":[{"count":0,"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/posts\/100\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/media?parent=100"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/categories?post=100"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/tags?post=100"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}