{"id":3411,"date":"2018-12-22T00:01:08","date_gmt":"2018-12-22T08:01:08","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3411"},"modified":"2019-11-23T21:57:08","modified_gmt":"2019-11-24T05:57:08","slug":"tick-tock-goes-the-clock-part-ii","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3411","title":{"rendered":"Tick-Tock Goes the Clock, Part II"},"content":{"rendered":"<p>From <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3409\">last week&#8217;s Lesson<\/a>, the code I wrote for my clock program outputs the current hour and minute. I also want it to update, to keep running and output the next minute and so on. To me, that&#8217;s the difference between a clock program and code that just outputs the current time.<br \/>\n<!--more--><br \/>\nAn updating clock program requires some sort of loop. In the following code, I add a loop to the program:<\/p>\n<pre class=\"screen\">#include &lt;stdio.h&gt;\r\n#include &lt;time.h&gt;\r\n\r\nint main()\r\n{\r\n    time_t now;\r\n    struct tm *clock;\r\n\r\n    while(1)\r\n    {\r\n        <span class=\"comments\">\/* fetch the current time *\/<\/span>\r\n        time(&amp;now);\r\n        <span class=\"comments\">\/* fill the time structure *\/<\/span>\r\n        clock = localtime(&amp;now);\r\n        <span class=\"comments\">\/* display the clock *\/<\/span>\r\n        printf(\"It is now %2d:%02d\\n\",\r\n                clock-&gt;tm_hour,\r\n                clock-&gt;tm_min\r\n              );\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The code is identical, save for the addition of the endless <em>while<\/em> loop. Rather than display the time in discrete minute values, the output is a continuous stream of text:<\/p>\n<p><code>It is now 12:25<br \/>\nIt is now 12:25<br \/>\nIt is now 12:25<br \/>\nIt is now 12:25<br \/>\nIt is now 12:25<br \/>\n...<\/code><\/p>\n<p>Eventually, the time changes. This program qualifies as a clock, but it&#8217;s not what I had in mind. Instead, I want to see output like this:<\/p>\n<p><code>It is now 12:25<br \/>\nIt is now 12:26<br \/>\nIt is now 12:27<br \/>\n...<\/code><\/p>\n<p>The output above ticks every minute, updating the clock program&#8217;s output with the system time. To make this possible, a second loop is required. I would encourage you to try to concoct the second loop on your own. That&#8217;s because it&#8217;s not the easiest thing to do, though once you see the answer it makes sense.<\/p>\n<p><em>Yes<\/em>! Go off and figure out the second loop, the one that delays output for a minute. Do it on your own!<\/p>\n<p>I&#8217;ll wait.<\/p>\n<p>[ &#9834; <em>musical interlude<\/em> &#9835; ]<\/p>\n<p>Did you do it? Probably not. Anyway.<\/p>\n<p>Consider that the <em>time_t<\/em> value represents seconds ticked. So to make output pause for one minute, I add 60 to the <em>time_t<\/em> value stored in the <code>now<\/code> variable and then wait for the output of the <em>time()<\/em> function to be greater than or equal to that value. Here is the test:<\/p>\n<p><code>while( now+60 &gt; time(NULL) )<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;;<\/code><\/p>\n<p>In the <em>while<\/em> loop&#8217;s test, 60 seconds are added to the value of <code>now<\/code>. The <code>time(NULL)<\/code> expression returns the current clock tick value. So the loop waits 60 seconds; when <code>time(NULL)<\/code> returns a value 60 seconds in the future, the loop stops. At that point, the next clock value is output:<\/p>\n<p><code>It is now 12:32<br \/>\nIt is now 12:33<br \/>\nIt is now 12:34<br \/>\nIt is now 12:35<br \/>\nIt is now 12:36<br \/>\n^C<\/code><\/p>\n<p><a href=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/12\/1222b.c\">Click here<\/a> to view the complete code.<\/p>\n<p>With a little output modification, I have my clock program. I just noticed one problem: The clock&#8217;s time wasn&#8217;t in sync with the system time. I checked the seconds elapsed, and discovered that the code counts the next minute based on when the program starts, not when the system clock hits :00 seconds. The surprising thing is that only one change is required to fix this problem \u2014 and it might even be something you came up with, if you worked on your own solution to determining how to implement the second loop.<\/p>\n<p>See <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3421\">next week&#8217;s Lesson<\/a> for the solution.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Outputting and updating the current time. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3411\">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-3411","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\/3411","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=3411"}],"version-history":[{"count":8,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3411\/revisions"}],"predecessor-version":[{"id":3881,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3411\/revisions\/3881"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3411"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3411"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3411"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}