{"id":2877,"date":"2017-12-30T00:01:53","date_gmt":"2017-12-30T08:01:53","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2877"},"modified":"2017-12-23T09:48:59","modified_gmt":"2017-12-23T17:48:59","slug":"the-size_t-variable-type","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2877","title":{"rendered":"The <em>size_t<\/em> Variable Type"},"content":{"rendered":"<p>The C language has its basic variable types: <em>char<\/em>, <em>int<\/em>, <em>float<\/em>, <em>double<\/em>, <em>struct<\/em>, and that oddball newbie <em>_Bool<\/em>. Anything else you see as a &#8220;variable&#8221; is probably a convenient shortcut manufactured by using a <em>typedef<\/em> statement. Some of these <em>typedef<\/em> variables are faily standard, including the most common one, <em>size_t<\/em>.<br \/>\n<!--more--><br \/>\nThe <em>typedef<\/em> operator creates a convenient shortcut for a variable type. It&#8217;s similar to the <code>#define<\/code> preprocessor directive in that it&#8217;s valid only in the code where it&#8217;s used.<\/p>\n<p>Most frequently, <em>typedef<\/em> creates shortcuts for structure variables, but many header files use <em>typedef<\/em> to create special names for specific variable types, such as <em>size_t<\/em>. Specifically, the <em>size_t<\/em> variable type is related to the <em>sizeof<\/em> operator, which returns the size of a memory object in bytes.<\/p>\n<p>In the original K&#038;R book, a <em>byte<\/em> is defined as the size of a <em>char<\/em> variable: One byte of memory equals one <em>char<\/em>-sized value. When the <em>sizeof<\/em> operator returns 4, you know that the object occupies 4 bytes (<em>char<\/em>s) of memory.<\/p>\n<p>When Drs. Kernighan and Ritchie updated their book to cover ANSI C, they introduced the <em>size_t<\/em> variable as the type of value returned from the <em>sizeof<\/em> operator. It&#8217;s defined, or <em>typedef<\/em>&#8216;d, as an <em>unsigned int<\/em> value, usually an <em>unsigned long<\/em>. The value represents the size (in bytes) of a chunk of memory. So, whenever you see the <em>size_t<\/em> variable in a function prototype, it means that the function somehow deals with a chunk of memory.<\/p>\n<p>For example, the <em>malloc()<\/em> function uses a <em>size_t<\/em> argument to set aside a chunk of memory:<\/p>\n<p><code>void * malloc(size_t size);<\/code><\/p>\n<p>The <em>size_t<\/em> value also appears in some string function prototypes:<\/p>\n<p><code>size_t strlen(const char *s);<\/code><\/p>\n<p>The <em>strlen()<\/em> function returns the length of a string, which is really a value describing (almost) how many bytes (<em>char<\/em>s) of storage the string occupies. (The value returned from <em>strlen()<\/em> doesn&#8217;t account for the null character at the end of the string.)<\/p>\n<p>It&#8217;s common practice to use an <em>int<\/em> variable when a <em>size_t<\/em> value is specified, though this approach isn&#8217;t best. First, <em>size_t<\/em> is an <em>unsigned<\/em> value; you can&#8217;t have negative memory storage. Second, you must use the proper variable type with a function, lest you get into trouble.<\/p>\n<p>When a function calls for a <em>size_t<\/em> variable, I recommend declaring any associated variables as <em>size_t<\/em> values, not an <em>int<\/em>, <em>unsigned long<\/em>, or <em>unsigned int<\/em>:<\/p>\n<p><code>size_t msize,mnew,and,so,on;<\/code><\/p>\n<p>The variables defined above are created as <em>size_t<\/em> types.<\/p>\n<p>Another good practices is to use the proper placeholder in a <em>printf()<\/em> statement to display a <em>size_t<\/em> value. Remember, the value isn&#8217;t an <em>int<\/em>: When you use the <code>%d<\/code> placeholder, the compiler whines at you. That&#8217;s because the <code>%d<\/code> placeholder represents a <em>signed<\/em> value.<\/p>\n<p>In my code, I use <code>%lu<\/code> (<em>long unsigned<\/em>):<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n  \r\nint main()\r\n{\r\n    printf(\"An int uses %lu bytes of storage.\\n\",\r\n            sizeof(int)\r\n          );\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The C99 standard established the <code>%z<\/code> placeholder prefix for <em>size_t<\/em> values. You postfix the <code>%z<\/code> with a <code>d<\/code> or <code>u<\/code>, as in <code>%zu<\/code> or <code>%zd<\/code>, to properly display a <em>size_t<\/em> value.<\/p>\n<p>Other <em>typedef<\/em> values are used in various C language functions, such as the <em>time_t<\/em> values found in <code>time.h<\/code> functions. As with <em>size_t<\/em>, ensure that you create <em>time_t<\/em> variables for use with these functions. Using <em>unsigned int<\/em> values is okay, and I&#8217;ve done so in my code, but setting the proper variable type reduces the potential for errors.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s not an official C language variable type, but it could be. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2877\">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-2877","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\/2877","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=2877"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2877\/revisions"}],"predecessor-version":[{"id":2892,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2877\/revisions\/2892"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2877"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2877"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2877"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}