I am learning C form the book, 'The C programming language'. In that when it was introducing putchar() function using the code as follows:
#include<stdio.h>
/* program to copy its input to its
output, one character at a time
using assignment inside while loop */
int main()
{
int c;
while((c=getchar()) != EOF)
putchar(c);
}
Initially I thought that if I want to input '123\n' and for this when I press key '1' then on in the terminal '11' will be visible to me, and so after pressing key '2' and '3' I thought that the terminal will show '112233' and when I finally hit 'enter' then there will be two new lines. But I was wrong and the input was printed only when I hit 'enter' or 'EOF'.
There was another problem in book, to print those lines whose length is more than 80 characters. I wrote the following code, hoping that it will work:
#include<stdio.h>
int main()
{
int c, nc=0;
while((c=getchar())!=EOF)
{
++nc;
if(c=='\n')
{
if(nc > 80)
putchar(c);
nc=0;
}
}
}
But this is not working.
In the first code when I hit 'enter' then that prints the complete line, in the second code I wanted to count the characters and when I hit 'enter' my code checks whether the character count is more than 80 or not, and if more than 80 then I have done 'putchar(c)'. Why this is not working?
Please be clear that I am not asking for the solution of the problem in book. I am confused by the output of 'putchar()', kindly clear my doubt.