static const?

G

Guest

Could someone tell me why somebody would declare a variable static
const?

The only reason I can see is an implementation dependency.
 
B

Ben Pfaff

No spam said:
Could someone tell me why somebody would declare a variable static
const?

Why not? A variable, especially an array or structure, whose
value will not change and of which only a single copy is needed
(or which is only needed within a single translation unit), can
reasonably be declared static const.
The only reason I can see is an implementation dependency.

I don't follow.
 
G

Guest

Well, let me rephrase.

What is the difference between const int foo and static const int foo
as far as the C standard is concerned?

As for implementation dependency, which I hope I dont get flamed for,
the addition of the "static" puts the variable in ram when just a
"const" would put the variable in read-only memory on a
compiler/linker used in embedded applications. (assuming default
linker and compiler options).
 
B

Ben Pfaff

No spam said:
Well, let me rephrase.

What is the difference between const int foo and static const int foo
as far as the C standard is concerned?

Within a function, adding "static" gives `foo' static lifetime,
i.e. its value persists across the lifetime of the program.
Outside a function, adding "static" gives `foo' internal linkage,
i.e. it is not accessible by name from other translation units.

In either case, you'd better give `foo' an initializer if you
want it to be useful.
As for implementation dependency, which I hope I dont get flamed for,
the addition of the "static" puts the variable in ram when just a
"const" would put the variable in read-only memory on a
compiler/linker used in embedded applications. (assuming default
linker and compiler options).

That's possible, but I don't see why a compiler would do that.
String literals are effectively[1] `static const', and many
implementations do put them into read-only storage.

[1] They are not `const'-qualified, but they are non-modifiable.
 
G

Guest

Thanks, I keep forgetting that static affects scope.
Ben Pfaff said:
No spam said:
Well, let me rephrase.

What is the difference between const int foo and static const int foo
as far as the C standard is concerned?

Within a function, adding "static" gives `foo' static lifetime,
i.e. its value persists across the lifetime of the program.
Outside a function, adding "static" gives `foo' internal linkage,
i.e. it is not accessible by name from other translation units.

In either case, you'd better give `foo' an initializer if you
want it to be useful.
As for implementation dependency, which I hope I dont get flamed for,
the addition of the "static" puts the variable in ram when just a
"const" would put the variable in read-only memory on a
compiler/linker used in embedded applications. (assuming default
linker and compiler options).

That's possible, but I don't see why a compiler would do that.
String literals are effectively[1] `static const', and many
implementations do put them into read-only storage.

[1] They are not `const'-qualified, but they are non-modifiable.
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa67f6aaa,0xaa9aa9f6,0x1f6},*
p=
b,x,i=24;for(;p+=!*p;*p/=4)switch(x=*p&3)case 0:{return 0;for(p--;i--;i--)case
2:{i++;if(1)break;else default:continue;if(0)case[/QUOTE]
1:putchar(a[i&15]);break;}}}
 
E

E. Robert Tisdale

No said:
Could someone tell me why somebody would declare a variable static const?

There is no such thing as a const variable. It's an oxymoron.
The only reason I can see is an implementation dependency.
> cat static.c
static const int i = 0;
> gcc -Wall -std=c99 -pedantic -c static.c
> nm static.o 00000000 r i
> nm -g static.o
> gcc --version
gcc (GCC) 3.2 20020903 (Red Hat Linux 8.0 3.2-7)
> vi static.c
> cat static.c
const int i = 0;
> gcc -Wall -std=c99 -pedantic -c static.c
> nm static.o 00000000 R i
> nm -g static.o
00000000 R i

You can use static to specify local scope for your constant.
 
C

CBFalconer

E. Robert Tisdale said:
There is no such thing as a const variable. It's an oxymoron.

As usual, you are showing your ignorance. In C, const means that
the variable is not to be written to.

*** General Warning *** anything written by ERT is probably wrong,
and should never be taken seriously.
 
P

Peter Shaggy Haywood

Groovy hepcat no spam was jivin' on Wed, 17 Dec 2003 10:26:33 -0500 in
comp.lang.c.
Re: static const?'s a cool scene! Dig it!
Thanks, I keep forgetting that static affects scope.

No, it does not. Scope is the range of an identifier within the
translation unit in which it appears, and depends on what the
identifier refers to and where it is declared.
What I think you meant to say was either that static affects the
linkage or that static affects the duration of an identifier. Neither
linkage nor duration have anithing to do with scope.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,125
Messages
2,570,748
Members
47,302
Latest member
MitziWragg

Latest Threads

Top