Thread: Static Variable

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    1

    Static Variable

    What is the diffrenece between static variable and global variable?

    Thanks in advance
    Urmil

  2. #2
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    A static variable doesn't get wipe out of memory when the function exits, but it can't be accessed everywhere in the program, unlike a global variable.

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    34
    The main difference is the scope. A static variable defined inside a function, can only be used inside that function. The value of that variable will be preserved when the function is finished. Global variables can be used throughout the program. Although global variables may have their place, generally speaking, they should be avoided. Here's an example:

    Code:
    int   global_int; /* global_int can be seen throughout the program */
    
    
    int main(void)
    {
       func1();
    
       ...
    }
    
    void func1()
    {
       static int local_int;  /* local_int can only be seen inside func1 */
    
       ...
    }
    clu82

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    A variable declared within a function is a local variable. This variable only exists and can only be used when the function is called.

    A global variable is a variable that can be used in any function in any file. A global variable is declared in one file and referred to by other files as extern.

    A static is declared in one file and can only be used in that file. It's global to the functions in that file.

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. what is static variable....?
    By gardenair in forum C Programming
    Replies: 4
    Last Post: 04-09-2003, 08:54 AM
  5. Replies: 3
    Last Post: 10-10-2002, 07:34 AM