2

Possible Duplicate:
Why doesn’t getchar() recognise return as EOF in windows console?

I'm trying to print out the value in the variable 'nc' in the next program:

int main()

{ 
 long nc;
 nc = 0;

 while (getchar() != EOF)
 ++nc;
 printf("%ld\n", nc); 
}

Please tell me why is it not printing?

Andrew Brēza
  • 7,705
  • 3
  • 34
  • 40
MNY
  • 1,506
  • 5
  • 21
  • 34

5 Answers5

5

You don't have brackets in your while loop (this is why not using brackets leads to error prone software). Therefore, the value is getting incremented, but not printing.

Try:

int main(int argc, char** argv)
{ 
    long nc;
    nc = 0;

    while (getchar() != EOF)
    { // ADD THIS
        ++nc;
        printf("%ld\n", nc); 
    } // AND THIS
}

otherwise, your code is essentially doing:

int main(int argc, char** argv)
{ 
    long nc;
    nc = 0;

    while (getchar() != EOF)
    {
        ++nc; // ENDLESSLY ADDING
    }
    printf("%ld\n", nc); // NEVER REACHED DUE TO WHILE LOOP.
}
Inisheer
  • 20,376
  • 9
  • 50
  • 82
  • too bad there is no feature that you can add certain people that help you in the past to future question :) @Inisheer – MNY Jan 11 '13 at 21:30
  • @user1959174 Maybe, but I could certainly see that getting out of hand as well :P . – Inisheer Jan 11 '13 at 21:31
  • hah yes i know, this is probably the reason they didnt do it in the first place. I'm sure they though of that @Inisheer . tell me buddy, I just recently started to learn programming and took C as the first language cause people recomanded me that if I want to learn Objective -C, it will be good o have a solid understanding of C. I started couple of days ago with the book "the c programming language" (k&r), do you think its a good book for programming beginners? – MNY Jan 11 '13 at 21:34
  • @user1959174 See here for good recommendations: http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list?rq=1 – Inisheer Jan 12 '13 at 14:29
3

Your while loop will continue to loop until you end the input using Control-D on Unix or Control-Z, Return on Windows. It will do this without printing anything because you did not use braces around the ++nc and printf.

You may also have problems with printf if you did not #include <stdio.h> at the top of your program. If the compiler does not know that printf is a varargs function, it will not format the argument list correctly when calling it.

Zan Lynx
  • 53,022
  • 10
  • 79
  • 131
3

after entering some inputs like

1 2 4 u must type ctrl + D since its the EOF ASCII equivalent.

Else modify the progeam and put

while(getchar()!='\r') (until you hit Enter)

Sagar Sakre
  • 2,336
  • 22
  • 31
1

What do you mean? It works:

./a.out 
asdfsdfasdfasdfasddddddddddddddddddddddd
41

echo "Try to count this" | ./a.out 
18
Eddy_Em
  • 864
  • 6
  • 14
1

you have to stop reading characters from stdin by stopping the while loop of getchar

and then you will see the nc value printed

To do

EOF = CTRL + D (for Linux)

EOF = CTRL + Z (for Windows)

MOHAMED
  • 41,599
  • 58
  • 163
  • 268