{"id":6615,"date":"2024-10-19T00:01:09","date_gmt":"2024-10-19T07:01:09","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6615"},"modified":"2024-10-12T15:33:37","modified_gmt":"2024-10-12T22:33:37","slug":"the-second-kr-program-that-no-one-talks-about","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6615","title":{"rendered":"The Second K&#038;R Program (That No One Talks About)"},"content":{"rendered":"<p>The original K&amp;R, the first C programming book &mdash; and truly the programming book all others are based upon, is famous for its &#8220;Hello, world!&#8221; program. It&#8217;s the first program in the book. But what about the second program? Do you know what it is?<br \/>\n<!--more--><br \/>\nFirst, the book&#8217;s proper title is <em>The C Programming Language<\/em>, Copyright 1978. I have the first edition, 20th printing, which I bought in person at San Diego Technical Books. Sadly, this once awesome bookstore is gone now, but I still have plenty of books I purchased back in its heyday.<\/p>\n<p>Second, the code presented as the book&#8217;s second program doesn&#8217;t compile. Here&#8217;s the code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2024_10_19-Lesson-a.c\" rel=\"noopener\" target=\"_blank\">2024_10_19-Lesson-a.c<\/a><\/h3>\n<pre class=\"screen\">\r\n<span class=\"comments\">\/* print Fahrenheit-Celsius table\r\n   for f = 0, 20, ..., 300 *\/<\/span>\r\nmain()\r\n{\r\n    int lower, upper, step;\r\n    float fahr, celsius;\r\n\r\n    lower = 0;        <span class=\"comments\">\/* lower limit of temperature table *\/<\/span>\r\n    upper = 300;     <span class=\"comments\">\/* upper limit *\/<\/span>\r\n    step = 20;        <span class=\"comments\">\/* step size *\/<\/span>\r\n\r\n    fahr = lower;\r\n    while (fahr &lt;= upper) {\r\n        celsius = (5.0\/9.0) * (fahr-32.0);\r\n        printf(\"%4.0f %6.1f\\n\",fahr,celsius);\r\n        fahr = fahr + step;\r\n    }\r\n}<\/pre>\n<p>A few things you may notice right away, since it&#8217;s been almost 50 years since the book was published: The <em>main()<\/em> function isn&#8217;t declared as an <em>int<\/em>. Further, no <em>return<\/em> statement is present. This approach is how C code was written for some time. Back when I first learned C, <em>main()<\/em> wasn&#8217;t typed, or often typed <em>void<\/em>, and the <em>return<\/em> statement was optional.<\/p>\n<p>Another thing to notice, and the reason why the code won&#8217;t compile, is that the <em>printf()<\/em> statement requires the <code>stdio.h<\/code> header file, which is missing. The reason stated in the book is that the <code>#include<\/code> directive isn&#8217;t covered until Chapter 7. Still this is the second program presented in the book.<\/p>\n<p>The following code is my attempt to update the original program to the ANSI standard.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2024_10_19-Lesson-b.c\" rel=\"noopener\" target=\"_blank\">2024_10_19-Lesson-b.c<\/a><\/h3>\n<pre class=\"screen\">\r\n<span class=\"comments\">\/* print Fahrenheit-Celsius table\r\n   for f = 0, 20, ..., 300 *\/<\/span>\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int lower, upper, step;\r\n    float fahr, celsius;\r\n\r\n    lower = 0;        <span class=\"comments\">\/* lower limit of temperature table *\/<\/span>\r\n    upper = 300;     <span class=\"comments\">\/* upper limit *\/<\/span>\r\n    step = 20;        <span class=\"comments\">\/* step size *\/<\/span>\r\n\r\n    fahr = lower;\r\n    while (fahr &lt;= upper) {\r\n        celsius = (5.0\/9.0) * (fahr-32.0);\r\n        printf(\"%4.0f %6.1f\\n\",fahr,celsius);\r\n        fahr = fahr + step;\r\n    }\r\n    return 0;\r\n}<\/pre>\n<p>I&#8217;ve added the <code>#include<\/code> directive and a <em>return<\/em> statement. Otherwise, this is the same code published 47 years ago. And it runs when compiled on a modern C compiler. Here&#8217;s the output:<\/p>\n<p><code>&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;-17.8<br \/>\n&nbsp;&nbsp;20&nbsp;&nbsp;&nbsp;-6.7<br \/>\n&nbsp;&nbsp;40&nbsp;&nbsp;&nbsp;&nbsp;4.4<br \/>\n&nbsp;&nbsp;60&nbsp;&nbsp;&nbsp;15.6<br \/>\n&nbsp;&nbsp;80&nbsp;&nbsp;&nbsp;26.7<br \/>\n&nbsp;100&nbsp;&nbsp;&nbsp;37.8<br \/>\n&nbsp;120&nbsp;&nbsp;&nbsp;48.9<br \/>\n&nbsp;140&nbsp;&nbsp;&nbsp;60.0<br \/>\n&nbsp;160&nbsp;&nbsp;&nbsp;71.1<br \/>\n&nbsp;180&nbsp;&nbsp;&nbsp;82.2<br \/>\n&nbsp;200&nbsp;&nbsp;&nbsp;93.3<br \/>\n&nbsp;220&nbsp;&nbsp;104.4<br \/>\n&nbsp;240&nbsp;&nbsp;115.6<br \/>\n&nbsp;260&nbsp;&nbsp;126.7<br \/>\n&nbsp;280&nbsp;&nbsp;137.8<br \/>\n&nbsp;300&nbsp;&nbsp;148.9<\/code><\/p>\n<p>If you truly want to go back in time, you can compile the code with the <code>-ansi<\/code> switch. If so, you can remove the <em>int<\/em> before <em>main()<\/em> and pull out the <em>return<\/em> statement as well. (Originally, I commented out these items using the <code>\/\/<\/code> style comments &mdash; but these aren&#8217;t allowed in ANSI C!)<\/p>\n<p>As I wrote earlier, K&amp;R is one of the original computer books. To prove how other programming books are based on it is demonstrated by this very program. When I learned BASIC on the TRS-80, I used the book shown in Figure 1, which came with my Model III microcomputer. On Page 25 of this book is the BASIC Celsius-to-Fahrenheit conversion program, shown in Figure 2.<\/p>\n<div id=\"attachment_6622\" style=\"width: 522px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-6622\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2024\/10\/1019-learning_basic_trs80.png\" alt=\"\" width=\"512\" height=\"386\" class=\"size-full wp-image-6622\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2024\/10\/1019-learning_basic_trs80.png 512w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2024\/10\/1019-learning_basic_trs80-300x226.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2024\/10\/1019-learning_basic_trs80-398x300.png 398w\" sizes=\"auto, (max-width: 512px) 100vw, 512px\" \/><p id=\"caption-attachment-6622\" class=\"wp-caption-text\">Figure 1. The BASIC programming instruction manual that came with my TRS-80 Model III computer. (Yes, I still own this copy.)<\/p><\/div>\n<div id=\"attachment_6621\" style=\"width: 522px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-6621\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2024\/10\/1019-learning_basic_ctof.png\" alt=\"\" width=\"512\" height=\"382\" class=\"size-full wp-image-6621\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2024\/10\/1019-learning_basic_ctof.png 512w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2024\/10\/1019-learning_basic_ctof-300x224.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2024\/10\/1019-learning_basic_ctof-402x300.png 402w\" sizes=\"auto, (max-width: 512px) 100vw, 512px\" \/><p id=\"caption-attachment-6621\" class=\"wp-caption-text\">Figure 2. Page 25 of the BASIC programming manual, which lists the Celsius-to-Fahrenheit  conversion program at the bottom of the page.<\/p><\/div>\n<p>As far as I can tell, I&#8217;ve never used this type of temperature conversion program in any of my programming books. It&#8217;s tempting, but I don&#8217;t think I&#8217;ve ever done it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Everyone knows about the &#8220;Hello, world!&#8221; program. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6615\">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-6615","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\/6615","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=6615"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6615\/revisions"}],"predecessor-version":[{"id":6629,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6615\/revisions\/6629"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6615"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6615"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6615"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}