extern and sizeof
1)
#include
int main()
{
extern int a;
a=20;
printf("%d",sizeof(a));
return 0;
#include
int main()
{
extern int a;
printf("%d",sizeof(a));
return 0;
}
In case 1 we will get linker error and in cae 2 we get 4 a o/p.please explain the behaviour.
#include
int main()
{
extern int a;
a=20;
printf("%d",sizeof(a));
return 0;
}
2) #include
int main()
{
extern int a;
printf("%d",sizeof(a));
return 0;
}
In case 1 we will get linker error and in cae 2 we get 4 a o/p.please explain the behaviour.
never used 'extern' before.
ReplyDeleteknow what sizeof(), but also did not used it.
When ever u initialize a variable inside a function, the local variable takes a preference and hence prints 2bytes. I don't get the 2nd part
ReplyDeleteIn first case there is a linker error because a is searched in the other object files ( since here is only one) but not found.Here we have declared a as extern int and then using it.
ReplyDeleteIn second case we just declared the variable as extern int and in sizeof operator the actual type is evaluated so it gives 4 bytes as answer.
@Shiva sizeof operator takes the declaration of operand so definition is never required but when you initialize a variable it looks for definition so linker error is produced...:)
ReplyDelete