I read that there is a difference between declaring and defining a global variable. My understanding is that in the code below, "a" is declared outside the main (no memory is allocated when just declared) and defined only when value is really assigned to it. But in case of variable C no memory is allocated because there is no usage of it. In case of variable d, it is declaration and initialization. Or is it that the declaration means declaring a variable using extern keyword?
Also what will happen in case of variable d? Does compiler remove this assuming that it is not used anywhere?
int a; // declaration
int c; // declaration
int d=10;
int main (void)
{
a=50; // a is now allocated memory
printf"%d",a);
}
But when it comes to a local variable, do we have the concept only declared but memory is not allocated? I read somewhere that just saying int b; will allocate the memory in stack. Does any memory is allocated to variable b in below case if it is not used? My understanding is "No" because we are not really using that variable and compiler does the optimization and removes b in such case.
void func1(void)
{
int b;
printf("Hello");
}
I would like to know the exact difference between declaring with and without extern, defining with respect to both global scope and local scope.
Update Clarification for not being duplicate question: The exact question I was asking is with respect to local variable declaration, initialization (Memory allocation) and comparison with global variable initialization/declaration. In globals we use the word "tentative" for just declaring tentatively, but not allocating memory. Why the same concept is not seen in locals is what my confusion. What happens if local variable is declared and not used. Will memory is allocated or not in stack.
ints are declarations, not only definitions. I think you'd have to addexternto all of them to make them declarations. - Colonel Thirty Two