-
Compiler Problem ?
I am fairly new to C programming (and computer programming)
I am typing examples from "The C Programming Language" book but after i compile in Visual studio and run the programs, i dont get the results that the author explains . To be more specific, i run small word and character count programs like..
main()
{
long nc;
nc=0;
while (getchar() != EOF)
--nc;
printf("%ld\n", nc);
}
and after typing a few words and pressing ENTER nothing
happens except that i goto a new line. is this normal, or should
i get a character count ?
-
EOF is only generated at the End Of the File. therefore pressing enter doesn't do that. If you on Windows, try entering your number followed by CTRL+Z, then enter. That should generate EOF.
-
Sorry for not being clear, but getting out is not the problem. the book says after running this in a Dos window, and type a few words or letters or whatever and press enter, you should get a character count or something. i dont gat anything though. if you complie and run this, you should get a count of some sort.
-
It appears that the CTRL+Z method of generating EOF is messing around. Here's a couple of things to do:
- Redirect input into the program from a file instead of the keyboard. EG c:\prog1 <sometextfile.txt
- Change the program to have '\n' instead of EOF. This will stop the loop when you press enter.
-
Ok.. nevr mind.. I got it :) Thanx !