printf not working inside main scope

This is a discussion on printf not working inside main scope within the C Programming forums, part of the General Programming Boards category; Hey guys, I just picked up k&r the other day and started learning C. Unfortunately, I've hit a snag early ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    2

    Question printf not working inside main scope

    Hey guys, I just picked up k&r the other day and started learning C. Unfortunately, I've hit a snag early on. For some reason I can't get printf to work inside the main scope when sub-scopes are involved. I'll show you what I have (the following code is a simple word counter straight from the k&r text):

    Code:
    #include <stdio.h>
    
    #define IN  1
    #define OUT 0
    
    main()
    {
        int c, nl, nw, nc, state;
    
        state = OUT;
        nl = nw = nc = 0;
        while ((c = getchar()) != EOF) {
            ++nc;
            if (c == '\n')
                ++nl;
            if (c == ' ' || c == '\n' || c == '\t')
                state = OUT;
            else if (state == OUT) {
                state = IN;
                ++nw;
            }
        }
        printf("%d %d %d\n", nl, nw, nc);
    }
    If I move the printf function inside the sub-scope it becomes visible but it displays itself each time the loop runs and I end up with 10 or more instances of printf (depending on the input). As it is now, nothing is displayed after input. I had the same problem with an even simpler program (also from k&r). Am I missing something obvious?

    I use code::blocks with mingw gcc if that makes any difference.

  2. #2
    Registered User GL.Sam's Avatar
    Join Date
    Aug 2009
    Posts
    88
    The program is looking for EOF (End Of File) on input and hence your loop never ends. Under Windows you could simulate the case by Ctrl+Z, on linux - Ctrl+D.

    btw, K&R is not the best book for a complete novice.

  3. #3
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    It works for me, as long as you hit EOF after your done. Ctrl-Z on windows and I believe Ctrl-D on unix.
    Spidey out!

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    2
    Quote Originally Posted by GL.Sam View Post
    The program is looking for EOF (End Of File) on input and hence your loop never ends. Under Windows you could simulate the case by Ctrl+Z, on linux - Ctrl+D.

    btw, K&R is not the best book for a complete novice.
    I have experience with assembly and c++, it's just been about five years and I'm rusty as heck :P but yeah k&r definitely doesn't go into much detail on EOF, haha.

    Thanks guys, I'll be back with whatever stumps me next.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 11:39 PM
  2. Help with a C Programing Quiz project
    By dilemma in forum C Programming
    Replies: 12
    Last Post: 05-15-2009, 04:35 PM
  3. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 04:25 PM
  4. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21