Thread: getchar buffer size

  1. #1
    Registered User
    Join Date
    Jul 2005

    getchar buffer size


    i am calling getchar, and i know usually that you have to hit enter to flush the buffer and read the data with getchar. however, i see that if i input 2k characters interactively, getchar will read the data without a return. also, if i use a perl script as input, it seems i have to generate about 4k worth of characters before getchar will read it.

    is there a standard buffer size that "overflows" and forces getchar to read without a carriage return? and why would i get different results between interactive and perl script?

    just wondering...

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Oregon, USA
    I believe stdio's internal buffer size is defined as BUFSIZ (8192 bytes on my system.) I could be wrong, but I'm pretty sure that's what that macro is for.

    EDIT: Now that I think about it, I don't think stdio's buffer size has as much as to do with it as the terminal's buffer size does. I don't think the stdio routines even see any of the data until the terminal passes it on (usually after pressing ENTER in buffered mode.)
    If you understand what you're doing, you're not learning anything.

  3. #3
    Join Date
    Nov 2003
    You can set the tty characteristics on unix so that the tty returns single chars without a return required. You can also do this in console mode apps in windows.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > is there a standard buffer size that "overflows" and forces getchar to read without a carriage return?

    You can try messing with setvbuf() before you call any other stdio function, but that only solves the internal 'C' runtime library buffering. Data may still be buffered by the OS before it even gets anywhere near your code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stdin buffer size
    By Kempelen in forum C Programming
    Replies: 6
    Last Post: 03-16-2009, 03:06 PM
  2. Adventures in labyrinth generation.
    By guesst in forum Game Programming
    Replies: 8
    Last Post: 10-12-2008, 01:30 PM
  3. Generic heapsort
    By Sephiroth1109 in forum C Programming
    Replies: 15
    Last Post: 12-07-2007, 06:14 PM
  4. Error with a vector
    By Tropicalia in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2006, 07:45 PM
  5. Console Screen Buffer
    By GaPe in forum Windows Programming
    Replies: 0
    Last Post: 02-06-2003, 05:15 AM