C/Extern storage class
Expert: Zlatko - 10/25/2010
Question#include<stdio.h>
extern int count;
int main()
{
printf(" value = %d", count);
return 0;
}
I have not defined the variable 'count' but when I run the above code it outputs the value with 0. Is it correct ?
I think it should report error saying that count is not defined.
AnswerHello Ravi
The behavior as you describe it is not correct. I can think of three possibilities to explain it.
1) You are linking in another object file which has count defined.
or
2) You had count defined at one time, and then made it extern. Since then the linking has failed, and you are running an older executable.
or
3) One of the libraries you are linking in has count defined as a global variable. That is poor programming but mistakes happen.
Add another print statement to ensure that you are running the latest executable. Check your linker command for other object files or libraries being linked in. It is very unlikely that the system or compiler libraries have a global count defined.
Perhaps with some detective work, you can discover why it is happening.
Best regards
Zlatko