473,761 Members | 10,057 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Global Static Variables

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
5 2054

"sworna vidhya" <sw************ @yahoo.co.in> wrote in message
news:46******** *************** ***@posting.goo gle.com...
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
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
Martin Ambuhl <ma*****@earthl ink.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
In <8a************ **************@ posting.google. com> ni***********@m arconi.com (Nick Keighley) writes:
Martin Ambuhl <ma*****@earthl ink.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
Da*****@cern.ch (Dan Pop) wrote in message news:<c5******* ***@sunnews.cer n.ch>...
In <8a************ **************@ posting.google. com>
ni***********@m arconi.com (Nick Keighley) writes:
Martin Ambuhl <ma*****@earthl ink.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
2534
by: jose luis fernandez diaz | last post by:
Hi, I am reading Stroustrup's book 'C++ Programming Language'. In the 10.4.9 section (Nonlocal Store) he says: "A variable defined outside any function (that is global, namespace, and class static variables) is initializated (constructed) before main is invoked . . ." .. . .
2
8832
by: Thomas Matthews | last post by:
Hi, I'm getting linking errors when I declare a variable in the global scope, but not inside a function. The declarations are the same (only the names have been changed...). class Book { public: Book()
17
5630
by: MLH | last post by:
A97 Topic: If there is a way to preserve the values assigned to global variables when an untrapped runtime error occurs? I don't think there is, but I thought I'd ask. During development, I'm constantly running tests on imperfect code. On of the cumbersome jobs encountered is reassigning global vars their values after a close encounter with an untrapped runtime error. Rather than writing a procedure to simply reassign them all with a...
33
3049
by: MLH | last post by:
I've read some posts indicating that having tons of GV's in an Access app is a bad idea. Personally, I love GVs and I use them (possibly abuse them) all the time for everything imaginable - have been for years. If the machine has memory to spare and windows can use it - I'm thinking "Why not?" I was wondering what some of you have to say about that, particularly any severe "gotchas" you've had the unfortunate experience to contend with.
7
3144
by: Michael | last post by:
Hi newsgroup, as the subject indicates I am looking for an advice using global variables. I am not if this problem is more about style then C. If its wrong in thi group, sorry. So I have a couple of function that all need the same information (all located in the same file). By now it looks like /* file beginns */
8
4869
by: Vishwanathan Raman | last post by:
Hi I have a declared a static DataSet object SOBJ in Global.asax.I also have a localy defined DataSet LSOBJ in Global.asax which I am storing in Application State.Is there any technical differences in the way both the objects are handled by IIS. Are both objects stored in different memory spaces? I can access both the objects in my web page. I will be grateful if some one can help me understand the difference.
37
2747
by: eoindeb | last post by:
Sorry to ask another global variable question, but from reading other posts I'm still not sure whether to use them or not. I have a program with a set function that calls 4 other functions in order - let's say function A, B, C, D. It always calls function A first which is a function that returns a system path. Now all other functions require that variable as well (function A returns a char pointer)
9
8656
by: CDMAPoster | last post by:
About a year ago there was a thread about the use of global variables in A97: http://groups.google.com/group/comp.databases.ms-access/browse_frm/thread/fedc837a5aeb6157 Best Practices by Kang Su Gatlin, casual mention was made about using static variables as an alternative to using global variables. This caused me to think of the following: '-----Begin module code
5
11826
by: Sandman | last post by:
I dont think I understand them. I've read the section on scope in the manual inside out. I'm running PHP 5.2.0 Here is the code I'm working on: //include_me.php <?php $MYVAR = array(); global $MYVAR, $a; ?>
1
29378
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have called it polluting the global namespace. This article explores what happens when the global namespace becomes polluted and how to avoid this condition. The opinions expressed in this article are those of the author alone although many have...
0
9521
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9333
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10107
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9900
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9765
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8768
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7324
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
3
3442
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2733
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.