{"id":3034,"date":"2024-05-10T11:28:57","date_gmt":"2024-05-10T11:28:57","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=3034"},"modified":"2024-05-10T11:28:57","modified_gmt":"2024-05-10T11:28:57","slug":"initialization-of-static-variables-in-c","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/initialization-of-static-variables-in-c\/","title":{"rendered":"Initialization Of Static Variables In C"},"content":{"rendered":"\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\" id=\"rank-math-toc\"><p>Table of Contents<\/p><nav><ul><li ><a href=\"#initialization-of-static-variables-in-c\">Initialization Of Static Variables In C<\/a><\/li><li ><a href=\"#the-program-below-fail-in-compilation\">The program below Fail in Compilation<\/a><\/li><li ><a href=\"#changing-the-program-to-work-without-any-error\">Changing the Program To Work Without Any Error<\/a><\/li><li ><a href=\"#faq-initialization-of-static-variables-in-c\">FAQ- Initialization of static variables in C<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"initialization-of-static-variables-in-c\">Initialization Of Static Variables In C<\/h2>\n\n\n\n<p>In C, static variables must be initialized using constant literals. If you attempt to initialize a static variable with a non-constant value or an expression that cannot be determined at compile time, the program will indeed fail to compile. This is because static variables must have values that are known at compile time to ensure their memory allocation and initialization can be properly handled by the compiler. The program given below will fail in compilation.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"the-program-below-fail-in-compilation\">The program below Fail in Compilation<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include&lt;stdio.h&gt; \nint initializer(void) \n{ \n    return 50; \n} \n  \nint main() \n{ \n    static int i = initializer(); \n    printf(\" value of i = %d\", i); \n    getchar(); \n    return 0; \n} <\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"changing-the-program-to-work-without-any-error\">Changing the Program To Work Without Any Error<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include&lt;stdio.h&gt; \nint main() \n{ \n    static int i = 50; \n    printf(\" value of i = %d\", i); \n    getchar(); \n    return 0; \n} <\/code><\/pre>\n\n\n\n<p>Static variables, which have static storage duration, must be initialized before the <code>main()<\/code> function starts executing. Because the value of non-constant expressions may not be known at translation (compile) time, they cannot be used for the initialization of static variables. This requirement ensures that static variables have well-defined initial values that can be determined at compile time and properly set before the program&#8217;s execution begins.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"faq-initialization-of-static-variables-in-c\">FAQ- Initialization of static variables in C<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1698225212512\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1. How is a static variable initialized?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. In C and C++, a static variable declared within a block is initialized only once, before the program starts. On the other hand, an auto variable with an initializer is initialized each time it&#8217;s created. Additionally, a static object of a class type will employ the default constructor if you don&#8217;t provide an explicit initialization. These rules help ensure that variables have predictable initial values and behavior in a program.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1698225249661\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2. Are static variables initialized to zero in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. In C and C++, static variables, like global variables, are automatically initialized to zero if you don&#8217;t provide an explicit initial value. This is a default behavior that ensures static and global variables start with a known value (zero) if you haven&#8217;t specified another value during the declaration.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1698225258772\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3. How do you initialize a static int?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. In your C++ code snippet, <code>MyStruct::a<\/code> will be &#8220;const-initialized&#8221; because the value 67 is a compile-time constant, which is also considered a constant expression.<br \/>In C++, when you provide an initializer for a <code>static<\/code> data member that is a constant expression, it is &#8220;const-initialized,&#8221; which means it&#8217;s initialized at compile time. This is a valuable feature for ensuring that the variable&#8217;s value is known and set before the program starts running, and it allows for more predictable behavior in your code.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Initialization Of Static Variables In C In C, static variables must be initialized using constant literals. If you attempt to initialize a static variable with a non-constant value or an expression that cannot be determined at compile time, the program will indeed fail to compile. This is because static variables must have values that are &#8230; <a title=\"Initialization Of Static Variables In C\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/initialization-of-static-variables-in-c\/\" aria-label=\"More on Initialization Of Static Variables In C\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":5398,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[501],"class_list":["post-3034","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-programming","tag-initialization-of-static-variables-in-c","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"_links":{"self":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/3034","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/comments?post=3034"}],"version-history":[{"count":9,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/3034\/revisions"}],"predecessor-version":[{"id":10741,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/3034\/revisions\/10741"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/5398"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=3034"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=3034"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=3034"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}