external and static in c
-
- Member
- Posts: 566
- Joined: Tue Jun 20, 2006 9:17 am
Re: external and static in c
hi,
do you really think he is serious
If yes , I cant stop laughing .
Regards
Shrek
do you really think he is serious
If yes , I cant stop laughing .
Regards
Shrek
Re: external and static in c
No it doesn't.yemista wrote:well i guess it does work then
From "ISO/IEC 9899:1999 The C Programming Language":
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.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.
Every good solution is obvious once you've found it.
Re: external and static in c
It must be because you forgot to declare 'a' static. Also, the value of a is incorrect. Didn't you mean:JamesM wrote:Why doesn't my code compile?
I don't understands!Code: Select all
const volatile int a = 3.1415f;
Kthx.
Code: Select all
a = 1.337
- Love4Boobies
- Member
- Posts: 2111
- Joined: Fri Mar 07, 2008 5:36 pm
- Location: Bucharest, Romania
Re: external and static in c
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
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
[ Project UDI ]
Re: external and static in c
To add my answer(s) to this rhetorical question:JamesM wrote:Why doesn't my code compile?
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.
- Love4Boobies
- Member
- Posts: 2111
- Joined: Fri Mar 07, 2008 5:36 pm
- Location: Bucharest, Romania
Re: external and static in c
Oh yeah? Well... you... look funny!Solar wrote:To add my answer(s) to this rhetorical question:JamesM wrote:Why doesn't my code compile?
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.)
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
[ Project UDI ]
Re: external and static in c
Shrek wrote:hi,
do you really think he is serious
If yes , I cant stop laughing .
Regards
Shrek
no i dont. sarcasm cuts both ways
-
- Member
- Posts: 368
- Joined: Sun Sep 23, 2007 4:52 am
Re: external and static in c
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$
-
- Member
- Posts: 368
- Joined: Sun Sep 23, 2007 4:52 am
Re: external and static in c
Hi.JamesM wrote:Why doesn't my code compile?
I don't understands!Code: Select all
const volatile int a = 3.1415f;
Kthx.
Your code compiles cleanly. Either you're using a broken compiler or you're just a dumbass. (I pray for the first. )
-
- Member
- Posts: 524
- Joined: Sun Nov 09, 2008 2:55 am
- Location: Pennsylvania, USA
Re: external and static in c
The problem is located between your ears.
-
- Member
- Posts: 566
- Joined: Tue Jun 20, 2006 9:17 am
Re: external and static in c
@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
@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
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.Shrek wrote:What's the point of making a global variable static ?
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.
- 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.
Every good solution is obvious once you've found it.
Re: external and static in c
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.Craze Frog wrote:Hi.JamesM wrote:Why doesn't my code compile?
I don't understands!Code: Select all
const volatile int a = 3.1415f;
Kthx.
Your code compiles cleanly. Either you're using a broken compiler or you're just a dumbass. (I pray for the first. )
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 $
Re: external and static in c
Try gcc -c, it works then.
Every good solution is obvious once you've found it.