what does static int mean?

This is a discussion on what does static int mean? within the C++ Programming forums, part of the General Programming Boards category; what does static int mean in this situtation: int x =42; void g(int y) { static int x=7; x+=y; } ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    14

    what does static int mean?

    what does static int mean in this situtation:

    int x =42;

    void g(int y)
    {
    static int x=7;
    x+=y;
    }

    there is another function too and the main function. thanks

  2. #2
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    It means that the function g() has an int x that is persistent between calls, and in that particular situation, it will only be initialized to 7 once (the first time g() is called), and subsequently it will have whatever value was left in it from the previous call. It's sort of a like a global variable, except it is not 'seen' except inside that particular function.
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 03:24 PM
  3. Replies: 2
    Last Post: 03-24-2006, 07:36 PM
  4. Converted from Dev-C++ 4 to Dev-C++ 5
    By Wraithan in forum C++ Programming
    Replies: 8
    Last Post: 12-03-2005, 06:45 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21