How Big is Your BUFSIZ?

When my code requires a random odd buffer, I generally assign it a given size in some holy computer value: 16, 32, 64, and so on. But a defined constant exists, BUFSIZ, that can also be used to set a buffer size safely and consistently on all C compilers.

Just to get it out of the way, it vexes me to no end that the BUFSIZ constant lacks an E in its name. Seriously.

The BUFSIZ defined constant is declared in the stdio.h header file as a default buffer size. Specifically, it’s the size of the buffer used by the setbuf() function, which was covered in this blog post. Here is the code from the post:

2021_04_24-Lesson-a.c

#include <stdio.h>

int main()
{
    char buffer[BUFSIZ];

    /* setup the buffer */
    setbuf(stdout,buffer);

    puts("Hold it!");
    puts("Hold it!");
    puts("Go!");
    getchar();
    puts("Thank you.");

    return(0);
}

Streaming C functions use BUFSIZ for their operations, as does setbuf() shown above. The code changes output streaming, holding it in buffer[] until input is received from the getchar() function at Line 13. The program’s output then appears all at once. Refer to the earlier post for more details, but my purpose here is to discuss BUFSIZ.

You can borrow the BUFSIZ defined constant for use in declaring your code’s buffer size, especially when working with input and output. Still, curiosity gets the best of me: How big is BUFSIZ? What is its defined value?

You could plow through stdio.h (and all its included header files) to locate the BUFSIZ declaration, but instead just run the following code:

2021_04_24-Lesson-b.c

#include <stdio.h>

int main()
{
    printf("On this machine, the BUFSIZ buffer is set to %d bytes\n",
            BUFSIZ
          );

    return(0);
}

Because BUFSIZ is assigned an int, I use the %d placeholder in the printf() statement to output the value.

Here’s what appears on my Mac:

On this machine, the BUFSIZ buffer is set to 1024 bytes

A 1K buffer seems nice, but here’s the output for Ubuntu Linux:

On this machine, the BUFSIZ buffer is set to 8192 bytes

My Linux distro sets a buffer size of 8K, or 8,192 bytes. Interesting — and I don’t know why.

Here’s the output from MinGW using Code::Blocks in Windows:

On this machine, the BUFSIZ buffer is set to 512 bytes

Whoa.

While no consistent value seems to be set for the various compilers, the point is that BUFSIZ is available and ready for use when you quickly need a buffer for streaming input and output. Whether it be large or small, it’s what the compiler designers felt would be adequate for the given environment.

Another point!

This Lesson demonstrates the value of defined constants: The name BUFSIZ is consistent and available to all compilers, yet each compiler sets a different value. The same concept applies to typedefs, providing cross-platform consistency without the programmer (you) needing to worry about or research the specifics.

2 thoughts on “How Big is Your BUFSIZ?

  1. I’ve wondered why the funky abbreviations persist in computerdom. A lot of it had to do with storage in the early days: bytes were valuable.

Leave a Reply