top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Why Static variable initialize by 0 and auto variable initialize with garbage value?

+2 votes
450 views
Why Static variable initialize by 0 and auto variable initialize with garbage value?
posted Mar 19, 2014 by Sandeep

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+3 votes
 
Best answer

Static variables are stored in the section called BSS (better save space :) ) and by the structure of the BSS all variable of BSS carry the value as zero (remember the variable is not assigned zero its the property of BSS that whole BSS area carry null value so the variable in BSS) where as local variables are stored in the local stack which does not have such property as BSS.

answer Mar 19, 2014 by Salil Agrawal
Similar Questions
+3 votes

As per my understanding uninitialized static variable is stored in BSS and initialized static variable is stored in DS, then what about the variable which is initialized with 0 ? where it will be stored BSS/DS?

+5 votes

You can find three principal uses for the static. This is a good way how to start your answer to this question. Let’s look at the three principal uses:

  1. Firstly, when you declare inside of a function: This retains the value between function calls

  2. Secondly, when it is declared for the function name: By default function is extern..therefore it will be visible out of different files if the function declaration is set as static..it will be invisible for outer files

  3. And lastly, static for global parameters: By default you can use global variables from outside files When it’s static global..this variable will be limited to within the file.

is this right ?

...