Thread: problem with character counting program

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    14

    problem with character counting program

    Code:
    //counting no:characters
    #include <stdio.h>
    main()
    {
    int c;
    for(c=0;getchar()!=EOF;c++)
    ;
    printf("%d",c);
    }
    my problem is when i compile it in my gcc compiler(gcc count.c -o count)
    the program keeps on taking characters as input. thats all. does nothing. havn't even printing any number........i don't understand whats wrong with this

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You need to press CTRL-D (Unix/Linux/MacOS) or CTRL-Z (DOS/Windows) to signal an EOF.
    This will cause the loop to exit, and print whatever is in c.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Plus you need to remove the semicolon after your for loop
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    14
    guys thank u very much.im just beginner....these may seem silly to u .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a character counting program!
    By Redpred in forum C Programming
    Replies: 9
    Last Post: 08-09-2006, 08:15 AM
  2. Character counting
    By Jas11 in forum C++ Programming
    Replies: 3
    Last Post: 04-21-2005, 02:55 AM
  3. Problem with character counting program...
    By MyglyMP2 in forum C++ Programming
    Replies: 9
    Last Post: 03-25-2005, 05:12 PM
  4. Character Counting
    By glider_pilot123 in forum C Programming
    Replies: 6
    Last Post: 11-15-2004, 05:57 PM
  5. Character counting program
    By TankCDR in forum C++ Programming
    Replies: 5
    Last Post: 04-05-2002, 10:01 PM