What does getchar return if it reads the "enter"...?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rahul sinha
    New Member
    • Jan 2011
    • 17

    #16
    hey people, i wanted to ask,if i type a line and then press enter,how come getchar() is still able to read all the characters of a line..if it only reads a character after pressing enter....It appears as if its reading line by line ,not character by character....pl ease help me out with this confusion....

    Comment

    • horace1
      Recognized Expert Top Contributor
      • Nov 2006
      • 1510

      #17
      Text streams such as stdin operate on a line by line basis with characters entered being placed in an input buffer until newline (or end of file) is entered. Then successive calls to getchar() read characters until the input buffer is exhausted (last character is newline or EOF).
      Some C systems have functions which will read individual character by character directly from the keyboard, e.g. function getch() and getche() in <conio.h> in some Windows based compilers. Their use is not recommended as it is non standard and code is not protable.

      Comment

      • rahul sinha
        New Member
        • Jan 2011
        • 17

        #18
        what if we suffix the input string with ctrl+z on a windows os(an EOF character).and then press enter....in that case getchar() should read eof as well and comeout of the loop no...

        Comment

        • horace1
          Recognized Expert Top Contributor
          • Nov 2006
          • 1510

          #19
          Try the following fragment of code
          Code:
          int main()
          {
            int ch=0;
            while(ch != EOF)
            {
             ch=getchar();
             printf(" char %c %d",ch, ch);
            }
          }
          }
          if you enter a line of test is displays the character and the character code.
          To enter EOF you enter CTRL/Z then RETURN key on a line by itself and it will break out of the loop. Using CodeBlock under Windows if I type characters then CTRL/Z then RETURN it passes the CTRL/Z as character code 26 decimal not EOF (which is -1 with this compiler)

          Comment

          • rahul sinha
            New Member
            • Jan 2011
            • 17

            #20
            ok,that means with characters ctrl+z has ascii value 26 and when
            provided alone getchar returns unsigned form of -1...(on this
            compiler)...but enter is also there in the buffer then why does it not
            print enter's ascii value which is 10.....

            Comment

            • horace1
              Recognized Expert Top Contributor
              • Nov 2006
              • 1510

              #21
              the ASCII character code 10 decimal is displayed for RETURN key, e.g. if I type
              abc<return>
              abc<CTRL/Z><RETURN>
              <CTRL/Z><RETURN>
              I get
              Code:
               char a 97 char b 98 char c 99 char 
               10 char a 97 char b 98 char c 99 char  26 char ΓΏ -1
              you can see the character 10 ecimal at the end of the first line. Entering <CTRL/Z> at the end of a line of text displays character code 26 decimal, and EOF appears as -1 decimal.

              Comment

              • donbock
                Recognized Expert Top Contributor
                • Mar 2008
                • 2427

                #22
                You shouldn't count on the ENTER key being encoded as ASCII character 10. Instead, test for the newline character: '\n'. Using the newline character will automatically account for environments with differing end-of-line codes.

                Comment

                Working...