Thread: what is static variable....?

  1. #1
    Registered User
    Join Date
    Apr 2003

    what is static variable....?

    Can anybody tell me what is static variable in C Language ,its usage and how it retain or holds its value in it ? I ts mechanism etc etc...?
    Further i shall be so much thankfull i you write a small piece of program where in i can see the usage of Static variable ....
    Thanks in advance.

  2. #2
    Registered User
    Join Date
    Jul 2002
    is basicly a normal varible except it cant be changed. its used like a normal varible just with the word "static" in front of it.

    check out

  3. #3
    Registered User
    Join Date
    Apr 2002
    What do you mean its contents can't be changed? Of course a variable can change.

    It just means the variable is allocated at the beginning of the program and deallocated at the end. It is initialized to 0 if not specified otherwise.

    Here is a quick example.
    void Foo( void )
      static int x = 5;
    int main( void )
      Foo( );
      Foo( );
      return 0;
    whenever you enter the function the second time the value will be 6. It will NOT be created and destroyed within the scope of the function.

    There are other meanings of the static keyword in C++ but I think this is the one you were thinking of.

  4. #4
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    I think Mart got confused with the keyword "const". if you const something, it can not be changed. Static just means like Wizard said, it will keep its value no matter when its called, its scope is from program start to finish, not function start to finish like local variables. Good thing about static variables is that they can keep track of counting how many times something is done, or what functions are called and then printed out in the end of the program. (thats one good use, there are a lot more)

    --edit-- just adding an example of how to const something.

    const int age = 21;
    //now, if you tried to change this later with
    age = 22;
    //you will receive a compilier error because it has been cast as const and will always be 21.
    Last edited by stumon; 04-08-2003 at 10:53 PM.
    The keyboard is the standard device used to cause computer errors!

  5. #5
    " think Mart got confused with the keyword "const""

    yeah, i did

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scope of global variable vs. static
    By chiefmonkey in forum C++ Programming
    Replies: 4
    Last Post: 06-21-2009, 12:23 PM
  2. Static variable usage
    By shwetha_siddu in forum C Programming
    Replies: 1
    Last Post: 04-02-2009, 12:33 AM
  3. static variable vs. DLL
    By pheres in forum C++ Programming
    Replies: 11
    Last Post: 02-06-2008, 02:15 AM
  4. Replies: 3
    Last Post: 10-10-2002, 07:34 AM
  5. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 02:26 PM