Begginer C Question About getchar()

This is a discussion on Begginer C Question About getchar() within the C Programming forums, part of the General Programming Boards category; As a preface, I have done my best to read through the FAQs on this forum so if I am ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    7

    Question Begginer C Question About getchar()

    As a preface, I have done my best to read through the FAQs on this forum so if I am violating the forum rules it's not out of negligence.

    That said, I have a question regarding how the getchar() function in the stdio library works. For contextual purposes, I am writing a simple program that takes input from the terminal (stdin) and stores the inputted characters into a consolidated C string.

    Code:
      1 #include <stdio.h>
      2 
      3 #define MAX_LENGTH 10
      4 
      5 int readbuff(char[]);
      6 
      7 main()
      8 {
      9     char buff[MAX_LENGTH];
     10 
     11     readbuff(buff);
     12     printf("%s", buff);
     13 
     14     return 0;
     15 }
     16 
     17 /* read buffer and return the number of characters input */
     18 int readbuff(char buff[])
     19 {
     20     int c, i;
     21 
     22     for(i = 0; i < MAX_LENGTH - 1 && (c = getchar()) != EOF && c != '\n'; ++i)
     23         buff[i] = c;
     24     if(c == '\n')
     25         buff[i++] = c;
     26     buff[i++] = '\0';
     27 
     28     return i;
     29 }
    In short, my question is what exactly happens when the user presses the Enter key? I am wondering if the reaction is relative to the OS, or if there is some standard signal that is sent when this key is pressed. I am aware that the newline character, '\n', is appended to the end of the current string of characters, but why does it also cause the program to start executing the getchar() function inside of the for loop?

    I am running Ubuntu 11.04 with the BASH if that makes a difference.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    User input is line-buffered (this is what allows you, for instance, to backspace over characters to correct mistakes). Once you press enter the whole line is placed the input buffer for your program to process. (And the flip side: if there's still input in the buffer that hasn't been processed, further calls for getchar() or other input functions won't "block" waiting for more input, but use what's still in the buffer.)

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    7
    Just to be certain that I fully understand this concept:
    When you say that "User input is line-buffered", do you mean that that the shell is waiting for the newline character to appear before it moves the user-inputted characters into the actual input buffer, and it is from there that the getchar() function begins to read the input? Also, when you say "the input buffer" are you referring to stdin?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Yes, and yes.

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    7
    I guess my last question is whether or not it is standard for input at the shell level the be line-buffered?

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,667
    The standard pertains to the language, not anything else: if you try to put C on a system without files and expect I/O operations to work, you have to emulate the file concept to a sufficient degree.
    Last edited by whiteflags; 01-03-2011 at 02:31 PM.

  7. #7
    Registered User
    Join Date
    Jan 2011
    Posts
    7
    Thanks for all of your replies!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Design layer question
    By mdoland in forum C# Programming
    Replies: 0
    Last Post: 10-19-2007, 04:22 AM
  2. noob question
    By gillypie in forum C Programming
    Replies: 21
    Last Post: 09-04-2007, 07:49 AM
  3. getchar() problem
    By jlharrison in forum C Programming
    Replies: 6
    Last Post: 01-25-2006, 01:49 PM
  4. getchar buffer size
    By oncemyway in forum C Programming
    Replies: 3
    Last Post: 08-02-2005, 12:49 AM
  5. getchar() problem from K&R book
    By anemicrose in forum C Programming
    Replies: 13
    Last Post: 04-04-2004, 11:06 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21