{"id":4521,"date":"2022-08-23T05:43:24","date_gmt":"2022-08-23T00:13:24","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-why-doesnt-bar-in-this-code-have-static-storage-duration\/"},"modified":"2022-08-23T05:43:24","modified_gmt":"2022-08-23T00:13:24","slug":"solved-why-doesnt-bar-in-this-code-have-static-storage-duration","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-why-doesnt-bar-in-this-code-have-static-storage-duration\/","title":{"rendered":"[Solved] Why doesn&#8217;t `bar` in this code have static storage duration?"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-35274059\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"35274059\" data-parentid=\"35273855\" data-score=\"5\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>You are confusing the <em>scope<\/em> of a variable with the <em>storage duration<\/em>.<\/p>\n<p>As mentioned in the <code>C11<\/code> standard, chapter \u00a76.2.1, <em>Scopes of identifiers<\/em>,<\/p>\n<ul>\n<li>for <em>file scope<\/em><\/li>\n<\/ul>\n<blockquote>\n<p>[&#8230;] If the declarator or type specifier that declares the identifier<br \/>\n  appears outside of any block or list of parameters, the identifier has file scope, which<br \/>\n  terminates at the end of the translation unit. [&#8230;]<\/p>\n<\/blockquote>\n<p>and for <em>function<\/em> (or <em>block<\/em>) scope<\/p>\n<blockquote>\n<p>[&#8230;] If the declarator or type specifier that<br \/>\n  declares the identifier appears inside a block or within the list of parameter declarations in<br \/>\n  a function definition, the identifier has block scope, which terminates at the end of the<br \/>\n  associated block. [&#8230;]<\/p>\n<\/blockquote>\n<p>In your case, <code>bar<\/code> has the file scope in <code>foo()<\/code>. So this is not <em>visible<\/em> in <code>main()<\/code>.<\/p>\n<p>OTOH, for the <em>storage duration<\/em> part,<\/p>\n<blockquote>\n<p>An object whose identifier is declared without the storage-class specifier<br \/>\n  <code>_Thread_local<\/code>, and either with external or internal linkage or with the storage-class<br \/>\n  specifier <code>static<\/code>, has <em>static storage duration<\/em>. Its lifetime is the entire execution of the<br \/>\n  program and its stored value is initialized only once, prior to program startup.<\/p>\n<\/blockquote>\n<p>So, to summarize, <code>bar<\/code> has static storage duration, but the scope is limited to the <code>foo()<\/code> function. So, it is<\/p>\n<blockquote>\n<p><em>declared and initialized prior to <code>main()<\/code> function<\/em><\/p>\n<\/blockquote>\n<p>(before <code>main()<\/code> starts, to be exact) but not visible and accessible in <code>main()<\/code>.<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\"><\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Why doesn&#8217;t `bar` in this code have static storage duration? <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] You are confusing the scope of a variable with the storage duration. As mentioned in the C11 standard, chapter \u00a76.2.1, Scopes of identifiers, for file scope [&#8230;] If the declarator or type specifier that declares the identifier appears outside of any block or list of parameters, the identifier has file scope, which terminates at &#8230; <a title=\"[Solved] Why doesn&#8217;t `bar` in this code have static storage duration?\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-why-doesnt-bar-in-this-code-have-static-storage-duration\/\" aria-label=\"More on [Solved] Why doesn&#8217;t `bar` in this code have static storage duration?\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[324,904,414,903],"class_list":["post-4521","post","type-post","status-publish","format-standard","hentry","category-solved","tag-c","tag-declaration","tag-scope","tag-static"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Why doesn&#039;t `bar` in this code have static storage duration? - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-why-doesnt-bar-in-this-code-have-static-storage-duration\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Why doesn&#039;t `bar` in this code have static storage duration? - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] You are confusing the scope of a variable with the storage duration. As mentioned in the C11 standard, chapter \u00a76.2.1, Scopes of identifiers, for file scope [&#8230;] If the declarator or type specifier that declares the identifier appears outside of any block or list of parameters, the identifier has file scope, which terminates at ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-why-doesnt-bar-in-this-code-have-static-storage-duration\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-08-23T00:13:24+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-why-doesnt-bar-in-this-code-have-static-storage-duration\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-why-doesnt-bar-in-this-code-have-static-storage-duration\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Why doesn&#8217;t `bar` in this code have static storage duration?\",\"datePublished\":\"2022-08-23T00:13:24+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-why-doesnt-bar-in-this-code-have-static-storage-duration\/\"},\"wordCount\":230,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"c++\",\"declaration\",\"scope\",\"static\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-why-doesnt-bar-in-this-code-have-static-storage-duration\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-why-doesnt-bar-in-this-code-have-static-storage-duration\/\",\"name\":\"[Solved] Why doesn't `bar` in this code have static storage duration? - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-08-23T00:13:24+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-why-doesnt-bar-in-this-code-have-static-storage-duration\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-why-doesnt-bar-in-this-code-have-static-storage-duration\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-why-doesnt-bar-in-this-code-have-static-storage-duration\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Why doesn&#8217;t `bar` in this code have static storage duration?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Why doesn't `bar` in this code have static storage duration? - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-why-doesnt-bar-in-this-code-have-static-storage-duration\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Why doesn't `bar` in this code have static storage duration? - JassWeb","og_description":"[ad_1] You are confusing the scope of a variable with the storage duration. As mentioned in the C11 standard, chapter \u00a76.2.1, Scopes of identifiers, for file scope [&#8230;] If the declarator or type specifier that declares the identifier appears outside of any block or list of parameters, the identifier has file scope, which terminates at ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-why-doesnt-bar-in-this-code-have-static-storage-duration\/","og_site_name":"JassWeb","article_published_time":"2022-08-23T00:13:24+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-why-doesnt-bar-in-this-code-have-static-storage-duration\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-why-doesnt-bar-in-this-code-have-static-storage-duration\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Why doesn&#8217;t `bar` in this code have static storage duration?","datePublished":"2022-08-23T00:13:24+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-why-doesnt-bar-in-this-code-have-static-storage-duration\/"},"wordCount":230,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["c++","declaration","scope","static"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-why-doesnt-bar-in-this-code-have-static-storage-duration\/","url":"https:\/\/jassweb.com\/solved\/solved-why-doesnt-bar-in-this-code-have-static-storage-duration\/","name":"[Solved] Why doesn't `bar` in this code have static storage duration? - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-08-23T00:13:24+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-why-doesnt-bar-in-this-code-have-static-storage-duration\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-why-doesnt-bar-in-this-code-have-static-storage-duration\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-why-doesnt-bar-in-this-code-have-static-storage-duration\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Why doesn&#8217;t `bar` in this code have static storage duration?"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/4521","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=4521"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/4521\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=4521"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=4521"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=4521"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}