Page 2 of 2

Re: external and static in c

Posted: Tue Jan 27, 2009 8:53 pm
by yemista
it might be the f

Re: external and static in c

Posted: Wed Jan 28, 2009 12:09 am
by DeletedAccount
hi,
do you really think he is serious :?:


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

Regards
Shrek

Re: external and static in c

Posted: Wed Jan 28, 2009 1:39 am
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.

Re: external and static in c

Posted: Wed Jan 28, 2009 2:52 am
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

Re: external and static in c

Posted: Wed Jan 28, 2009 3:02 am
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:

Re: external and static in c

Posted: Wed Jan 28, 2009 3:07 am
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.) ;-)

Re: external and static in c

Posted: Wed Jan 28, 2009 3:40 am
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! :?

Re: external and static in c

Posted: Wed Jan 28, 2009 7:26 am
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

Re: external and static in c

Posted: Wed Jan 28, 2009 11:15 am
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$

Re: external and static in c

Posted: Wed Jan 28, 2009 11:18 am
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< )

Re: external and static in c

Posted: Wed Jan 28, 2009 11:41 am
by JohnnyTheDon
The problem is located between your ears. :P

Re: external and static in c

Posted: Thu Jan 29, 2009 6:26 am
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

Re: external and static in c

Posted: Thu Jan 29, 2009 6:44 am
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.

Re: external and static in c

Posted: Thu Jan 29, 2009 7:01 am
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.

Re: external and static in c

Posted: Thu Jan 29, 2009 7:16 am
by Solar
Try gcc -c, it works then. :twisted: