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:

Re: external and static in c

Post by yemista »

it might be the f
DeletedAccount
Member
Member
Posts: 566
Joined: Tue Jun 20, 2006 9:17 am

Re: external and static in c

Post by DeletedAccount »

hi,
do you really think he is serious :?:


If yes , I cant stop laughing :D :D .

Regards
Shrek
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: external and static in c

Post by Solar »

yemista wrote:well i guess it does work then
No it doesn't.

From "ISO/IEC 9899:1999 The C Programming Language":
6.7.1 Storage-class specifiers
Syntax
storage-class-specifier:
typedef
extern
static
auto
register

Constaints
At most, one storage-class specifier may be given in the declaration specifiers in a declaration.
Ergo, if some version of some compiler accepts such code, it is a compiler bug, behaviour is undefined, and expect it to be fixed sooner or later.
Every good solution is obvious once you've found it.
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: external and static in c

Post by AJ »

JamesM wrote:Why doesn't my code compile?

Code: Select all

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

Kthx.
It must be because you forgot to declare 'a' static. Also, the value of a is incorrect. Didn't you mean:

Code: Select all

a = 1.337
:D
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 »

And please, my firend, do not forget that float is not the same with int. To avoid any problems, try thinking about it this way:
  • float stands for floating-point
  • int stand for integer
That way you will surely remember... =D>

:lol:
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: external and static in c

Post by Solar »

JamesM wrote:Why doesn't my code compile?
To add my answer(s) to this rhetorical question:

1) It does compile, setting a to 3. ;-)

2) If you meant to point out the uselessness of the declaration, be informed that the ISO standard actually quotes an "extern const volatile int real_time_clock" in an example. (Might be modified by hardware, but not the program.) ;-)
Every good solution is obvious once you've found it.
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 »

Solar wrote:
JamesM wrote:Why doesn't my code compile?
To add my answer(s) to this rhetorical question:

1) It does compile, setting a to 3. ;-)

2) If you meant to point out the uselessness of the declaration, be informed that the ISO standard actually quotes an "extern const volatile int real_time_clock" in an example. (Might be modified by hardware, but not the program.) ;-)
Oh yeah? Well... you... look funny! :?
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
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 »

Shrek wrote:hi,
do you really think he is serious :?:


If yes , I cant stop laughing :D :D .

Regards
Shrek

no i dont. sarcasm cuts both ways
Craze Frog
Member
Member
Posts: 368
Joined: Sun Sep 23, 2007 4:52 am

Re: external and static in c

Post by Craze Frog »

berkus wrote:Just to throw a little bit of seriousness into the discussion:

Code: Select all

[berkus@aramaki]$ cat aaa.cpp
extern static int foobar;
static extern int boofar;
[berkus@aramaki]$ gcc aaa.cpp -c
aaa.cpp:2: error: conflicting specifiers in declaration of ‘foobar’
aaa.cpp:3: error: conflicting specifiers in declaration of ‘boofar’
[berkus@aramaki]$ gcc --version
gcc (GCC) 4.3.2

Code: Select all

bash-3.2$ cat bbb.cpp
static int foo;
extern int foo;
int main(void) {
    return foo;
}
bash-3.2$ gcc bbb.cpp
bash-3.2$ gcc --version
gcc (GCC) 4.3.2 20080827 (alpha-testing) 1
Copyright (C) 2008 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

bash-3.2$
Craze Frog
Member
Member
Posts: 368
Joined: Sun Sep 23, 2007 4:52 am

Re: external and static in c

Post by Craze Frog »

JamesM wrote:Why doesn't my code compile?

Code: Select all

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

Kthx.
Hi.
Your code compiles cleanly. Either you're using a broken compiler or you're just a dumbass. (I pray for the first. [-o< )
JohnnyTheDon
Member
Member
Posts: 524
Joined: Sun Nov 09, 2008 2:55 am
Location: Pennsylvania, USA

Re: external and static in c

Post by JohnnyTheDon »

The problem is located between your ears. :P
DeletedAccount
Member
Member
Posts: 566
Joined: Tue Jun 20, 2006 9:17 am

Re: external and static in c

Post by DeletedAccount »

@Craze Frog : Get yourself a new pair of specs . I said "function" not variable . Please be polite to other poster(s) , calling another person dump as is simply impolite . I cannot really comprehend this : What's the point of making a global variable static ? .

@Solar : I thought that was a useless declaration , thought of it as a joke . Thanks for your comments .

Regards
Shrek
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: external and static in c

Post by Solar »

Shrek wrote:What's the point of making a global variable static ?
So that it is global for the current translation unit only. I.e., all the functions in your .c file can use it as a global variable, but it won't show up in the linker symbols.

To recap "static":
  • static variable within function - holds value between function calls.
  • static variable in global scope - does not get external linkage.
  • static function in global scope - does not get external linkage. (Similar to anonymous namespace in C++, BTW.)
  • C++ only: static variable or function in class - one variable / function for all instances of the class.
As for extern
  • extern variable within function - syntax error.
  • extern variable (declaration only) in global scope - tells the compiler that a global, non-static variable exists in some other translation unit, and will be resolved at link time.
  • extern function in global scope (default) - function will receive external linkage.
I hope that covers it.
Every good solution is obvious once you've found it.
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 »

Craze Frog wrote:
JamesM wrote:Why doesn't my code compile?

Code: Select all

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

Kthx.
Hi.
Your code compiles cleanly. Either you're using a broken compiler or you're just a dumbass. (I pray for the first. [-o< )
If you're going to be an anal pedant about a clearly sarcastic comment, you should note that I have no main() definition, nor any scoping block of any sort.

Code: Select all

[13:01:30] ~/tmp $ cat tmp.c
const volatile int a = 3.1415f;
[13:01:35] ~/tmp $ gcc -o tmp tmp.c
/usr/lib/gcc/x86_64-linux-gnu/4.3.2/../../../../lib/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: ld returned 1 exit status
[13:01:36] ~/tmp $
Onoes.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: external and static in c

Post by Solar »

Try gcc -c, it works then. :twisted:
Every good solution is obvious once you've found it.
Post Reply