{"id":4739,"date":"2021-05-08T00:01:47","date_gmt":"2021-05-08T07:01:47","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4739"},"modified":"2021-05-01T10:55:58","modified_gmt":"2021-05-01T17:55:58","slug":"unravel-the-mystery-code-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4739","title":{"rendered":"Unravel the Mystery Code &#8211; Solution"},"content":{"rendered":"<p>I hope you enjoyed crafting your solution for <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4720\">this month&#8217;s Exercise<\/a>. It&#8217;s just for fun, as I assume no one is going to mess with C to such a degree that their code becomes so completely unrecognizable. Still, C coders are a mischievous bunch.<br \/>\n<!--more--><br \/>\nHere is the way the mystery code is presented:<\/p>\n<pre class=\"screen\">\r\nbegin program\r\nstart\r\nprint(\"Hello!\")\r\nend\r\nstop<\/pre>\n<p>To me, this code looks like a programming language from the early days of computing, back in the mainframe days. This notion was my inspiration. To write the faux code, I replaced parts of C code I&#8217;d already written one step at a time. Here is the original program:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    puts(\"Hello!\");\r\n    return(0);\r\n}<\/pre>\n<p>Starting with Line 3, I replaced each element with a defined constant. The first line required two of them, as I didn&#8217;t want to mess with the space between <em>int<\/em> and <em>main()<\/em>:<\/p>\n<p><code>#define begin int<br \/>\n#define program main()<\/code><\/p>\n<p>So the word <code>begin<\/code> is replaced with the keyword <em>int<\/em> throughout the code, and <code>program<\/code> becomes <em>main()<\/em>. This trick works for such a short program, but for longer code it would present lotsa problems.<\/p>\n<p>The left brace is replaced with <code>start<\/code>:<\/p>\n<p><code>#define start {<\/code><\/p>\n<p>To substitute the <em>puts()<\/em> function I defined the <code>print()<\/code> macro. Argument <code>a<\/code> is used in both:<\/p>\n<p><code>#define print(a) puts(a);<\/code><\/p>\n<p>See how the semicolon is added above? It allows me to use <code>print()<\/code> without the semicolon, which adds to the faux code&#8217;s mystery.<\/p>\n<p>The last two <code>#defines<\/code> complete the process:<\/p>\n<p><code>#define end return(0);<br \/>\n#define stop }<\/code><\/p>\n<p>Here is the complete code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2021_05-Exercise.c\" rel=\"noopener\" target=\"_blank\">2021_05-Exercise.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\n#define begin int\r\n#define program main()\r\n#define start {\r\n#define print(a) puts(a);\r\n#define end return(0);\r\n#define stop }\r\n\r\nbegin program\r\nstart\r\nprint(\"Hello!\")\r\nend\r\nstop<\/pre>\n<p>I hope you had fun coming up with a similar solution. You can use <code>#define<\/code> to change the way the compiler reads C code, though I recommend not getting carried away. The true purpose of defining something new should be to make the code more readable or elegant. But nothing requires that C programmers use these same tools to be devious.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I hope you enjoyed crafting your solution for this month&#8217;s Exercise. It&#8217;s just for fun, as I assume no one is going to mess with C to such a degree that their code becomes so completely unrecognizable. Still, C coders &hellip; <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4739\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-4739","post","type-post","status-publish","format-standard","hentry","category-solution"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4739","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4739"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4739\/revisions"}],"predecessor-version":[{"id":4755,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4739\/revisions\/4755"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4739"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4739"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4739"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}