I have been stuck trying to understand why triggering eof using ctrl-D adds to a counter in a for loop.
Here is my code:
#include <stdio.h>
int main()
{
double nc;
for (nc = 0; getchar() != EOF; nc++){
getchar();
}
printf("%.0f\n", nc);
return 0;
}
My outcome is :
0
1
2
3
4
5
6
7
8
The 8 is what's given to me when I use ctrl-D after inputting 7. Is there a reason why triggering the eof causes the code to run another complete loop?
I thought an empty buffer will return nothing.
Here is something you might miss. Take piece of your code:
for (nc = 0; getchar() != EOF; nc++)
getchar();
There are two getchar(). Let's call them, getchar1() and getchar2(). Your input should be like this:
0\n
.
.
.
7\n
EOF
getchar1() catch the sequence of digits and EOF. getchar2() always catch the newline ('\n'). And the count of you go through for loop body is 8 (0 to 7).
Hope it helpful for you.
Related
I am trying to write a program for counting the number of characters in C. Below is my program:
#include <stdio.h>
int main(void){
// long nc;
// for(nc = 0; getchar() != EOF; nc++);
// printf("%ld\n", nc);
long nc;
nc = 0;
while(getchar() != EOF){
++nc;
}
printf("%ld\n", nc);
return 0;
}
When I execute the above program using the input :-
123<Enter>
then I press the control + ^d on my Mac to represent EOF, I am getting the output as 4D instead of just 4. Can anyone please tell me why I am getting D in my output?
[Turning my comment into an answer]
The "problem" is that the terminal program itself writes the output ^D as response to the Ctrl-D.
With the original output of your program (without the extra leading newline) the program writes its output 4 over the ^ written by the terminal. The (trailing) newline from the program then makes the terminal go to the next line where the shell takes over and writes it prompt.
This will make it seem like the output of your program is 4D.
As a possible solution, you might want to check the settings of your terminal program to see if its own output could be disabled.
I found this book have many people suggest for newbie, but some code on it doesn't work, although I code it exactly like the code in the book but it still don't work
#include <stdio.h>
main()
{
int a ;
for (a = 0; getchar() != EOF ; ++a);
printf ("%d",a);
}
It looks like after the loop it ends immediately, code after the loop is not executed.
Is this book is too old? Is there any another book for self learning c programming?
int main( void )
{
int a ;
for (a = 0; getchar() != EOF ; ++a);
printf ("%d\n",a);
return 0;
}
On Unix platform run this code and when you want to exit introduce EOF by ctrl+d, if you are on windows then EOF is introduced by ctrl+z
So basically when you exit you will get the count of number of times your loop ran.
If you want to print out each input then you need to get rid of the ; at the end of for loop
int main( void )
{
int a ;
for (a = 0; getchar() != EOF ; ++a)
printf ("%d\n",a);
return 0;
}
I strongly suspect that the console closes immedeately after the loop ends. Try to insert something like system("pause") to prevent the console from closing.
The loop loops until you type EOF and prints the number of characters typed so far. To type EOF, you have to hit ctrl-z and return (in a test I had to do this after a return, so return, then ctrl-z, then return). If the console closes directly after the ctrl-z, you can add this system("pause") to wait for another key afterwards, so you see the output.
I am new to C, and to Ubuntu. I wrote a very simple program to count the number of characters using while and getchar(). The program is:
#include <stdio.h>
main() {
int i;
int c= 0;
while ( ( i = getchar() ) != EOF ){
c++ ;
}
printf( "%d characters\n" , c) ;
return 0;
}
I saved it and compiled it using gcc c1.c -o c1. No errors reported. I executed the program using ./c1 . I give the input as daniweb then I press enter, but the count is displayed. What went wrong? Is it infinite loop? How does getchar() determine EOF when input is given from keyboard?
On the terminal you can send EOF to an application by pressing Ctrl+D. You can also do something like this:
echo "blablub" | ./yourprogram
To count how many characters are in blablub. In this case EOF is sent automatically.
Pressing enter sends a new line character to your program, not EOF. As others have mention already, use Ctrl+D to send EOF. If you want to stop reading characters on newline, change your while loop to this:
while ( ( i = getchar() ) != '\n' ){
c++ ;
}
#include <stdio.h>
// copy input to output
// my version
int main()
{
int c;
printf("\n\nUse CONTROL + D to terminate this program\n\n");
while ((c = getchar()) != EOF) {
putchar(c);
}
if ((c = getchar()) == EOF) {
printf("\n\nProgram TERMINATED\n\n");
}
return 0;
}
When I enter control + D, the body of the if statement runs. That's what I had wanted, but as I analyzed the code more thoroughly, shouldn't it ask for my input again since the if's condition is (c = getchar()) == EOF?
When you hit ^D, input to the program is closed, so getchar() will subsequently always return EOF.
Control-D is canonical mode end-of-file character. When entered at the beginning of a line it causes an EOF condition to be seen by the process, that is the read returns 0. However if if Control-D is entered somewhere other than the beginning of the line it just causes the read to return immediately with what has been input thus far.
If you hit Control-D twice in a row you should see what I think you asking about.
EDIT
Here is a pretty good explanation.
^D terminates the program instantly. Thus you're getchar would never return when ^D is hit.
That is why REPL like python exits using 'exit()'.
If you want, try to use 'q' for quiting:
I wrote the most innocuous C program but I can't get the expected result. I hope you can tell where my error is.
This is the code:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv) {
int c, var4;
double var1,var2,var3;
while ((c = getchar()) != EOF) {
while (c != ':') {
putchar(c);
c = getchar();
}
//scanf("%d/%d/%d",&mm,&dd,&yy);
//scanf("%lf%lf%lf%d",&var1,&var2,&var3,&var4);
}
return 0;
}
and I'm using this file for input(command line redirection)
Name1 - Code1:
04/03/2011 4.5 5.6 9.8 145
04/03/2011 6.5 4.6 9.9 185
Name2 - Code2:
05/03/2011 4.5 5.6 9.8 135
05/03/2011 6.5 4.6 9.9 165
The error appears during while loop (I tried printf instead of putchar and it prints -1 endlessly and seems to never reach EOF)
I thinks that's all, I thank your help in advance.
You are getting one character, then going into the inner loop — which checks for ':', but not for EOF. So, unless the file ends with : (so that it will be seen by the outer loop), the inner loop will spin forever when it hits EOF.
The inner loop doesn't stop at EOF.
You're getting an error from getchar (EOF == -1) because thee file is finished. That's what it's printing endlessly.
You are not checking for EOF inside your inner getchar loop, therefore allowing it to continuously read EOF (-1) and printing that out.
I don't know what your expected result is so I cannot help you there.