Thread: Purpose of a static global variable???

  1. #1
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343

    Purpose of a static global variable???

    Is there any use of declaring a global variable as static? If it it then how???

  2. #2
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    sure there is, say you have a program that needs to use pi, or any number, very accuretly several times in your program...well no ones gonna change pi, and no one wants to type out

    3.141592653589793238462643383279502884197169399375 10582097494459....

    a bunch of times, so just do it once.
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    I don't think so. Perhaps when linking several modules together, but that's what extern is for...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by kermi3
    sure there is, say you have a program that needs to use pi, or any number, very accuretly several times in your program...well no ones gonna change pi, and no one wants to type out

    3.141592653589793238462643383279502884197169399375 10582097494459....

    a bunch of times, so just do it once.
    Aren't you thinking of const now...?
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    global, static means global but for that module only

    in a function, static means global but for that function only

    in a class, static means global but for that class only.


    so....

    in your example, static globals basically are not available outside that module and you can have the same name be reused in another module and represent another variable
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  6. #6
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    global static is deprecated.
    Code:
    static int a = 23;
    should be
    Code:
    namespace
    {
      int a = 23;
    }
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

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. [GLUT] Pointers to class methods
    By cboard_member in forum C++ Programming
    Replies: 13
    Last Post: 02-16-2006, 04:03 PM
  3. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  5. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 02:26 PM