problem with character counting program

This is a discussion on problem with character counting program within the C Programming forums, part of the General Programming Boards category; 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 ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,138
    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, 04:12 PM
  4. Character Counting
    By glider_pilot123 in forum C Programming
    Replies: 6
    Last Post: 11-15-2004, 04:57 PM
  5. Character counting program
    By TankCDR in forum C++ Programming
    Replies: 5
    Last Post: 04-05-2002, 09:01 PM

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