Thread: Character counting issues from Kernighan text

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    2

    Character counting issues from Kernighan text

    I've just started to learn C programming, using book 'The C Programming Language' by Kernighan and Ritchie.

    I'm on 1.5.2, and the following program (provided in the book) is supposed to count characters that I type in:

    Code:
    #include <stdio.h>
    
    /* count characters in input; 1st version */
    main()
    {
        long nc;
    
        nc = 0;
        while (getchar() != EOF)
            ++nc;
        printf("%ld\n", nc);
    }
    However, when I build and run the program (using Code::Blocks and Pelles C IDE compilers), the program does not count and display the characters I input.

    On Code::Blocks, I received no warnings or errors when compiling, but on Pelles, I received the following warning on compilation:
    C:\Users\...\Documents\cpractice2\cpracticeb1.5.2. c(4): warning #2099: Missing type specifier; assuming 'int'.

    I would like to know what may be wrong. Thanks.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    It's not supposed to (re)display the characters you type, just the total number, which it does. I'm guessing your issue is "how do I type EOF at my computer", which given you are on windows is "type Ctrl-Z on a line by itself".

    (As to the warning, back in the old days you could just leave int off in a surprising number of places. Nowadays, not so much.)

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    2

    Thumbs up

    Hey thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linking to shlwapi.lib in C, MSVC CMD.
    By Joerge in forum Windows Programming
    Replies: 4
    Last Post: 08-07-2009, 05:18 PM
  2. text issues
    By pastitprogram in forum C++ Programming
    Replies: 2
    Last Post: 10-10-2008, 05:12 PM
  3. Replies: 11
    Last Post: 10-07-2008, 06:19 PM
  4. Counting Number of Words in a Text File Using C
    By wvu2005 in forum C Programming
    Replies: 16
    Last Post: 09-27-2005, 11:45 AM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 07:35 PM

Tags for this Thread