Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
09-30-2004, 03:24 PM
|
#1
|
Member
Registered: Apr 2003
Posts: 225
Rep:
|
What is a static variable?
Basically what my subject says, what is a static variable?? And why would someone would want to use one? Thanks in advance.
|
|
|
09-30-2004, 03:28 PM
|
#2
|
Member
Registered: Oct 2002
Location: Ohio, USA
Distribution: OS X 10.4.8, Ubuntu 6.10
Posts: 146
Rep:
|
when something is static, it does not get changed. you use statics so that these variables are impossible to change because they are there to serve one purpose. when you are first writing programs, you may not use to many, if any, statics, but later on they come in handy when writing programs with a lot of functionality.
|
|
|
09-30-2004, 03:35 PM
|
#3
|
Member
Registered: Apr 2003
Posts: 225
Original Poster
Rep:
|
thanks a lot... they seem to be similar to constants... which as I think of it.. what is the diff between a static variable and a constant?
|
|
|
09-30-2004, 03:54 PM
|
#4
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
static variables (when inside a function) don't go away when the function exits. For instance, say you have this program:
Code:
void myfunc(void)
{
int a = 0;
printf("%d\n", a);
a++;
}
int main(void)
{
int i;
for(i = 0;i < 10;++i)
myfunc();
return 0;
}
...it will print 0 10 times. But if you change myfunc() so that a is static...
Code:
void myfunc(void)
{
static int a = 0;
printf("%d\n", a);
a++;
}
Now all of a sudden the program prints the numbers 0 through 9.
So it's not that the variable doesn't change, it's just that the variable doesn't get recreated every time you call the function.
|
|
|
09-30-2004, 04:04 PM
|
#5
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
Here, this will tell you all you need to know about static variables:
http://www-ee.eng.hawaii.edu/Courses...on2.1.1.6.html
(picked up from a simple "What is a static variable?" query on google.
|
|
|
09-30-2004, 04:41 PM
|
#6
|
Member
Registered: Sep 2004
Posts: 273
Rep:
|
Saying a static variable doesn't get (its value) changed is wrong, one can change the value of a static (non-const) variable just like any other non-const variable (and, yes, I know you can "cast away" constness). Having said that, it's common to use static together with const when declaring constants that you don't want several instances of (that would just waste memory). The thing that separates static variables from normal (automatic) variables is that they exist during the life time of their translation unit (read: file), instead of the current scope as the case is for automatic variables. For example, in C++, and static member variable could be used to keep track of the number of instances of a class. I use statics from time to time, often when dealing with callbacks, or when declaring global constant variables.
|
|
|
09-30-2004, 05:02 PM
|
#7
|
Member
Registered: Aug 2002
Location: tennessee
Distribution: gentoo
Posts: 39
Rep:
|
Another thing to keep in mind is when "static" is used in front of a global
variable (one not declared within a function), then the meaning is quite different.
In that case "static" indicates that the name (of the global) is local to the file.
|
|
|
09-30-2004, 05:17 PM
|
#8
|
Member
Registered: Aug 2002
Location: tennessee
Distribution: gentoo
Posts: 39
Rep:
|
I'm sorry, my mind wasn't connected to my mouth (fingers)...
My previous comment about "static" applies before a function,
not a variable.
|
|
|
All times are GMT -5. The time now is 09:53 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|