{"id":2978,"date":"2018-03-03T00:01:19","date_gmt":"2018-03-03T08:01:19","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2978"},"modified":"2018-02-24T10:30:39","modified_gmt":"2018-02-24T18:30:39","slug":"from-the-c-relic-file-auto","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2978","title":{"rendered":"From the C Relic File: <em>auto<\/em>"},"content":{"rendered":"<p>Of all the C language keywords you don&#8217;t use, <em>auto<\/em> is probably the most memorable. That&#8217;s because it&#8217;s listed first among the canonical table of 32 C language keywords. Yet, in all the code you&#8217;ve written or seen, <em>auto<\/em> is seldom used if at all. So what does it do and is it even necessary?<br \/>\n<!--more--><br \/>\nFirst, a note to the nerds: Yes, I know, in the C11 standard, the first keyword is <em>_Alignas<\/em>. Even today, when you see the C language keywords listed, most resources show <em>auto<\/em> first. And few keyword tables list all the underscore keywords.<\/p>\n<p>Second, <em>auto<\/em> is officially known as a <em>storage class specifier<\/em>. The C language has five of them:<\/p>\n<ul>\n<li><em>auto<\/em><\/li>\n<li><em>register<\/em><\/li>\n<li><em>static<\/em><\/li>\n<li><em>extern<\/em><\/li>\n<li><em>typedef<\/em><\/li>\n<\/ul>\n<p>All variables you declare in a function without using a storage class specifier are assumed to be <em>auto<\/em>. That&#8217;s why you don&#8217;t see it used; it&#8217;s set <em>auto<\/em>matically. And this definition is important: <em>auto<\/em> is used only within a function. You cannot create an global <em>auto<\/em> variable, one that exists outside of a function. Those are <em>extern<\/em> storage class variables.<\/p>\n<p>In a way, you can think of the <em>auto<\/em> class as the opposite of the <em>static<\/em> class. A <em>static<\/em> variable type is one thats value is retained when a function is done; <em>auto<\/em> variable values are released.<\/p>\n<p>Third, some programmers may claim that <em>auto<\/em> is a synonym for an <em>int<\/em> variable. It&#8217;s not. The <em>int<\/em> variable type is the default for C. So if you use a declaration such as:<\/p>\n<p><code>auto x;<\/code><\/p>\n<p>What you&#8217;re specifying is:<\/p>\n<p><code>auto int x;<\/code><\/p>\n<p>Or really:<\/p>\n<p><code>int x;<\/code><\/p>\n<p>Each of these three statements is processed identically by the compiler, though <code>auto x;<\/code> generates a warning that an <em>int<\/em> variable is assumed.<\/p>\n<p>I&#8217;ve mused for a while on which code to present as an example in this Lesson. Because <em>auto<\/em> is applied to any variable type in a function that doesn&#8217;t have <em>register<\/em>, <em>static<\/em>, <em>extern<\/em>, or <em>typedef<\/em> before it, an example is pointless. This raises the question of whether <em>auto<\/em> is necessary. It&#8217;s not! You can code with it or without it. In fact, leaving it off is the best course &mdash; unless you want to confuse someone by sticking a random <em>auto<\/em> in your code.<\/p>\n<p>By the way, the <em>register<\/em> keyword is similar to <em>auto<\/em>. It follows the same rules, though <em>register<\/em> stipulates that the variable is used frequently. Further, you cannot use the <code>&<\/code> (address-of) operator with a <code>register<\/code> class variable.<\/p>\n<p>In the olden days, <em>register<\/em> may have directed the compiler to use a processor register to store an integer value. I&#8217;m not sure whether that&#8217;s implemented today or even if such action would noticeably improve program performance.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The <em>auto<\/em> keyword is a holdover from the ancient B language that predated C. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2978\">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-2978","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\/2978","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=2978"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2978\/revisions"}],"predecessor-version":[{"id":3001,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2978\/revisions\/3001"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2978"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2978"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2978"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}