"Pavan" <pa*******@gmail.com> wrote in message
i would like to know abt the data variable storage in C like
1.where does global variables gets stored and why is it so ..
2.like wise static ,local variables,
as for as i know i remember that localvariables/functionarguments gets
stored in stack is it true then how abt this
That's true enough. Most sytems have a stack, which is an area of memory
which grows when a function is called, and is reset when it returns. Local
variables usually go on the stack.
lets go with an explicit example since it can give me a exact answers
#include <string.h>
int global;
main()
{
int a ;
int b;
increment(a);
}
increment(int a)
{
a++;
}
now can any one get me clerared abt the memory/stack allocation of the
variables for the above simple code
The integer "global" goes in the global memory section, which will usually
be a special fixed-sized section of memory determined at compile time.
The variables a and b, in main, will probably go on the bottom of the stack.
When the function increment is called, the variable "a" in main is copied to
the parameter "a" which is local to increment. These days the parameter
would usually be stored in a register. On older compilers, it was common to
move the top of the stack by a few bytes, and place the variable on the
stack top.
The ++ operation affects the copy, not the variable named "a" local to main.
Since you don't do anything with the resulting values, it is a no-op.
The fact that both variables are called "a" shouldn't confuse you into
thinking that they are the same variable.