I am working through K&R (2nd edition) for my own edification and encountered the following exercise (exercise 2-2 p42):

```
Write a loop equivalent to the following without using && or ||:

   for (i=0; i<lim-1 && (c=getchar()) != '\n' && c != EOF; ++i)
           s[i] = c;

```

This was my solution:

```
#include <stdio.h>

/* write a loop equivalent to the following without using && or ||

   for (i=0; i<lim-1 && (c=getchar()) != '\n' && c != EOF; ++i)
           s[i] = c;
*/

int main()
{
    int counter = 0, lim = 1000;
    int s[lim], c;

    while(counter < lim-1)
    {
        while((c = getchar()) != '\n')
        {
            while(c != EOF)
            {
                s[counter] = c;
            }
        }
        counter++;
    }   
    return 0;
}

```

I was expecting the indented loops and therefore the entire program to exit normally once it encountered a newline character (`'\n'`) or an EOF character (`Ctrl-d` 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.

What am I not seeing?

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 `c == '\n'` 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 `c`, 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 `OK_TO_EXECUTE` 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&R to go through the book more thoroughly and to solve the exercises properly.

Redone code (still buggy!!!):

```
#include <stdio.h>

/* write a loop equivalent to the following without using && or ||

   for (i=0; i<lim-1 && (c=getchar()) != '\n' && 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 < lim-1)
            {
                s[counter] = c;
                counter++;
            }
        }
    }   
    return 0;
}

```

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 `lim` is reached. I added break
statements and think I am on my way to a solution.

I am still wrestling with how to run gdb on this problem though; enter the command line entries AND `print` the value of `c`. Linking gdb to the pid of the executable still did not work as expected. I even posted a [separate question](https://stackoverflow.com/questions/15865614/how-do-i-run-gdb-enter-text-into-the-command-line-and-see-how-the-executable-tr) regarding gdb.