Thread: printf not printing

  1. #1
    Registered User
    Join Date
    Feb 2008
    Location
    Yokohama
    Posts
    48

    printf not printing

    As with my previous question this is also some confusion about an example or two from K&R. Both of these programs are supposed to produce the same answers and I can follow the code easily enough but when I run the program it doesn't print the results. I am guessing that it is supposed to print the no. of characters that I input.

    Program 1:
    Code:
    #include <stdio.h>
    
    main()
    {
        long nc;
        nc = 0;
        while (getchar() != EOF)
            ++nc;
        printf("%ld\n", nc);
    
    }
    Program 2:
    Code:
    #include <stdio.h>
    
    main()
    {
        double nc;
        for (nc = 0; getchar() != EOF; ++nc)
            ;
        printf ("%.0f\n", nc);
    }
    Why would they not print? Or is my assumption that they should print something wrong?

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    Works for me
    Code:
    1 1 1 1
    123
    123
    ^Z
    17
    Note that K&R style is outdated
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        long nc = 0;
        while (getchar() != EOF)
            ++nc;
        printf("&#37;ld\n", nc);
        return 0;
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  3. #3
    Registered User
    Join Date
    Feb 2008
    Location
    Yokohama
    Posts
    48
    Hi,
    Thanks a lot. You are my saviour.
    It works for me too.
    I now understand your CTRL-Z.
    After Ctrl-Z I have to press return.

    I thought that it would print the result immediately after I press return (without entering Ctrl-Z).

    Not getting this would have meant the rest of the chapter would have been confusing. Thank you very much.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    to print result after enter you need to modify code in the following way:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        long nc = 0;
        int ch;
        while ((ch =getchar()) != EOF && ch != '\n')
            ++nc;
        printf("&#37;ld\n", nc);
    	return 0;
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  5. #5
    Registered User
    Join Date
    Feb 2008
    Location
    Yokohama
    Posts
    48
    Thanks again. That code works perfectly for me and with a bit of testing I understand that "ch != '\n'" is the reason it prints the result after enter. Why does this make such a difference?

    Also looking at your previous results and mine too where did the number 17 come from?

    1 1 1 1
    123
    123
    ^Z
    17
    I retried your numbers and got the same result. But I am guessing how to count them.
    First line has 9 characters: 4x1, 4xspace and \n . Maybe that's right.
    Next two lines have four characters each including newline and the total is 17?

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    I suppose so
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. making it portable.....?
    By ShadeS_07 in forum C Programming
    Replies: 11
    Last Post: 12-24-2008, 09:38 AM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. segmentation fault upon reload
    By yabud in forum C Programming
    Replies: 8
    Last Post: 12-18-2006, 06:54 AM
  4. Simple C question: user input to repeat a loop
    By evernaut in forum C Programming
    Replies: 2
    Last Post: 11-18-2006, 09:23 AM
  5. Drawing tables in C
    By stanoman in forum C Programming
    Replies: 5
    Last Post: 10-09-2003, 10:14 AM