{"id":5204,"date":"2022-02-19T00:01:53","date_gmt":"2022-02-19T08:01:53","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5204"},"modified":"2022-02-12T11:08:59","modified_gmt":"2022-02-12T19:08:59","slug":"variable-scope","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5204","title":{"rendered":"Variable Scope"},"content":{"rendered":"<p>In C programming, variables declared within a function are local to the function. Specifically, they&#8217;re the <em>auto<\/em> storage class, the default. External, or global, variables are defined outside of a function declaration and are of the <em>extern<\/em> storage class. These variables are available to all functions. But the scope within a function can also be limited, depending on where the variable is declared.<br \/>\n<!--more--><br \/>\nFor a review of storage class specifiers, refer to my <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2978\">2018 post on the <em>auto<\/em> keyword<\/a>. Or don&#8217;t. The point is that an <em>auto<\/em> variable&#8217;s scope is local to its function and that the variable&#8217;s value is discarded when the function terminates. Further, an <em>auto<\/em> variable&#8217;s scope is also limited to the block statements in which it&#8217;s declared. This limitation may throw you for a loop [sic].<\/p>\n<p>For my coding style, variable scope within a function or block isn&#8217;t an issue. I declare all variables at the top of the function, immediately after the first brace. For example, here are the first few lines of code from <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5183\">this month&#8217;s Exercise<\/a> solution:<\/p>\n<pre class=\"screen\">\r\nint main()\r\n{\r\n    <span class=\"comments\">\/* constants to set seconds in a minute and seconds in an hour *\/<\/span>\r\n    const int minutes = 60;\r\n    const int hours = 60 * minutes;\r\n    int h,m,s;<\/pre>\n<p>These statements list all the constants and variables used throughout the <em>main()<\/em> function.<\/p>\n<p>It&#8217;s also possible in C to declare a variable as it&#8217;s needed. For example:<\/p>\n<p><code>for( int i = 0; i<100; i++ )<\/code><\/p>\n<p>The statement above declares variable <code>i<\/code> as it's used in the <em>for<\/em> loop. As this declaration happens within the function's main block, variable <code>i<\/code> is available within that block. But if you define a block within a function and declare a variable within that block, the variable is visible only within the block; its scope is limited.<\/p>\n<p>Consider this code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2022_02_19-Lesson.c\" rel=\"noopener\" target=\"_blank\">2022_02_19-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int a = 10;\r\n\r\n    printf(\"Variable a = %d\\n\",a);\r\n    {\r\n        int b = a+5;\r\n        printf(\"Variable b = %d\\n\",b);\r\n        int c = b+a;\r\n        printf(\"Variable c = %d\\n\",c);\r\n    }\r\n    int d = a+b\/c;\r\n    printf(\"Variable d = %d\\n\",d);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>This program won't build. Two errors are generated because variables <code>b<\/code> and <code>c<\/code> referenced at Line 14 don't exist. These variables are declared in a block and they remain local to the block, invisible to the rest of the function.<\/p>\n<p>The sample code above shows a lonely block without a control statement, which is possible but rare inside a function. More common are loop and decision blocks. If you declare a variable within such a block, it remains local (<em>auto<\/em>) to the block. The rest of the function cannot access the variable.<\/p>\n<p>No solution exists to get around a variable's block-scope; you cannot declare such a variable <em>static<\/em> or <em>extern<\/em> lest the compiler have a hissy fit. The only solution I can present is to declare all variables at the top of the function, which is what I do. This approach may not seem convenient or be popular, but it avoids any problems found from declaring a variable inside a block.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Variable declarations within a block remain local to the block. This rule can cause grief if you don&#8217;t understand variable scope. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5204\">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-5204","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\/5204","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=5204"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5204\/revisions"}],"predecessor-version":[{"id":5215,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5204\/revisions\/5215"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5204"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5204"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5204"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}