Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. getchar / putchar - how does it really works ?

getchar / putchar - how does it really works ?

Scheduled Pinned Locked Moved C / C++ / MFC
question
11 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Vaclav_

    My task is to input individual characters , terminate the input with "space" or "ESC" and then retrieve / save the characters entered. getchar and its variety waits in loop (?) and outputs to stdout until "enter" key is pressed. putchar outputs ( to stdout) EACH character entered in do { ...} while loop - runs the loop. Questions 1. Is there a way to terminate getchar other than entering "enter "? 2. How does putchar "runs" in do { .. } while loop ignoring getchar? 3. How can I "retrieve" characters in WHAT buffer for additional processing AFTER the do{} while loop is terminated ?

    char next;
    do
    {
    next = getchar(); // outputs each character as entered 
    putchar(next);    // outputs characters in loop 
    }
    while (next != '\\n'); // terminates with enter key press / new line /n
    
    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #2

    Vaclav_ wrote:

    getchar and its variety waits in loop (?) and outputs to stdout until "enter" key is pressed.

    No, getchar merely reads the next character entered at the terminal. If the terminal is in (normal) echo mode then it displays the character as typed. The putchar function merely displays a character, it has no knowledge of the loop or anything else. 1. Yes, you can terminate on anything, just inspect the character as entered and compare it to your terminator. 2. As above, putchar does nothing beyond displaying a single character. 3. Save them somewhere.

    V 1 Reply Last reply
    0
    • L Lost User

      Vaclav_ wrote:

      getchar and its variety waits in loop (?) and outputs to stdout until "enter" key is pressed.

      No, getchar merely reads the next character entered at the terminal. If the terminal is in (normal) echo mode then it displays the character as typed. The putchar function merely displays a character, it has no knowledge of the loop or anything else. 1. Yes, you can terminate on anything, just inspect the character as entered and compare it to your terminator. 2. As above, putchar does nothing beyond displaying a single character. 3. Save them somewhere.

      V Offline
      V Offline
      Vaclav_
      wrote on last edited by
      #3

      I am seeing this

      char next;
      do
      {
      next = getchar(); // outputs each character as entered
      cout << "text entered " << endl;
      putchar(next);    // outputs characters in loop
      }
      while (next != '\\n'); // terminates with enter key press / new line /n
      

      qwerty
      text entered
      qtext entered
      wtext entered
      etext entered
      rtext entered
      ttext entered
      ytext entered

      K L 2 Replies Last reply
      0
      • V Vaclav_

        I am seeing this

        char next;
        do
        {
        next = getchar(); // outputs each character as entered
        cout << "text entered " << endl;
        putchar(next);    // outputs characters in loop
        }
        while (next != '\\n'); // terminates with enter key press / new line /n
        

        qwerty
        text entered
        qtext entered
        wtext entered
        etext entered
        rtext entered
        ttext entered
        ytext entered

        K Offline
        K Offline
        k5054
        wrote on last edited by
        #4

        you need to turn off canonical mode on input. man termios, and look for ICANON. As a quick test without recompiling:

        $ myprog
        qwerty
        text entered
        qtext entered
        wtext entered
        etext entered
        rtext entered
        ttext entered
        ytext entered

        $ stty -icanon
        $ myprog
        qtext entered
        qwtext entered
        wetext entered
        ertext entered
        rttext entered
        tytext entered
        y
        text entered

        $ stty icanon

        See: [FAQ - Cprogramming.com](https://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1045691686&id=1043284392)

        1 Reply Last reply
        0
        • V Vaclav_

          I am seeing this

          char next;
          do
          {
          next = getchar(); // outputs each character as entered
          cout << "text entered " << endl;
          putchar(next);    // outputs characters in loop
          }
          while (next != '\\n'); // terminates with enter key press / new line /n
          

          qwerty
          text entered
          qtext entered
          wtext entered
          etext entered
          rtext entered
          ttext entered
          ytext entered

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #5

          It seems that reading characters one at a time is no longer possible (on Windows). While getchar returns the characters one at a time, the system does not present them until after the enter key was pressed. So although you are trying to read single characters, they are still streamed internally. [edit] Same result on Ubuntu. [/edit]

          L S 2 Replies Last reply
          0
          • L Lost User

            It seems that reading characters one at a time is no longer possible (on Windows). While getchar returns the characters one at a time, the system does not present them until after the enter key was pressed. So although you are trying to read single characters, they are still streamed internally. [edit] Same result on Ubuntu. [/edit]

            L Offline
            L Offline
            leon de boer
            wrote on last edited by
            #6

            It has nothing to do with windows :-) Whoever writes the C/C++ library determines how it works, you are making a C/C++ standard call not a windows call and if they don't do the standard then it is hardly a fault of windows. On windows really the easy way is to hook the WM_CHAR and/or WM_SYSCHAR messages because the message queue is available even in console apps since windows XP service pack 2.

            In vino veritas

            L 1 Reply Last reply
            0
            • L leon de boer

              It has nothing to do with windows :-) Whoever writes the C/C++ library determines how it works, you are making a C/C++ standard call not a windows call and if they don't do the standard then it is hardly a fault of windows. On windows really the easy way is to hook the WM_CHAR and/or WM_SYSCHAR messages because the message queue is available even in console apps since windows XP service pack 2.

              In vino veritas

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #7

              I know that, I was merely pointing out its behaviour on Windows as opposed to Linux.

              1 Reply Last reply
              0
              • L Lost User

                It seems that reading characters one at a time is no longer possible (on Windows). While getchar returns the characters one at a time, the system does not present them until after the enter key was pressed. So although you are trying to read single characters, they are still streamed internally. [edit] Same result on Ubuntu. [/edit]

                S Offline
                S Offline
                Stefan_Lang
                wrote on last edited by
                #8

                Richard MacCutchan wrote:

                the system does not present them until after the enter key was pressed.

                Not quite true, or at least not complete: if there is something in the input buffer already, getchar will immediately return with the next char it finds in that buffer. It will only wait if the input buffer is empty. AFAIK this is not dependend on the OS.

                GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

                L L 2 Replies Last reply
                0
                • S Stefan_Lang

                  Richard MacCutchan wrote:

                  the system does not present them until after the enter key was pressed.

                  Not quite true, or at least not complete: if there is something in the input buffer already, getchar will immediately return with the next char it finds in that buffer. It will only wait if the input buffer is empty. AFAIK this is not dependend on the OS.

                  GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #9

                  Well that is exactly what happened in my tests, none of the characters were retrieved until the enter key was pressed.

                  1 Reply Last reply
                  0
                  • S Stefan_Lang

                    Richard MacCutchan wrote:

                    the system does not present them until after the enter key was pressed.

                    Not quite true, or at least not complete: if there is something in the input buffer already, getchar will immediately return with the next char it finds in that buffer. It will only wait if the input buffer is empty. AFAIK this is not dependend on the OS.

                    GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

                    L Offline
                    L Offline
                    leon de boer
                    wrote on last edited by
                    #10

                    No it doesn't always it depends on the canonical mode of your input, this was covered above. The C standard doesn't cover how the underlying system is operating and it cant present what it doesn't yet have. Richard simply has an OS that is in canonical mode.

                    In vino veritas

                    S 1 Reply Last reply
                    0
                    • L leon de boer

                      No it doesn't always it depends on the canonical mode of your input, this was covered above. The C standard doesn't cover how the underlying system is operating and it cant present what it doesn't yet have. Richard simply has an OS that is in canonical mode.

                      In vino veritas

                      S Offline
                      S Offline
                      Stefan_Lang
                      wrote on last edited by
                      #11

                      Well it always worked as I described for me. But I admit I haven't heard of the canonical mode before, so I'm going to trust you on this.

                      GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups