{"id":6469,"date":"2024-07-08T00:01:58","date_gmt":"2024-07-08T07:01:58","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6469"},"modified":"2024-07-06T06:07:41","modified_gmt":"2024-07-06T13:07:41","slug":"clear-the-screen-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6469","title":{"rendered":"Clear the Screen &#8211; Solution"},"content":{"rendered":"<p>The task for <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6457\">this month&#8217;s Exercise<\/a> is to clear the screen. I pray you didn&#8217;t arrive at a solution that outputs multiple blank lines. No, you must use the ANSI codes offered in the exercise post to manipulate the cursor and wipe clean the screen. Or you could cheat, which is what I did.<br \/>\n<!--more--><br \/>\nI didn&#8217;t really cheat. No, I obtained guidance.<\/p>\n<p>The <em>clear<\/em> command wipes the terminal window screen and homes the cursor. It does so by outputting text, the same ANSI codes that appear in the table presented in the challenge post. I redirected this output to a file, then used the <em>hexdump<\/em> utility to view its contents:<\/p>\n<p><code>00000000&nbsp;&nbsp;1b&nbsp;5b&nbsp;48&nbsp;1b&nbsp;5b&nbsp;32&nbsp;4a&nbsp;1b&nbsp;&nbsp;5b&nbsp;33&nbsp;4a&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|.[H.[2J.[3J|<br \/>\n0000000b<\/code><\/p>\n<p>Hex code <code>1b<\/code> is the escape character, ESC on the keyboard and <code>\\e<\/code> in C. In the dump, I see three ANSI commands:<\/p>\n<p><code>\\e[H<\/code> &#8211; home the cursor<br \/>\n<code>\\e[2J<\/code> &#8211; erase the entire screen<br \/>\n<code>\\e[3J<\/code> &#8211; erase saved lines<\/p>\n<p>This is the output I generated in my <em>cls()<\/em> function for my solution:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2024_07-Exercise.c\" rel=\"noopener\" target=\"_blank\">2024_07-Exercise.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nvoid cls(void)\r\n{\r\n    printf(\"\\e[H\\e[2J\\e[3J\");\r\n}\r\n\r\nint main()\r\n{\r\n    printf(\"Prepare to clear the screen!\");\r\n    getchar();\r\n    cls();\r\n\r\n    return 0;\r\n}<\/pre>\n<p>Even so, and yes I kinda did cheat, you can get by with the following <em>printf()<\/em> statement instead:<\/p>\n<p><code>printf(\"\\e[H\\e[2J\");<\/code><\/p>\n<p>These ANSI codes also clear the screen, but they don&#8217;t erase saved lines. I&#8217;m not clear on what the difference is, so either way if you used these codes to clear the screen, you passed the challenge.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The task for this month&#8217;s Exercise is to clear the screen. I pray you didn&#8217;t arrive at a solution that outputs multiple blank lines. No, you must use the ANSI codes offered in the exercise post to manipulate the cursor &hellip; <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6469\">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-6469","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\/6469","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=6469"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6469\/revisions"}],"predecessor-version":[{"id":6488,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6469\/revisions\/6488"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6469"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6469"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6469"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}