{"id":4358,"date":"2020-09-19T00:01:32","date_gmt":"2020-09-19T07:01:32","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4358"},"modified":"2020-09-19T08:58:02","modified_gmt":"2020-09-19T15:58:02","slug":"looping-with-testing-variables","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4358","title":{"rendered":"Looping with Testing Variables"},"content":{"rendered":"<p>Often in my code I use a variable to both fetch data and to test the data. Before using this variable, it must be initialized, lest the test act on uninitialized garbage. A clever solution is necessary to avoid this situation.<br \/>\n<!--more--><br \/>\nAs an example, consider <em>int<\/em> variable <code>c<\/code> in the following code, a crude file dumping program:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2020_09_19-Lesson.c\" rel=\"noopener noreferrer\" target=\"_blank\">2020_09_19-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n\r\n<span class=\"comments\">\/* a simple file dumper *\/<\/span>\r\nint main(int argc, char *argv[])\r\n{\r\n    FILE *dumpme;\r\n    int c;\r\n\r\n    <span class=\"comments\">\/* check for filename *\/<\/span>\r\n    if( argc &lt; 2 )\r\n    {\r\n        puts(\"Format: dumper filename\");\r\n        exit(1);\r\n    }\r\n\r\n    <span class=\"comments\">\/* open and process file *\/<\/span>\r\n    dumpme = fopen(argv[1],\"r\");\r\n    if( dumpme==NULL )\r\n    {\r\n        printf(\"Unable to read %s\\n\",argv[1]);\r\n        exit(2);\r\n    }\r\n\r\n    while((c=fgetc(dumpme)) != EOF)\r\n        printf(\"%02X \",c);\r\n    putchar('\\n');\r\n\r\n    fclose(dumpme);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Line 25 is a common way to express both obtaining and comparing data:<\/p>\n<p><code>while((c=fgetc(dumpme)) != EOF)<\/code><\/p>\n<p>Working inside out, the <em>fgetc()<\/em> function fetches a character from the file represented by the <code>dumpme<\/code> handle. This character is stored in <em>int<\/em> variable <code>c<\/code>. This expression is enclosed in parentheses, which yields the value stored in variable <code>c<\/code>. The value is then compared with the <code>EOF<\/code> constant. If unequal, the loop runs. When <code>c<\/code> is equal to <code>EOF<\/code>, the loop stops.<\/p>\n<p>Ideally, the <em>while<\/em> loop comparison should be <code>while(c != EOF)<\/code>. This comparison is less cryptic and less prone to errors or exploits. The problem is where to put the <code>c = fgetc(dumpme)<\/code> statement that fetches a character from the open file.<\/p>\n<p>You could read the character before the loop, which looks like this:<\/p>\n<pre class=\"screen\">\r\nc = fgetc(dumpme);\r\nwhile(c != EOF)\r\n{\r\n    printf(\"%02X \",c);\r\n    c = fgetc(dumpme);\r\n}<\/pre>\n<p>This construction is legal, and it works, but it results in the <em>fgetc()<\/em> function called outside and inside the loop, which I think is kind of dumb. Still, it&#8217;s a valid and clean approach, I would offer that it&#8217;s the best way to initialize and use the variable.<\/p>\n<p>Another solution, on that I use frequently, is to initialize the variable to some harmless value before the loop starts:<\/p>\n<pre class=\"screen\">\r\nc = 1;\r\nwhile(c != EOF)\r\n{\r\n    c = fgetc(dumpme);\r\n    printf(\"%02X \",c);\r\n}<\/pre>\n<p>A better way to initialize the variable is to set it to the opposite value being tested:<\/p>\n<pre class=\"screen\">\r\nc = !EOF;\r\nwhile(c != EOF)\r\n{\r\n    c = fgetc(dumpme);\r\n    printf(\"%02X \",c);\r\n}<\/pre>\n<p>Above, the value of variable <code>c<\/code> is initialized to the opposite value of <code>EOF<\/code>. The result is that the <em>while<\/em> loop test passes (naturally) and the loop proceeds.<\/p>\n<p>The problem with both of these approaches is that the <code>EOF<\/code> is read and stored before the loop stops, so its value appears in the output. (The value is typically -1, which outputs as <code>FFFFFFFF<\/code> on some systems.) Because the the <code>EOF<\/code> is output, these approaches are improper.<\/p>\n<p>Yet another solution, one I prefer, involves an endless loop. Use an <em>if<\/em> comparison in the loop to break out when the terminating condition is encountered:<\/p>\n<pre class=\"screen\">\r\nwhile(1)\r\n{\r\n    c = fgetc(dumpme);\r\n    if( c == EOF )\r\n        break;\r\n    printf(\"%02X \",c);\r\n}<\/pre>\n<p>This solution works, and more code could be added to check for file read errors other than the <code>EOF<\/code>.<\/p>\n<p>Of all these methods, I admire the <code>c = !EOF<\/code> initialize approach the best. It may not be appropriate in this Lesson&#8217;s example, but it provides a solid way to initialize a variable before it&#8217;s used, specifically to avoid a condition like <code>c != EOF<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The issue is how to best initialize a variable used in a loop at its terminating condition. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4358\">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-4358","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\/4358","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=4358"}],"version-history":[{"count":7,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4358\/revisions"}],"predecessor-version":[{"id":4378,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4358\/revisions\/4378"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4358"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4358"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4358"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}