LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 09-30-2004, 04:24 PM   #1
saiz66
Member
 
Registered: Apr 2003
Posts: 225

Rep: Reputation: 30
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.
 
Old 09-30-2004, 04:28 PM   #2
AquamaN
Member
 
Registered: Oct 2002
Location: Ohio, USA
Distribution: OS X 10.4.8, Ubuntu 6.10
Posts: 146

Rep: Reputation: 15
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.
 
Old 09-30-2004, 04:35 PM   #3
saiz66
Member
 
Registered: Apr 2003
Posts: 225

Original Poster
Rep: Reputation: 30
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?
 
Old 09-30-2004, 04:54 PM   #4
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
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.
 
Old 09-30-2004, 05:04 PM   #5
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
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.
 
Old 09-30-2004, 05:41 PM   #6
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
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.
 
Old 09-30-2004, 06:02 PM   #7
vose
Member
 
Registered: Aug 2002
Location: tennessee
Distribution: gentoo
Posts: 39

Rep: Reputation: 15
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.
 
Old 09-30-2004, 06:17 PM   #8
vose
Member
 
Registered: Aug 2002
Location: tennessee
Distribution: gentoo
Posts: 39

Rep: Reputation: 15
I'm sorry, my mind wasn't connected to my mouth (fingers)...

My previous comment about "static" applies before a function,
not a variable.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Howto Assign Multiple Static Public IP Addresses under SBC's PPPoE Static Ip system o trekgraham Linux - Networking 8 04-17-2007 11:51 AM
extern static variable alaios Programming 9 09-11-2005 05:11 AM
Is there any static ARP for static IP network? linux_lover2005 Linux - Networking 2 05-18-2005 01:01 PM
FC2 Overriding static if in favor of dhcp system set for static pkraus109 Linux - Networking 8 09-21-2004 12:13 PM
How to get 'Window' variable from a 'Widget' variable bordel Programming 0 11-19-2003 04:19 AM


All times are GMT -5. The time now is 08:00 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration