Global Static Variables

S

sworna vidhya

Hai,

When viewing threads of comp.lang.c, I came across with 'static
const char * const resultFileName = "param.txt";' . Here in this
thread, 'static const char * const resultFileName = "param.txt";' is
declared globally.

a) What is the use of declaring a global variable static?
b) Why in this thread they had use "const" the two times?
c) When mentionting as const, the variable we declared remains
constant. Why there is need for using static?

Kindly clear my doubts.

Thanks and Regards,
M.Sworna Vidhya.
 
M

Mike Wahler

sworna vidhya said:
Hai,

When viewing threads of comp.lang.c, I came across with 'static
const char * const resultFileName = "param.txt";' . Here in this
thread, 'static const char * const resultFileName = "param.txt";' is
declared globally.

a) What is the use of declaring a global variable static?

It causes the object to have internal linkage.
b) Why in this thread they had use "const" the two times?

It indicates that both the pointer and what it points
to are const.

const char *p; /* 1) non-const pointer to const char */
char const *p; /* 2) same as 1) */
char * const p; /* 3) const pointer to non-const char */
const char * const p; /* 4) const pointer to const char */
char const * const p; /* 5) same as 4) */

c) When mentionting as const, the variable we declared remains
constant. Why there is need for using static?

'static' and 'const' are two separate concepts in C.

'const' prohibits modification of the object it qualifies.

When used at file scope, 'static' affects linkage.
When used at block scope, 'static' affects lifetime.

-Mike
 
M

Martin Ambuhl

sworna said:
Hai,

When viewing threads of comp.lang.c, I came across with 'static
const char * const resultFileName = "param.txt";' . Here in this
thread, 'static const char * const resultFileName = "param.txt";' is
declared globally.

a) What is the use of declaring a global variable static?

static prevents the variable from having external linkage.
In C, variables have scope and linkage, neither of which is called "global."
b) Why in this thread they had use "const" the two times?

Because two things are declared cont: the pointer and the thing pointed to.
c) When mentionting as const, the variable we declared remains
constant. Why there is need for using static?

Already answered above.
 
N

Nick Keighley

Martin Ambuhl said:
sworna vidhya wrote:

static prevents the variable from having external linkage.
In C, variables have scope and linkage, neither of which is called "global."

that is it is only accessible in the file it appears in.

<snip>
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top