Showing posts with label linkage. Show all posts
Showing posts with label linkage. Show all posts

Sunday, December 6, 2009

void main()
{
static int i=5;
if(--i){
main();
printf("%d ",i);
}
}


Answer:
0 0 0 0
Explanation:
The variable "I" is declared as static, hence memory for I will be allocated
for only once, as it encounters the statement. The function main() will be called
recursively unless I becomes equal to 0, and since main() is recursively called, so
the value of static I ie., 0 will be printed every time the control is returned.

Thursday, December 3, 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.