getchar buffer size

This is a discussion on getchar buffer size within the C Programming forums, part of the General Programming Boards category; hello- i am calling getchar, and i know usually that you have to hit enter to flush the buffer and ...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    4

    getchar buffer size

    hello-

    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...
    thanks

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    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
    Posts
    307
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,497
    > is there a standard buffer size that "overflows" and forces getchar to read without a carriage return?
    No.

    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 05: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, 04:15 AM

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