{"id":4217,"date":"2020-07-04T00:01:21","date_gmt":"2020-07-04T07:01:21","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4217"},"modified":"2025-05-26T08:53:25","modified_gmt":"2025-05-26T15:53:25","slug":"what-size-integer","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4217","title":{"rendered":"What Size Integer?"},"content":{"rendered":"<p>Things were stable back in the old days. When I first coded C, a <em>char<\/em> was 8-bits (a byte), and an <em>int<\/em> was 16-bits. The <em>short<\/em> was also 16-bits and the <em>long<\/em>, it was truly long at 32-bits. Today, things aren&#8217;t as consistent.<br \/>\n<!--more--><br \/>\nThe problem is that no true standard exists for the size of a C language integer data type. The only constant is that a <em>char<\/em> is still a byte, an 8-bit value. Otherwise, to determine the size of things for a given compiler, you can run this code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2020_07_04-Lesson-a.c\" rel=\"noopener noreferrer\" target=\"_blank\">2020_07_04-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    printf(\"char is %lu bits\\n\",sizeof(char)*8);\r\n    printf(\"short is %lu bits\\n\",sizeof(short)*8);\r\n    printf(\"int is %lu bits\\n\",sizeof(int)*8);\r\n    printf(\"long is %lu bits\\n\",sizeof(long)*8);\r\n    printf(\"long long is %lu bits\\n\",sizeof(long long)*8);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Each <em>printf()<\/em> statement takes a given integer data type and outputs its bit width. The <em>sizeof<\/em> operator returns the size in bytes, which are assumed to be 8-bits wide. The <code>%lu<\/code> placeholder handles the <em>long unsigned<\/em> value returned from the <em>sizeof<\/em> operator.<\/p>\n<p>The output on my iMac appears as follows:<\/p>\n<p><code>char is 8 bits<br \/>\nshort is 16 bits<br \/>\nint is 32 bits<br \/>\nlong is 64 bits<br \/>\nlong long is 64 bits<\/code><\/p>\n<p>Decades ago, a <em>short<\/em> and an <em>int<\/em> were the same size. No more! On the Mac, a <em>long<\/em> is 64 bits &mdash; and so is the <em>long long<\/em>. On some systems, the <em>long long<\/em> might be 128 bits. Who knows?<\/p>\n<p>Well, the compiler knows, of course. The <em>sizeof<\/em> operator returns the width. Constants declared in the <code>limits.h<\/code> header file also help with determining the maximum values that certain data types can handle.<\/p>\n<p>Beyond these tidbits, you can use special defined types that predicate their bit width right in the type definition. They are:<\/p>\n<p><em>int8_t<\/em>, an 8-bit integer<br \/>\n<em>int16_t<\/em>, a 16-bit integer<br \/>\n<em>int32_t<\/em>, a 32-bit integer<br \/>\n<em>int64_t<\/em>, a 64-bit integer<\/p>\n<p>These typedefs are available in the <code>stdint.h<\/code> header file, though they might also be available directly in <code>stdio.h<\/code> &mdash; but don&#8217;t count on it! Regardless, these types define precise bit widths as their names imply. No matter how the compiler interprets a <em>short<\/em>, <em>int<\/em>, or <em>long<\/em>, these data types remain consistent, as this sample code demonstrates:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2020_07_04-Lesson-b.c\" rel=\"noopener noreferrer\" target=\"_blank\">2020_07_04-Lesson-b.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdint.h&gt;\r\n\r\nint main()\r\n{\r\n    printf(\"int8_t is %lu bits\\n\",sizeof(int8_t)*8);\r\n    printf(\"int16_t is %lu bits\\n\",sizeof(int16_t)*8);\r\n    printf(\"int32_t is %lu bits\\n\",sizeof(int32_t)*8);\r\n    printf(\"int64_t is %lu bits\\n\",sizeof(int64_t)*8);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The output:<\/p>\n<p><code>int8_t is 8 bits<br \/>\nint16_t is 16 bits<br \/>\nint32_t is 32 bits<br \/>\nint64_t is 64 bits<\/code><\/p>\n<p>There. Consistency.<\/p>\n<p>Two variations exist for each of these defined data types, <em>least<\/em> and <em>fast<\/em>. The least version ensures that the values use at least the bit width specified:<\/p>\n<p><em>int_least8_t<\/em>, at least an 8-bit integer<br \/>\n<em>int_least16_t<\/em>, at least a 16-bit integer<br \/>\n<em>int_least32_t<\/em>, at least a 32-bit integer<br \/>\n<em>int_least64_t<\/em>, at least a 64-bit integer<\/p>\n<p>For example, <em>int_least8_t<\/em> has a minimum width of 8-bits.<\/p>\n<p>I&#8217;m unsure what the fast variation does:<\/p>\n<p><em>int_fast8_t<\/em>, a fast 8-bit integer<br \/>\n<em>int_fast16_t<\/em>, a fast 16-bit integer<br \/>\n<em>int_fast32_t<\/em>, a fast 32-bit integer<br \/>\n<em>int_fast64_t<\/em>, a fast 64-bit integer<\/p>\n<p>Internally and at least on the Mac, the <code>stdint.h<\/code> header typedef&#8217;s all these variations to the basic definitions. These types most likely exist for compatibility purposes only.<\/p>\n<p>Another data type not yet available in C is the variable width integer. The <em>varint<\/em> declares its bit width and then ensures that all declared variables use the given width. This additional data type sounds interesting and would be a fun addition to C in the future.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Is an <em>int<\/em> the same as a <em>short<\/em> or a <em>long<\/em>? The number of bits in an integer isn&#8217;t really defined. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4217\">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-4217","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\/4217","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=4217"}],"version-history":[{"count":8,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4217\/revisions"}],"predecessor-version":[{"id":7018,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4217\/revisions\/7018"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4217"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4217"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4217"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}