Thread: Count Characters

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    2

    Question Count Characters

    Hi all,

    I am very new to C and am working through the book (The C Programming Language).

    This program does not displaying the character count when I enter text. Can anybody tell me if I am missing something, this is straight from the text on page 20 (I'm sure lots of people will have or have read this book).

    Code:
    #include <stdio.h>
    
    
    main()
    {
        double nc;
        for (nc = 0; gechar() != EOF; ++nc)
             ;
        printf("%.0f\n", nc);
    }
    Cheers,
    O

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Assuming you meant "getchar()", do you pass the End-Of-File to the console? It's Ctrl+Z for Windows and Ctrl+D for Linux
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    2
    Thanks GReaper!

    Can I ask what exactly Crtl+Z does?

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by ojhawkins View Post
    Can I ask what exactly Crtl+Z does?
    It passes a special "character" to the console, the EOF.
    Devoted my life to programming...

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Replace main() with int main(). Otherwise it's not standard and raises warnings.

    Don't use a double for anything like this, it wastes memory and is slower. No reason to make the FPU do work and eat 8 bytes of stack memory when a simple int would suffice.

    Also, gechar() isn't a function, it should be getchar().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Uppercase/Lowercase/Word COunt/Char count
    By Charak in forum C Programming
    Replies: 7
    Last Post: 02-23-2011, 08:16 AM
  2. count no of characters
    By manzoor in forum C++ Programming
    Replies: 8
    Last Post: 08-27-2008, 08:22 AM
  3. How to count characters, but not spaces?
    By RedZippo in forum C++ Programming
    Replies: 17
    Last Post: 03-29-2004, 05:09 PM
  4. [STRING][to count][characters]
    By krakz in forum C Programming
    Replies: 2
    Last Post: 02-12-2003, 04:18 AM
  5. Replies: 2
    Last Post: 05-05-2002, 01:38 PM

Tags for this Thread