Print input one word per line I'm doing K&amp;R C Exercises.  I could use a code review to see how I can improve my logic. The excercise is:


  Write a program that prints its input one word per line with getchar.


My basic idea is we need to know whether or not we're in a word. We use the IN status.  Every time we hit a blank space, tab, or newline, the status is changed to OUT.  

Please offer only suggestions based on what i'm learning in the book up to this point, "symbolic constants,  if else, and loops only." Also thoughts on my comments would be welcome.

// Exercise1.11.cpp : Defines the entry point for the console application.
/* 
 Write a program that prints its input one word per line.
*/


#include "stdafx.h"

#define IN 1 
#define OUT 0

int _tmain(int argc, _TCHAR* argv[])
{
    int status = OUT; 
    int c; 

    while ((c = getchar()) != EOF){

        // Check to see if char is not a new line, blank or space.
        if ((c != '\n' &amp;&amp; c !=' ' &amp;&amp; c != '\t') ){

            if (status == OUT){
                putchar('\n');
                status = IN;
            }           

            putchar(c);
        }
        else {
            // char is a new line, blank or tab, so don't process them. 
            status = OUT;
        }
    }

    return 0;
}
Overall it looks like you are in the right track, though I think you have overcomplicated things a bit with that custom state flag and constants. C99 gives you the bool type, which could be used instead. Failing that, I would have settled with an integer. The defines seem overkill for such a tiny program.

You can omit the return 0 at the end of main, it is implicit (C99 standard).

_tmain and _TCHAR are Microsoft extensions, added by Visual Studio when you create a project with it. Be advised that this is a compiler extension and is therefore non-portable. The standard C entry point function is called just main and it should take no arguments, unless you need to process the command line.

Prefer exposing your #include dependencies instead of relying on the auto-generated stdafx.h that Visual Studio adds for you. Your program needs at least &lt;stdio.h&gt;.

This is how I would have written it, using a simple bool to keep track of the state, and, as @Loki suggested, using isspace() to test the character (always avoid reinventing the wheel, unless you really mean to):

#include &lt;stdio.h&gt;   // getchar() &amp; friends
#include &lt;ctype.h&gt;   // isspace()
#include &lt;stdbool.h&gt; // bool, true, false

int main(void)
{
    int c;
    bool needNewLine = false;

    while ((c = getchar()) != EOF) {

        if (isspace(c)) {
            needNewLine = true;
            continue;
        }

        if (needNewLine) {
            putchar('\n');
            needNewLine = false;
        }

        putchar(c);
    }
}