I have written below code (Just for understanding purpose only). Outside the main, int b; means tentative definition. Inside main int b=16 is a local variable. Inside the function variable b has internal linkage (file scope). The output is 16 and 0. I read that memory is allocated for tentatively defined variables only when they are initialized. So here my question is inside function, printf prints the value of b and does the memory gets allocated when printf called? In case it is so, can I say that memory is allocated to a tentatively defined variables when they are first accessed (whether initialized or accessed)? Or am I wrong?
int b;
int b;
void f1(int a1);
int main ()
{
int b=16;
printf("b=%d\n",b);
f1(5);
return 0;
}
void f1(int a1)
{
printf("b = %d\n",b);
}
Edited: Adding more clarity: I am aware that variable inside the function is different from that in main. Also aware that local variable is in stack. My question here is with respect to tentative definition. int b was declared twice and it is accepted in C because it is tentative definition. In case it is actual definition, then multiple definitions are not allowed and hence this is not accepted. There are several questions on tentative definition (About Tentative definition). According to one of them "C has a special "tentative definition" rule which allows multiple definitions for the same variable so long as they all match and at most one has an initializer. The C compiler, behind the scenes, combines all of the tentative definitions into a single definition." So to rephrase my question:
Does memory gets allocated if variable is just tentatively defined, but not used? (assuming that compiler does not optimize the code). It looks like at the end of translation unit, memory gets allocated as per the question linked above. Or whether memory is allocated only when it is used for the fist time (in this case in printf statement)?
int b; int b;
allowed? :O – cadaniluktypedef int it; it b;
, had to lookup in the standard, but I'd avoid this like hell anyway. – too honest for this site