Exactly what is a static int?

This is a discussion on Exactly what is a static int? within the C++ Programming forums, part of the General Programming Boards category; I tried to declare a variable with the name (not type) static, however when I did this the name turned ...

  1. #1
    Shadow12345
    Guest

    Exactly what is a static int?

    I tried to declare a variable with the name (not type) static, however when I did this the name turned blue indicating static is a type. Exactly what is it?

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    317
    When you declare a variable static the compiler will place it in a seperate memory location with other static variables. This is special because thhe variable will exist thriugh the entire program. Unlike normal variables which die once they go out of scope, the static variable will persist and keep its data in memory so it can be used once it returns in scope.

  3. #3
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    most people don't realize that two large pieces of coral, painted brown and attached to his skull with common wood screws can make a child look like a deer.
    As you found this out you shouldn't have to much trouble opening a book or reading a webpage. However, a static variable (rather than a static function) is one that if declared globally is local to that particular file (in which case the modern C++ solution would be to create an unnamed namespace), or if it's declared locally within a function, will maintain it's value between calls (and therefore its presence in memory).

  4. #4
    Registered User Mario's Avatar
    Join Date
    May 2002
    Posts
    317
    A static variable is a variable that doesn't get deleted when it loses focus and, as such, retains it's value.

    static int myvar = 10;

    myvar will exist for the lifetime of the application. It is different from a global variable in the sense that even though it is not deleted, it can only be accessed within its scope.

    So,

    int whatever(int a , int b)
    {
    static int myvar = 10;
    //... do something
    myvar += 30;
    return result;
    }

    when you leave this function you will not be able to access this variable anymore. But when you return to it, myvar will now be equal to 40 and the second time it will be 70, and 100... ad eternum.
    Regards,
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

  5. #5
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    That's what I call service.

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,670
    Try this and you'll see what he means:



    Code:
    
    void foo()
    {
      static int times = 1;  
      printf("This function has been called %i times \n", times); 
      times++; 
    }
    
    
    int main()
    {
    
     int i = 0;
    
     while( ++i <= 10 )
     foo();
    
    
    getch();
    
    return 0;
    }
    Last edited by Sebastiani; 05-25-2002 at 05:43 PM.



    ITSA
    Socket Library!

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: Exactly what is a static int?

    Originally posted by Shadow12345
    I tried to declare a variable with the name (not type) static, however when I did this the name turned blue indicating static is a type. Exactly what is it?
    As you've found out, and probably already new, you can't use c++ keywords as variable names. To get an idea of what keywords exist, try this google search.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Shadow12345
    Guest
    lol thanks everyone. so if you create a static variable in visual basic that gets printed to the form everytime you hit command1 it should say 1,2,3,4,5,6,7, and so on. makes sense

    Sorenso (aka mommy): I thank thee for your wisdomful input oh great and knowledgable one. I have heard this advice many many times, and no doubt I will hear many times again...

  9. #9
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    You missed the n off the end of my name. You wanna watch yourself I'm a purple with little green spot hat hacker (ask my new friend $null).
    Last edited by Sorensen; 05-25-2002 at 06:24 PM.

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