The following code gave error on gcc compiler
But when static is removed from int a then it runs with no errors..Can someone please clear the doubt??Code:int main() { extern int a; printf("%d",a); return 0; } static int a=5;
The following code gave error on gcc compiler
But when static is removed from int a then it runs with no errors..Can someone please clear the doubt??Code:int main() { extern int a; printf("%d",a); return 0; } static int a=5;
extern means that the variable will be defined elsewhere. static (at least, at file scope) limits visibility to subsequent code in this source file.
With the static keyword, the definition of variable a is not visible to main(), so does not get associated with the extern declaration.
Thank you but I think static variables have internal linkage that is available to same file and not to outside files..
No. He is not.You are mistaken.
A variable with a `static' storage specifier defined at global scope has internal linkage.
[Edit]
To be clear, the `extern' keyword isn't a storage specifier in that external linkage is the default for global variables, but it does change the visibility of the variable for the compiler as it serves as a declaration which in C, and C++, is necessary.
In other words, you are essentially telling the compiler two different things about the same variable.
The "solution", for lack of a better word, is to put the static variable above `main' so that it serves as both a declaration and a definition.
If for some reason that is not an option, there are alternatives, but I will not explain them until you give a very good reason.
[/Edit]
Soma
Last edited by phantomotap; 03-19-2013 at 06:15 AM.
I didnt mention 'extern' in my previous comment No problems but now I have another doubt the following code gave error as "non static declaration of 'a' follows static declaration"
Can someone please clear the confusion and what does this error mean??Code:static int a; a=7; int main() { printf("%d",a); return 0; }
See my post: in such a case as that, the `extern' line serves as a declaration to the compiler that an appropriate variable exists.Then why does removing static gives no error although it is defined after main()..??
And again, that `static' storage class doesn't work here because the compiler is expecting a variable with external linkage because that is exactly what you told it to expect.
Yes.But he is mistaken in interpreting that as meaning an extern declaration in the same file will correspond to that static definition (aka "available").
The problem though is that he doesn't understand what `extern' is doing.
Telling him that he is mistaken about the one part he apparently understands isn't going to be helpful in teaching him about `extern'.
[Edit]
This is going to get ugly.Can someone please clear the confusion and what does this error mean??
*shrug*
Actually, I'm not going to explain all the details. You would not understand them.
Here is the minimum: you can't normally have instructions at global scope.
Yes, assignment is an instruction. A definition that has an `=' as part of the expression isn't really an assignment.
Basically, the assignment line is the same, conceptually, as putting "putc('!')" at global scope. You can't do that.
You are getting that particular error because C has a heritage that allows implicit declaration of variables. I'm not going to get into that, but if you change the name of the variable, you'll kind of see the evidence.
[/Edit]
Soma
Last edited by phantomotap; 03-19-2013 at 06:26 AM.
The way I see it:
extern is a hint for the linker to search for an exported symbol in all the objects and libs.
static means that the symbol will not be exported at all ( linker will not know about it)
in the original code there is a conflict because a is not an exported symbol -> ERROR
my error messages
kurtCode:t.c:7:12: error: static declaration of ‘a’ follows non-static declaration t.c:3:12: note: previous declaration of ‘a’ was here
This is wrong. The linker never sees `extern'. The linker either sees a name with external linkage or it doesn't.extern is a hint for the linker to search for an exported symbol in all the objects and libs.
If you have multiple variables with external linkage under the same name you'll get multiple definition errors.
The `extern' is saying to the compiler that a variable having external linkage under the given name exists.
[Edit]
If the name that you told the compiler would be available isn't found by the linker you will also get a linker error.
[/Edit]
Soma
@phantomotap thank you for your answer but in my previous question when i run the code by removing static from the global variable it gave no errors why is that so??
Because C is weird. If you have
at the global scope, the first line is a so-called "tentative definition". This construct is an unholy hack in the C standard to maintain compatibility with pre-standard codebases. It means something like "treat this as a definition if you don't see a better candidate, other as a declaration". So when the compiler sees the second line, the equally unholy "implicit int" rule tells it to treat it like "int a = 7;", i.e. a definition with an initializer. Since this is better than the previous line, the tentative definition becomes a declaration.Code:int a; a = 7;
A definition with the static storage specifier, as you had before, can never be a tentative definition but will be a full definition, thus the second line is a redefinition and therefore in conflict.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
Thank you it was a really great explaination!!!!