{"id":1190,"date":"2015-02-14T00:01:26","date_gmt":"2015-02-14T08:01:26","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1190"},"modified":"2015-02-21T07:54:31","modified_gmt":"2015-02-21T15:54:31","slug":"know-your-limits","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1190","title":{"rendered":"Know Your Limits"},"content":{"rendered":"<p>Back when I was a young, budding nerd, I popped off how there are always 8 bits in a byte. A much wiser programmer raised an eyebrow and quizzed me, &#8220;Always?&#8221;<br \/>\n<!--more--><br \/>\nToday it seems like all computers set a byte&#8217;s size equal to 8 bits, but back in the 1970s some computers held 7 bits in a byte, some 8, and some 9 or 10. A &#8220;byte&#8221; was merely a quantity and the number of bits dwelling within the byte wasn&#8217;t set by some international standard. It was set by the system&#8217;s hardware design.<\/p>\n<p>To deal with such inconsistencies, the C language library offers you some assistance. To confirm various sizes and other hardware-specific trivia, you use the <code>limits.h<\/code> header file. It beats guessing.<\/p>\n<p>The constants defined in the <code>limits.h<\/code> file are consistent. Their values, however, are unique to each system. On modern computers, a majority of the defined constants have the same values, although it&#8217;s good to run a test program just to be sure.<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;limits.h&gt;\r\n\r\nint main()\r\n{\r\n    printf(\"%-16s\\t%20s\\n\",\"VARIABLE\",\"VALUE\");\r\n    printf(\"%16s\\t%20d\\n\",\"bits in a byte\",CHAR_BIT);\r\n    printf(\"%16s\\t%20d\\n\",\"char max\",CHAR_MAX);\r\n    printf(\"%16s\\t%20d\\n\",\"char min\",CHAR_MIN);\r\n    printf(\"%16s\\t%20d\\n\",\"signed char max\",SCHAR_MAX);\r\n    printf(\"%16s\\t%20d\\n\",\"signed char min\",SCHAR_MIN);\r\n    printf(\"%16s\\t%20d\\n\",\"unsigned char\",UCHAR_MAX);\r\n    printf(\"%16s\\t%20d\\n\",\"short int max\",SHRT_MAX);\r\n    printf(\"%16s\\t%20d\\n\",\"short int min\",SHRT_MIN);\r\n    printf(\"%16s\\t%20d\\n\",\"int max\",INT_MAX);\r\n    printf(\"%16s\\t%20d\\n\",\"int min\",INT_MIN);\r\n    printf(\"%16s\\t%20ld\\n\",\"long max\",LONG_MAX);\r\n    printf(\"%16s\\t%20ld\\n\",\"long min\",LONG_MIN);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>This code examines only a few of the constants defined in the <code>limits.h<\/code> header. These are primarily the integer constants, with the &#8220;bits per byte&#8221; value included for good measure.<\/p>\n<blockquote><p>If you&#8217;re curious about the <em>printf()<\/em> function formatting, refer to my <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=314\">post from October of 2013<\/a> on the <code>%s<\/code> text-formatting conversion character.<\/p><\/blockquote>\n<p>Here is the sample output on my system, which is a Macintosh running an older version of OS X:<\/p>\n<pre><code>VARIABLE        \t               VALUE\r\n  bits in a byte\t                   8\r\n        char max\t                 127\r\n        char min\t                -128\r\n signed char max\t                 127\r\n signed char min\t                -128\r\n   unsigned char\t                 255\r\n   short int max\t               32767\r\n   short int min\t              -32768\r\n         int max\t          2147483647\r\n         int min\t         -2147483648\r\n        long max\t 9223372036854775807\r\n        long min\t-9223372036854775808<\/code><\/pre>\n<p>Other constants are defined in <code>limits.h<\/code> as well, a few of which are vital to know if you don&#8217;t want your code to crash. I&#8217;ll demonstrate one of them in <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1216\">next week&#8217;s Lesson<\/a>. I&#8217;ll also show you how to handle the instance where a constant is undefined.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You may not know, but the C Library knows its limits. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1190\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-1190","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\/1190","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=1190"}],"version-history":[{"count":7,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1190\/revisions"}],"predecessor-version":[{"id":1238,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1190\/revisions\/1238"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1190"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1190"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1190"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}