Menu

getchar() confusion

Help
2018-09-11
2018-09-11
  • Chris Chadwick

    Chris Chadwick - 2018-09-11

    Hi

    I haven't done any C coding in many years, so am reading through my Kernighan & Ritchie book again, to fully re-aquaint myself with it. One exercise is to write a program using getchar() to get keyboard input, and print it to the screen/stdout, replacing all tab, space and backspace characters with their escape sequence representations. I can't seem to get the following code to behave as expected.

    Firstly, getchar() seems to be echoing input to the screen without me specifically outputting it with putchar() or printf().

    The escape sequence representations don't display until I press ENTER.

    The test for backspace doesn't seem to work at all.

    I have mingw-w64 installed on win7. The above behaviour has got me scratching my head, and I'd really like to understand what's happening, before I move on. Any help greatly appreciated, and appologies in advance if I've made a basic mistake or assumption.

    Thanks in advance,
    Chris

    #include <stdio.h>
    
    int main()
    {
        int c;
    
        while((c = getchar()) != EOF)
        {
            if (c == '\t')
                printf("\\t");
            else if (c == '\b')
                printf("\\b");
            else if (c == '\\')
                printf("\\\\");
            else
                putchar(c);
        }
    
        return 0;
    }
    
     
    • Kai Tietz

      Kai Tietz - 2018-09-11

      Hello,

      \o/ finally after 10 years someone found the bug in msvcrt's getchar()
      implementation. We are all in your debt for this ....

      Sorry for kidding here a bit, but of some interesting might be
      tutorial about use of getchar(). See for example
      https://www.tutorialspoint.com/c_standard_library/c_function_getchar.htm
      with sample.

      Hopr this link will help you to answer your question,
      Kai

      Am Di., 11. Sep. 2018 um 18:10 Uhr schrieb Chris Chadwick
      big10p@users.sourceforge.net:

      Hi

      I haven't done any C coding in many years, so am reading through my Kernighan & Ritchie book again, to fully re-aquaint myself with it. One exercise is to write a program using getchar() to get keyboard input, and print it to the screen/stdout, replacing all tab, space and backspace characters with their escape sequence representations. I can't seem to get the following code to behave as expected.

      Firstly, getchar() seems to be echoing input to the screen without me specifically outputting it with putchar() or printf().

      The escape sequence representations don't display until I press ENTER.

      The test for backspace doesn't seem to work at all.

      I have mingw-w64 installed on win7. The above behaviour has got me scratching my head, and I'd really like to understand what's happening, before I move on. Any help greatly appreciated, and appologies in advance if I've made a basic mistake or assumption.

      Thanks in advance,
      Chris

      include <stdio.h></stdio.h>

      int main()
      {
      int c;

      while((c = getchar()) != EOF)
      {
          if (c == '\t')
              printf("\\t");
          else if (c == '\b')
              printf("\\b");
          else if (c == '\\')
              printf("\\\\");
          else
              putchar(c);
      }
      
      return 0;
      

      }


      getchar() confusion


      Sent from sourceforge.net because you indicated interest in https://sourceforge.net/p/mingw-w64/discussion/723798/

      To unsubscribe from further messages, please visit https://sourceforge.net/auth/subscriptions/

       
      • Chris Chadwick

        Chris Chadwick - 2018-09-11

        Thanks for your reply, Kai. I had already seen the page you linked to, when I was looking for an answer. It doesn't mention anything about echoing input to the output, though. I think I've just misunderstood how getchar() operates. I thought it was purely an input function, with putchar() being the output equivalent.

        Still wondering why backspace doesn't seem to be recognized by the code, though. Any idea?

        Thanks
        Chris

         
  • Doug Semler

    Doug Semler - 2018-09-11

    I think you want the non standard conio _getch which is unbuffered and no echo (_getche echos). getchar I think buffers until it sees the cr/lf. Meaning that the backspaces are buffered out.

    Note that in a while loop you'll have to do your own processing for EOF because those functions don't return an error code. (e.g. when you see the character ctrl-z, you should quit break the loop, ctrl-c should quit the program).

     
    • Chris Chadwick

      Chris Chadwick - 2018-09-11

      Thanks, Doug - that makes sense. I'll check out those functions in conio.

      Cheers
      Chris

       

Log in to post a comment.