Results 1 to 4 of 4
Suppose I compile the following simple program to count the number of chars in an input:
#include <stdio.h>
main()
{
long nc;
nc = 0;
for (nc = 0; getchar() ...
- 02-18-2008 #1Just Joined!
- Join Date
- Feb 2008
- Posts
- 4
bash/C issue
Suppose I compile the following simple program to count the number of chars in an input:
#include <stdio.h>
main()
{
long nc;
nc = 0;
for (nc = 0; getchar() != EOF; ++nc)
;
printf("%1d\n",nc);
}
Now, when I run this program from bash (with ./a.out), I can input, but for some reason the output never comes. The output does come, however, when I put the printf statement inside the loop. Does anyone know how to resolve this issue?
Thanks!
- 02-18-2008 #2
Try ending your program with Ctrl-D, which is EOF
- 02-18-2008 #3Just Joined!
- Join Date
- Feb 2008
- Posts
- 4
thanks, it works
- 02-18-2008 #4
That's to say when your program is running terminate it by typing Ctrl-d, this will put EOF in the input stream so when getchar retrieves it getchar() == EOF and your program will end


Reply With Quote