setbuf(3)
๐ setbuf(3) โ stream buffering operations ๐
okay bestie, sit down, hydrate, because this is the lore of how your output actually leaves the building. this is the setbuf(3) page from the library functions manual, and we're gonna go through it section by section, no cap.
โจ NAME โจ
setbuf, setbuffer, setlinebuf, setvbuf โ stream buffering operations ๐ซถ
๐ LIBRARY ๐
Standard C library (libc, -lc) ๐
๐ธ SYNOPSIS ๐ธ
#include <stdio.h>
int setvbuf(size_t size;
FILE *restrict stream, char buf[restrict size],
int mode, size_t size);
void setbuf(FILE *restrict stream, char *restrict buf);
void setbuffer(size_t size;
FILE *restrict stream, char buf[restrict size],
size_t size);
void setlinebuf(FILE *stream);
๐ง Feature Test Macro Requirements for glibc (see feature_test_macros(7)) ๐ง
setbuffer(),setlinebuf():- Since glibc 2.19:
_DEFAULT_SOURCE
- glibc 2.19 and earlier:
_BSD_SOURCE
- Since glibc 2.19:
๐ DESCRIPTION ๐
so there are, like, three whole buffering vibes available: unbuffered, block buffered, and line buffered. and the way they work is genuinely so simple:
- when an output stream is unbuffered, the info shows up on the destination file or terminal literally the second it's written. immediate. main character behavior.
- when it's block buffered, a whole bunch of characters get saved up and then written out together as one block. it's giving โจdelayed gratificationโจ.
- when it's line buffered, characters get saved up until a newline is output, or until input is read from any stream attached to a terminal device (typically
stdin). so basically it flushes whenever a line finishes or whenever someone's typing at you.
and if you can't wait for the block to finish cooking? fflush(3) forces it out early. (see also fclose(3).)
normally, all files are block buffered. but if a stream refers to a terminal (like stdout normally does), it's line buffered. and the standard error stream stderr? always unbuffered by default. iconic, honestly, because errors should never be late to the party. ๐ญ
๐ okay so setvbuf() ๐
setvbuf() can be used on any open stream to change its buffer. the mode argument must be one of these three macros:
_IONBFโ unbuffered_IOLBFโ line buffered_IOFBFโ fully buffered
except for unbuffered files, the buf argument should point to a buffer that's at least size bytes long, and that buffer will be used instead of the current one. if buf is NULL, only the mode is affected โ a new buffer gets allocated on the next read or write operation.
โ ๏ธ big one, bestie: setvbuf() may be used only after opening a stream and before any other operations have been performed on it. that's a real constraint, not a suggestion.
๐ฆ and the other three are just aliases ๐ฆ
the other three calls are, in effect, simply aliases for calls to setvbuf(). like, that's it, that's the whole thing.
setbuf() is exactly equivalent to this call:
setvbuf(stream, buf, buf ? _IOFBF : _IONBF, BUFSIZ);
setbuffer() is the same, except the size of the buffer is up to the caller instead of being determined by the default BUFSIZ. ๐
and setlinebuf() is exactly equivalent to this call:
setvbuf(stream, NULL, _IOLBF, 0);
๐ฅ RETURN VALUE ๐ฅ
setvbuf()returns0on success. it returns nonzero on failure (eithermodeis invalid or the request cannot be honored). it may seterrnoon failure.- the other functions do not return a value. they're void. they simply do not have opinions to give you.
๐ ATTRIBUTES ๐
for an explanation of the terms used in this section, see attributes(7).
setbuf(),setbuffer(),setlinebuf(),setvbuf()โ Thread safety:MT-Safe๐ซถ
(that's the whole table, bestie โ one row, fully slaying.)
โญ STANDARDS โญ
setbuf()setvbuf()- C11, POSIX.1-2008.
๐ HISTORY ๐
setbuf()setvbuf()- C89, POSIX.1-2001.
๐ CAVEATS ๐
POSIX notes that the value of errno is unspecified after a call to setbuf(), and further notes that, since the value of errno is not required to be unchanged after a successful call to setbuf(), applications should instead use setvbuf() in order to detect errors. so if you're out here trying to error-check with setbuf()... you can't, babe. use the other one. ๐ญ
๐ BUGS ๐
๐จ listen to me: you must make sure that the space thatbufpoints to still exists by the timestreamis closed โ which also happens at program termination. this is the classic footgun, no cap.
for example, the following is invalid:
#include <stdio.h>
int
main(void)
{
char buf[BUFSIZ];
setbuf(stdout, buf);
printf("Hello, world!\n");
return 0;
}
๐ SEE ALSO ๐
okay that's the whole page, you ate that ๐
go forth and buffer responsibly. this was Linux man-pages 6.20, dated 2025-09-21, and the manual name is, of course, setbuf(3). mwah ๐ซถโจ๐
๐ฆ where this page comes from
- arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- ๐ the page's license: BSD-4-Clause-UC
- Copyright 1980-1991, Regents of the University of California
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms