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

๐Ÿ’€ 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() returns 0 on success. it returns nonzero on failure (either mode is invalid or the request cannot be honored). it may set errno on 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 that buf points to still exists by the time stream is 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
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms