Page 1 of 2
external and static in c
Posted: Mon Jan 26, 2009 12:29 pm
by yemista
Does anyone know what the result is of declaring a variable or function static external? What about external static?
Re: external and static in c
Posted: Mon Jan 26, 2009 12:56 pm
by CodeCat
Does that even work? They are both linkage declarations so I figure they would conflict and the compiler won't compile the code.
Re: external and static in c
Posted: Mon Jan 26, 2009 1:00 pm
by yemista
Yes they both work, but I dont know the difference, but there is a difference. External means global and static means local, so together they should conflict but I think it has a special meaning.
Re: external and static in c
Posted: Mon Jan 26, 2009 10:51 pm
by yemista
hey, im asking the questions here. dont look to me for explanation. What would two opposite keywords make combined?
Re: external and static in c
Posted: Mon Jan 26, 2009 10:57 pm
by Hangin10
yemista wrote:hey, im asking the questions here. dont look to me for explanation. What would two opposite keywords make combined?
Berkus' post is a link...
Re: external and static in c
Posted: Mon Jan 26, 2009 11:00 pm
by Love4Boobies
Do you even know what those keywords mean?
Re: external and static in c
Posted: Tue Jan 27, 2009 12:35 am
by DeletedAccount
. If a function is declared as static it cannot be referenced externally , what's the point of your question ?
Regards
Shrek
Re: external and static in c
Posted: Tue Jan 27, 2009 12:49 am
by Hangin10
EDIT: Shrek explains it better below. This post was full of grammatical fail.
Re: external and static in c
Posted: Tue Jan 27, 2009 10:28 am
by DeletedAccount
Hi,
extern is used to denote symbols that are not present the current object file and its the duty of the linkage editor (linker ) , to find out the symbol from a different module .
static has two meanings
when used for a variable : The variable maitains its values between function calls , just to give a demonstration , type this program in and find out the output
Code: Select all
#include <stdio.h>
#include <stdlib.h>
void func();
int main()
{
func();
func();
func();
}
void func()
{
static int var = 0 ;
var++;
printf("%d \n " , var );
}
when used for a function :
when used for a function it means that , function has scope restricted to the given file . It provides some sort of isolation .
However all these are very basic and should not be asked at this level
.I now really understand Alboin's and Solar's sentiments. Please read a good C book , then your questions will make more sense .
Regards
Shrek
Re: external and static in c
Posted: Tue Jan 27, 2009 2:12 pm
by Creature
Doesn't she mean what they mean together? Something like
Which won't compile.
P.S.: I believe I've just advertised
foobar2000.
Re: external and static in c
Posted: Tue Jan 27, 2009 2:48 pm
by yemista
Yes I do mean together. And its he. If extern static int foobar wont compile, static extern int foobar will, and Im wondering what that means.
Re: external and static in c
Posted: Tue Jan 27, 2009 2:51 pm
by yemista
Ok it looks like it wont compile. I dont know wat I was thinking of, but as soon as I find that example ill put it up.
Re: external and static in c
Posted: Tue Jan 27, 2009 3:48 pm
by Craze Frog
Shrek wrote: . If a function is declared as static it cannot be referenced externally , what's the point of your question?
The point is, what the hell does gcc do when you declare the same symbol as both extern and static?
This code is absolute nonsense, yet it compiles cleanly with gcc 4.3.2. At the very least it's nonsense that "static int oups; extern int oups;" compiles while "static extern int oups;" does not.
#include <stdio.h>
static int oups;
extern int oups;
int main (int argc, char *argv[]) {
printf("%d, %d", argc, oups);
return 0;
}
Re: external and static in c
Posted: Tue Jan 27, 2009 5:51 pm
by yemista
well i guess it does work then
Re: external and static in c
Posted: Tue Jan 27, 2009 5:52 pm
by JamesM
Why doesn't my code compile?
I don't understands!
Kthx.