r/C_Programming • u/8d8n4mbo28026ulk • 22d ago
We can do better than line-buffered output
https://nullprogram.com/blog/2026/11/05/
52
Upvotes
r/C_Programming • u/8d8n4mbo28026ulk • 22d ago
22
u/aioeu 21d ago edited 21d ago
I think glibc's literal interpretation of the standard is right here. The C standard does not say that a line buffered output stream is flushed on some newline characters, but not flushed on others. It says that it is intended to be flushed "when a new-line character is encountered". That has to be the case even if it isn't the final newline in the buffer.
A stream may be connected to something where the
writesyscall actually does something significant. For instance, it might be sending the data over some kind of datagram protocol; the programmer should be able to rely on the automatic buffer flushes generating separate datagrams.30
u/kinchkun 21d ago
Yeah, thanks, i think flushing on newline is fine.
5
u/MarekKnapek 21d ago
I did something similar for our log file output. Before my change, there was a ton of small writes. Each opening a file, writing to it, closing it. After my change, there was an append to a shared buffer and a thread notification. On first write, the log output would go straight to the disk, just as before. Any other log outputs would wait in queue for 5 seconds or for 5 MB, whichever was filled first. Massive speedup for our app.
21
u/trejj 21d ago
Adding nondeterministic timing specific behavior to one of the most mundane things - text output printing.. what could go wrong, when on fast machines you get behavior X, and on slow machines you get behavior Y.
Run in a VM, you get behavior X, run under contention, you get behavior Y.
That would be a hard pass for me.
-1
u/LordRybec 21d ago
I think line buffering is a good default. It should operate by consistent rules, like always flush on a newline. I don't think random people online having other use cases that need something else justifies changing the default.
That said, I'm disappointed that changing this behavior is a huge pain! I started out as a video game programmer in QBasic, where you could read input in line buffered chunks or as individual keycode inputs. For turn based games, line buffering was the perfect tool, and when I wanted to write real-time games, reading keycode input directly was ideal. For line buffered input you'd do something like this:
The first argument is the prompt and the second is the variable to store the input. This blocks until a newline. From there you compare and do whatever it is the user indicated.
For keycode input you'd do something like this:
INKEY$ returns the keycode, and now you can compare R$ to find out what key the user pressed. This does not block. If the user isn't pressing a key when it runs, R$ = "", so a common pattern would be:
You would only use this though, if you wanted to block. For a real-time game I would just read INKEY$ at the end of the loop, use
LOOP UNTILto check if it isCHR$(27), to bail out and end the program when ther user pressed Escape, and then the loop starts over, and keyboard input handling starts at the beginning of the loop.This was very straightforward. I could use whichever input scheme I wanted without jumping through any hoops. I could even mix them in the same program without any side effects. Of course, there's still no non-blocking buffered input option. If you wanted non-blocking buffered input, you would have to read INKEY$ and add characters to a buffer manually as the user typed them.
---
Unfortunately in C it's a huge pain to do anything but buffered line input. Sure, as the article says, you can create new pipes, which don't block until newlines by default, and honestly, pipes aren't that difficult. Most resources trying to teach them stink, which makes them seem a lot harder than they are, but if you can find Beej's IPC guide, it teaches pipes extremely well. (In fact, I used it as the textbook for an undergrad "Concurrency and Parallelism" course. It's free, and it's better quality than any other textbook on the topic I've seen.) But this doesn't work if you want user input. Pipes are awesome for certain types of interprocess communication, but you can't inject input into them from the keyboard (outside of stdin, through the console).
There are ways of changing stdin behavior, but they are OS dependent and a pain in every OS. I use SDL2 now days when I need unbuffered real-time input. It's a game library, and when I need this, I'm usually writing a game, so it works really well. It also presents input as an event buffer, which is especially good for games and real-time applications in general. I've done some game and real-time non-game dev in Python as well, and I use Pygame in the same way. But what if I don't want a graphical application? Ncurses can do it. There's a series of functions you can call to put it into an unbuffered mode where individual input characters can be read in real-time, and there's even a function that allows arrow keys and other non-character inputs to be captured. It's kind of a pain to learn though, and portability isn't 100%.
Here's the thing: I understand that C is an old language that originated back when line input was generally the only input. It was designed with terminals in mind, and terminals themselves were often line buffered, so aligning the language to the hardware was efficient and made sense. It's clear that by the late 1980s, when QBasic started to be a thing, language designers recognized that computing was rapidly heading toward user interfaces that were not constrained to line input and needed to be able to accept input directly and immediately, rather than waiting for a specific character. But then we seem to have lost that. Visual Basic dropped INKEY$ entirely, reverting back to just line input. There's a .NET function you can call to get input like this for console applications, but that's like using SDL2 in C, not like the language itself providing that capability. Even Python doesn't have this capability built in. Sure, you can do it with Tkinter, but you have to create a window instance for the events to go to. None of this is using stdin. Your only options for that are complex OS level manipulation of stdin or ncurses which has limited portability.
This functionality should be built into the language. INKEY$ was introduced in GW-BASIC in 1983, and then it was inherited into QBasic in 1991. It was clearly already known that this was a valuable feature long before C89. Functionality either for changing input modes or for capturing input in different ways, changing modes behind the scenes as needed, should have been added in C89. And yet, it's 2026, and we still don't have it.
I don't really like most of the new features added to C since C99. There are a handful of C11 features I like, but they were part of the GNU C99 extensions long before 2011. I think the next C standard needs to add this as a standard library addition. Either there should be a function for switching modes between the default buffered, blocking until newline line input, buffered non-blocking line input, and unbuffered direct read of the most recent key pressed, or there needs to be addition functions for reading input, one that reads whatever is there without blocking, and one that gets keyboard state directly (which could include multiple keys being pressed at the same time). The current default should be left as the default, not just for backward compatibility, but also because it just makes sense, but C being stuck in the terminal era is frankly kind of dumb.
0
u/[deleted] 21d ago
[deleted] — view deleted comment