external and static in c

Programming, for all ages and all languages.
User avatar
yemista
Member
Member
Posts: 299
Joined: Fri Dec 26, 2008 12:31 pm
Location: Boston
Contact:

external and static in c

Post by yemista »

Does anyone know what the result is of declaring a variable or function static external? What about external static?
CodeCat
Member
Member
Posts: 158
Joined: Tue Sep 23, 2008 1:45 pm
Location: Eindhoven, Netherlands

Re: external and static in c

Post 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.
User avatar
yemista
Member
Member
Posts: 299
Joined: Fri Dec 26, 2008 12:31 pm
Location: Boston
Contact:

Re: external and static in c

Post 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.
User avatar
yemista
Member
Member
Posts: 299
Joined: Fri Dec 26, 2008 12:31 pm
Location: Boston
Contact:

Re: external and static in c

Post by yemista »

hey, im asking the questions here. dont look to me for explanation. What would two opposite keywords make combined?
Hangin10
Member
Member
Posts: 162
Joined: Wed Feb 27, 2008 12:40 am

Re: external and static in c

Post 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...
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: external and static in c

Post by Love4Boobies »

Do you even know what those keywords mean?
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
DeletedAccount
Member
Member
Posts: 566
Joined: Tue Jun 20, 2006 9:17 am

Re: external and static in c

Post by DeletedAccount »

#-o . If a function is declared as static it cannot be referenced externally , what's the point of your question ?

Regards
Shrek
Hangin10
Member
Member
Posts: 162
Joined: Wed Feb 27, 2008 12:40 am

Re: external and static in c

Post by Hangin10 »

EDIT: Shrek explains it better below. This post was full of grammatical fail.
Last edited by Hangin10 on Tue Jan 27, 2009 11:28 am, edited 1 time in total.
DeletedAccount
Member
Member
Posts: 566
Joined: Tue Jun 20, 2006 9:17 am

Re: external and static in c

Post 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 :evil: .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
User avatar
Creature
Member
Member
Posts: 548
Joined: Sat Dec 27, 2008 2:34 pm
Location: Belgium

Re: external and static in c

Post by Creature »

Doesn't she mean what they mean together? Something like

Code: Select all

extern static int foobar;
Which won't compile.

P.S.: I believe I've just advertised foobar2000.
When the chance of succeeding is 99%, there is still a 50% chance of that success happening.
User avatar
yemista
Member
Member
Posts: 299
Joined: Fri Dec 26, 2008 12:31 pm
Location: Boston
Contact:

Re: external and static in c

Post 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.
User avatar
yemista
Member
Member
Posts: 299
Joined: Fri Dec 26, 2008 12:31 pm
Location: Boston
Contact:

Re: external and static in c

Post 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.
Craze Frog
Member
Member
Posts: 368
Joined: Sun Sep 23, 2007 4:52 am

Re: external and static in c

Post by Craze Frog »

Shrek wrote:#-o . 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;
}
User avatar
yemista
Member
Member
Posts: 299
Joined: Fri Dec 26, 2008 12:31 pm
Location: Boston
Contact:

Re: external and static in c

Post by yemista »

well i guess it does work then
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: external and static in c

Post by JamesM »

Why doesn't my code compile?

Code: Select all

const volatile int a = 3.1415f;
I don't understands!

Kthx.
Post Reply