{"id":4711,"date":"2021-04-24T00:01:16","date_gmt":"2021-04-24T07:01:16","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4711"},"modified":"2021-04-16T15:55:33","modified_gmt":"2021-04-16T22:55:33","slug":"how-big-is-your-bufsiz","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4711","title":{"rendered":"How Big is Your BUFSIZ?"},"content":{"rendered":"<p>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, <code>BUFSIZ<\/code>, that can also be used to set a buffer size safely and consistently on all C compilers.<br \/>\n<!--more--><br \/>\nJust to get it out of the way, it vexes me to no end that the <code>BUFSIZ<\/code> constant lacks an <code>E<\/code> in its name. Seriously.<\/p>\n<p>The <code>BUFSIZ<\/code> defined constant is declared in the <code>stdio.h<\/code> header file as a default buffer size. Specifically, it&#8217;s the size of the buffer used by the <em>setbuf()<\/em> function, which was covered in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3701\">this blog post<\/a>. Here is the code from the post:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2021_04_24-Lesson-a.c\" rel=\"noopener\" target=\"_blank\">2021_04_24-Lesson-a.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    char buffer[BUFSIZ];\r\n\r\n    <span class=\"comments\">\/* setup the buffer *\/<\/span>\r\n    setbuf(stdout,buffer);\r\n\r\n    puts(\"Hold it!\");\r\n    puts(\"Hold it!\");\r\n    puts(\"Go!\");\r\n    getchar();\r\n    puts(\"Thank you.\");\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Streaming C functions use <code>BUFSIZ<\/code> for their operations, as does <em>setbuf()<\/em> shown above. The code changes output streaming, holding it in <code>buffer[]<\/code> until input is received from the <em>getchar()<\/em> function at Line 13. The program&#8217;s output then appears all at once. Refer to the earlier post for more details, but my purpose here is to discuss <code>BUFSIZ<\/code>.<\/p>\n<p>You can borrow the <code>BUFSIZ<\/code> defined constant for use in declaring your code&#8217;s buffer size, especially when working with input and output. Still, curiosity gets the best of me: How big is <code>BUFSIZ<\/code>? What is its defined value?<\/p>\n<p>You could plow through <code>stdio.h<\/code> (and all its included header files) to locate the <code>BUFSIZ<\/code> declaration, but instead just run the following code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2021_04_24-Lesson-b.c\" rel=\"noopener\" target=\"_blank\">2021_04_24-Lesson-b.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    printf(\"On this machine, the BUFSIZ buffer is set to %d bytes\\n\",\r\n            BUFSIZ\r\n          );\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Because <code>BUFSIZ<\/code> is assigned an <em>int<\/em>, I use the <code>%d<\/code> placeholder in the <em>printf()<\/em> statement to output the value.<\/p>\n<p>Here&#8217;s what appears on my Mac: <\/p>\n<p><code>On this machine, the BUFSIZ buffer is set to 1024 bytes<\/code><\/p>\n<p>A 1K buffer seems nice, but here&#8217;s the output for Ubuntu Linux:<\/p>\n<p><code>On this machine, the BUFSIZ buffer is set to 8192 bytes<\/code><\/p>\n<p>My Linux distro sets a buffer size of 8K, or 8,192 bytes. Interesting &mdash; and I don&#8217;t know why.<\/p>\n<p>Here&#8217;s the output from MinGW using Code::Blocks in Windows:<\/p>\n<p><code>On this machine, the BUFSIZ buffer is set to 512 bytes<\/code><\/p>\n<p>Whoa.<\/p>\n<p>While no consistent value seems to be set for the various compilers, the point is that <code>BUFSIZ<\/code> is available and ready for use when you quickly need a buffer for streaming input and output. Whether it be large or small, it&#8217;s what the compiler designers felt would be adequate for the given environment.<\/p>\n<p>Another point!<\/p>\n<p>This Lesson demonstrates the value of defined constants: The name <code>BUFSIZ<\/code> is consistent and available to all compilers, yet each compiler sets a different value. The same concept applies to <em>typedefs<\/em>, providing cross-platform consistency without the programmer (you) needing to worry about or research the specifics.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The <code>BUFSIZ<\/code> defined constant provides a handy way to create a buffer of a given size. But what&#8217;s the size? <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4711\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-4711","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4711","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4711"}],"version-history":[{"count":9,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4711\/revisions"}],"predecessor-version":[{"id":4735,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4711\/revisions\/4735"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4711"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4711"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4711"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}