{"id":6380,"date":"2024-05-04T00:01:36","date_gmt":"2024-05-04T07:01:36","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6380"},"modified":"2024-04-27T10:46:03","modified_gmt":"2024-04-27T17:46:03","slug":"tending-toward-obfuscation","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6380","title":{"rendered":"Tending Toward Obfuscation"},"content":{"rendered":"<p>I&#8217;m always mindful of the beginner when I write code for this blog. Even with my own code, I often go to pains to write things &#8220;the long way&#8221; just because I&#8217;m in the habit. Not every coder is so thoughtful. The reason is that C tends toward obfuscation. We must revel in this capability.<br \/>\n<!--more--><br \/>\nOf course, I can write delightfully cryptic code. It&#8217;s compact. But many programmers know that it&#8217;s a tough knot to unwind in the future when you may need to revisit your obfuscated code. It&#8217;s the &#8220;what the heck was I thinking?&#8221; phenomenon. But for me, what happens is often the opposite as I initially write code unwound.<\/p>\n<p>Consider this code from <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5812\">an earlier post<\/a> on a filter I wrote to convert spaces to the non-breakable space HTML code, &amp;nbsp;:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2023_04_01-Lesson.c\" rel=\"noopener\" target=\"_blank\">2023_04_01-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int ch;\r\n\r\n    while(1)\r\n    {\r\n        ch = getchar();\r\n        if( ch==EOF )\r\n            break;\r\n        if( ch==' ' )\r\n            printf(\"&amp;nbsp;\");\r\n        else\r\n            putchar(ch);\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>This code works and is understandable by most beginning C programmers, but it can be shortened. The key for me is that the <em>while<\/em> loop terminates upon the EOF character being encountered. Therefore, the loop can be re-written with the EOF as its terminating condition, saving several lines of code.<\/p>\n<p>You can write <code>while(ch!=EOF)<\/code> to start the loop, providing that you first initialize variable <code>ch<\/code>:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2024_05_04-Lesson-a.c\" rel=\"noopener\" target=\"_blank\">2024_05_04-Lesson-a.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int ch = '\\0';\r\n\r\n    while(ch!=EOF)\r\n    {\r\n        ch = getchar();\r\n        if( ch==' ' )\r\n            printf(\"&amp;nbsp;\");\r\n        else\r\n            putchar(ch);\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>In this update, variable <code>ch<\/code> is initialized to the null character. The <em>while<\/em> condition works because <code>ch<\/code> already holds a value, which isn&#8217;t the EOF. Then the rest of the loop continues, converting space characters from standard input into the HTML unbreakable space code. Yet more compacting is possible.<\/p>\n<p>The <em>getchar()<\/em> function returns a single character from standard input. The value is an integer, allowing the EOF constant to be read. But the expression <code>ch = getchar()<\/code> also generates a value, the character returned. Therefore, this statement can be used as the <em>while<\/em> loop&#8217;s condition:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2024_05_04-Lesson-b.c\" rel=\"noopener\" target=\"_blank\">2024_05_04-Lesson-b.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int ch;\r\n\r\n    while( (ch=getchar()) != EOF )\r\n    {\r\n        if( ch==' ' )\r\n            printf(\"&amp;nbsp;\");\r\n        else\r\n            putchar(ch);\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>while<\/em> loop&#8217;s condition now both reads a character from standard input and compares the character with the EOF. If true, the loop stops.<\/p>\n<p>Reading the expression from the inside out: <code>ch=getchar()<\/code> reads a character from standard input and stores it in variable <code>ch<\/code>. This expression is enclosed in parentheses, where it evaluates as the character returned, the value in variable <code>ch<\/code>. This value is then compared with the EOF. If it&#8217;s not equal, the loop repeats: The character read is compared with a space, replaced if true, output otherwise. When the character is the EOF, the loop stops.<\/p>\n<p>This approach is how I believe most experienced C programmers would write the loop. First, it takes fewer statements. Second, it&#8217;s cryptic. When teaching, I would need to explain all the mechanics, which is what I did above. It&#8217;s easier for me, and better for the beginner, to see things done the long way and then worry about tightening the code later.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>C has the delightful tendency to be remarkably cryptic. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6380\">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":[2],"tags":[],"class_list":["post-6380","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6380","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=6380"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6380\/revisions"}],"predecessor-version":[{"id":6402,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6380\/revisions\/6402"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6380"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6380"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6380"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}