Friday, December 4, 2009

Extern keyword

1)
main()
{
extern int i;
i=20;
printf("%d",sizeof(i));
}
Answer:
Linker error: undefined symbol '_i'.
Explanation:
extern declaration specifies that the variable i is defined somewhere else.
The compiler passes the external variable to be resolved by the linker. So
compiler doesn't find an error. During linking the linker searches for the
definition of i. Since it is not found the linker flags an error.
2)
main()
{
printf("%d", out);
}
int out=100;
Answer:
Compiler error: undefined symbol out in function main.

Explanation:
The rule is that a variable is available for use from the point of declaration.
Even though a is a global variable, it is not available for main. Hence an
error.
3)
main()
{
extern out;
printf("%d", out);
}
int out=100;
Answer:
100
Explanation:
This is the correct way of writing the previous program.

0 comments:

Post a Comment