when will be the memory allocated for a variable?when it declared or defined
About Variable declaration in C
Collapse
X
-
hi memory to a veriable allocated the time it is declered
i.e. int a[100] allocates 100 integer memory locations to this array
even if u r not using all.
thats why the concept of dynamic memory allocation came into picture to utilise the memory efficiently.Comment
-
What do you mean by variable declaration and variable definition?
Even if we declare a variable like
int a;
still it has some value (which we are saying normally as garbage).
So I think in terms of variable, definition and declaration do not matter.
Yes but in terms of functions, declaration means prototype and definition means the actual code of the function. Here only definition will occupy the space.
In case of structure also, unless you declare a variable of that structure it does not take any space in memory.Comment
-
Memory will be allocated only when a variable is defined.Originally posted by udhaywhen will be the memory allocated for a variable?when it declared or defined
When a variable is declared it just tells the attribute of the variable either int, float or char..... to the compiler and no memory will be allocated.Comment
-
The distinction between declaration and definition does matter: a declaration
just tells the compiler that a variable exists somewhere. It's up to the linker to
find it and adjust whatever it has to adjust. A definition tells the compiler "here
is the variable; do something with it, like allocating storage for it". A definition
whithout a preceeding declaration is a declaration (and a definition) by itself.
A 'tentative' definition is a declaration that *can* be a definition, as in 'int x;'
Multiple tentative definitions are considered declarations. The 'extern' keyword
isn't obsolete.
kind regards,
JosComment
Comment