title: Kernighan and Ritchie Exercise 2-2 Debugging?
body: <p>I am working through K&amp;R (2nd edition) for my own edification and encountered the following exercise (exercise 2-2 p42):</p>
  
  <pre><code>Write a loop equivalent to the following without using &amp;&amp; or ||:
  
     for (i=0; i&lt;lim-1 &amp;&amp; (c=getchar()) != '\n' &amp;&amp; c != EOF; ++i)
             s[i] = c;
  </code></pre>
  
  <p>This was my solution:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  /* write a loop equivalent to the following without using &amp;&amp; or ||
  
     for (i=0; i&lt;lim-1 &amp;&amp; (c=getchar()) != '\n' &amp;&amp; c != EOF; ++i)
             s[i] = c;
  */
  
  int main()
  {
      int counter = 0, lim = 1000;
      int s[lim], c;
  
      while(counter &lt; lim-1)
      {
          while((c = getchar()) != '\n')
          {
              while(c != EOF)
              {
                  s[counter] = c;
              }
          }
          counter++;
      }   
      return 0;
  }
  </code></pre>
  
  <p>I was expecting the indented loops and therefore the entire program to exit normally once it encountered a newline character (<code>'\n'</code>) or an EOF character (<code>Ctrl-d</code> on my Linux machine), but to my surprise it happily soldiers on. I tried to debug it using gdb but still could not figure it out.</p>
  
  <p>What am I not seeing?</p>
  
  <p>Addendum: I tried to reverse the sequence of tests the while loops perform and added an if statement to break out of the outer loop if <code>c == '\n'</code> but am still not seeing it! I am also having difficulty trying to run GDB entering text into the command line and simultaneously printing the value of <code>c</code>, even when I tried to link gdb to the pid of a running copy of the executable. I realize that there are probably other ways to solve this exercise, e.g. setting an <code>OK_TO_EXECUTE</code> flag or variable that is true only if all three conditions are met, but I am bothered by the fact that I seem unable to find the bug in a seemingly simple program. This is precisely why I am returning to K&amp;R to go through the book more thoroughly and to solve the exercises properly.</p>
  
  <p>Redone code (still buggy!!!):</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  /* write a loop equivalent to the following without using &amp;&amp; or ||
  
     for (i=0; i&lt;lim-1 &amp;&amp; (c=getchar()) != '\n' &amp;&amp; c != EOF; ++i)
             s[i] = c;
  */
  
  int main()
  {
      int counter = 0, lim = 1000;
      int s[lim], c;
  
  
  
      while((c = getchar()) != EOF)
      { 
          if ( c == '\n')
              break;
  
          while(c != '\n')
          {
              while(counter &lt; lim-1)
              {
                  s[counter] = c;
                  counter++;
              }
          }
      }   
      return 0;
  }
  </code></pre>
  
  <p>SOLVED! - I think! I think I have finally figured it out. The inner loops as written in my redone solution will still loop endlessly or at lease till <code>lim</code> is reached. I added break
  statements and think I am on my way to a solution.</p>
  
  <p>I am still wrestling with how to run gdb on this problem though; enter the command line entries AND <code>print</code> the value of <code>c</code>. Linking gdb to the pid of the executable still did not work as expected. I even posted a <a href="https://stackoverflow.com/questions/15865614/how-do-i-run-gdb-enter-text-into-the-command-line-and-see-how-the-executable-tr">separate question</a> regarding gdb.</p>