By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,885 Members | 1,499 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,885 IT Pros & Developers. It's quick & easy.

Global Static Variables

P: n/a
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.
Nov 14 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a

"sworna vidhya" <sw************@yahoo.co.in> wrote in message
news:46**************************@posting.google.c om...
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
Nov 14 '05 #2

P: n/a
sworna vidhya wrote:
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.
Nov 14 '05 #3

P: n/a
Martin Ambuhl <ma*****@earthlink.net> wrote in message news:<c5************@ID-227552.news.uni-berlin.de>...
sworna vidhya wrote:

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."


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

<snip>
--
Nick Keighley
Nov 14 '05 #4

P: n/a
In <8a**************************@posting.google.com > ni***********@marconi.com (Nick Keighley) writes:
Martin Ambuhl <ma*****@earthlink.net> wrote in message news:<c5************@ID-227552.news.uni-berlin.de>...
sworna vidhya wrote:

> 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."


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


In which case, calling it "global" is an oxymoron.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #5

P: n/a
Da*****@cern.ch (Dan Pop) wrote in message news:<c5**********@sunnews.cern.ch>...
In <8a**************************@posting.google.com >
ni***********@marconi.com (Nick Keighley) writes:
Martin Ambuhl <ma*****@earthlink.net> wrote in message news:<c5************@ID-227552.news.uni-berlin.de>...
sworna vidhya wrote: > 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."


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


In which case, calling it "global" is an oxymoron.


I'm not arguing I just wasn't sure the OP would understand terms
like "external linkage"
--
Nick Keighley
Nov 14 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.