extern and sizeof

1)
#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.

Comments

  1. never used 'extern' before.
    know what sizeof(), but also did not used it.

    ReplyDelete
  2. 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

    ReplyDelete
  3. In 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.

    In 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.

    ReplyDelete
  4. @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

Post a Comment

Popular posts from this blog