0

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.

Singh
  • 229
  • 2
  • 3
  • 7
  • Regarding your first question: This is the behavior of ISO C. If you want your program to react to keypresses without pressing ENTER, you will have to use platform-specific extensions. For example, Microsoft Windows offers the functions [`_getche`](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/getche-getwche?view=vs-2019) and [`kbhit`](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/kbhit?view=vs-2019). Linux offers similar features. – Andreas Wenzel Aug 06 '20 at 05:34
  • 3
    More answers and links at [How to avoid pressing Enter with getchar() for reading a single character only?](https://stackoverflow.com/questions/1798511/how-to-avoid-pressing-enter-with-getchar-for-reading-a-single-character-only). – dxiv Aug 06 '20 at 05:37
  • 2
    The standard streams are normally buffered. So in this case getchar is reading from the stand input stream (i.e. stdin) but your input gets stored in a buffer until a new line or EOF is encountered. You can do a internet search for something like "I/O buffering in C" for more information. – Stuart Aug 06 '20 at 05:47

1 Answers1

0

There are two things that may effect when input characters are available to getchar():

  1. If the C standard library detects that standard input stream is not connected to an "interactive device" (e.g. not connected to a terminal window), it will set the stream to fully buffered mode, otherwise it will be set to line buffered mode. (The same is true for the standard output stream, but the standard error stream will not be set to fully buffered mode.) (See C11 7.21.3p3 The buffering mode of a stream can be changed using the setvbuf(), or setbuf() functions. (Some systems have additional functions, but they are usually just wrappers around `setvbuf().)

  2. If the standard input is coming from a POSIX tty, it will normally be set to canonical input mode, meaning that input is made available to be read line by line. For POSIX, the termios settings can be changed to put the terminal into noncanonical input mode where input can be read character by character. (Microsoft Windows console windows have the SetConsoleMode function for similar settings.) These functions operate below the level of C streams.

Ian Abbott
  • 15,083
  • 19
  • 33