static variables

This is a discussion on static variables within the C++ Programming forums, part of the General Programming Boards category; what is static variables? I know global variables and local variables .. but static not sure.. Is it the same ...

  1. #1
    Burning in Hell! Luigi's Avatar
    Join Date
    Nov 2002
    Posts
    117

    static variables

    what is static variables?

    I know global variables and local variables .. but static not sure..

    Is it the same as local variables?
    Last edited by Luigi; 04-24-2003 at 08:18 PM.
    Luigi


    // I use Xcode 1.1 && CodeWarrior 8.3
    // When on Mac Os X 10.3.2

    // I use Microsoft Visual C++ 6.0
    // When on windows XP

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    102
    Static variables will remain the same after a fuction had been exited and then reentered.

    also check out This

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    An example:
    Code:
    #include <iostream>
    
    void foo()
    {
      static int i;
      std::cout << i++ << std::endl;
    }
    
    int main()
    {
      for (int i = 0; i < 10; i++)
        foo();
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Burning in Hell! Luigi's Avatar
    Join Date
    Nov 2002
    Posts
    117
    Ok..
    Im reading bjarne stroustrup's book..

    and he asks:

    CH 6. EX 21.
    CONVERT THE DESK CALCULATOR TO USE A SYMBOL STRUCTURE INSTEAD OF USING THE
    STATIC VARIABLES number_value AND string_value.

    now what does he mean by symbol structure...
    especially since those var contains numbers and word like "pi"..

    its nothing like + = or anything like that how can I use symbol to contain/equal "25" or "mother"?

    Im kinda lost there..
    Luigi


    // I use Xcode 1.1 && CodeWarrior 8.3
    // When on Mac Os X 10.3.2

    // I use Microsoft Visual C++ 6.0
    // When on windows XP

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    102
    a better example

    Code:
    #include <stdio.h> /* I like C  :) */
    
    int main(void)
    {
       int i =0;
       for(;i < 10 ;i++)
       {
         printf("i in main() is %d", i);
         foo();
        }
    return 0;
    }
    
    void foo(void)
    {
       static i = 999;
       printf("static i is %d", i);
       i--;
    }
    that will just show that statics are local variables, and the remain the same after a function ends.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Static variables + Initialisation
    By kris.c in forum C Programming
    Replies: 2
    Last Post: 07-08-2007, 02:16 AM
  2. Static variables
    By ashughs in forum C++ Programming
    Replies: 3
    Last Post: 10-26-2006, 09:21 AM
  3. static variables
    By swgh in forum C++ Programming
    Replies: 2
    Last Post: 08-28-2006, 06:35 AM
  4. Visual C++ 6/.net Debug static variables
    By neandrake in forum C++ Programming
    Replies: 2
    Last Post: 04-27-2005, 03:45 AM
  5. Replies: 5
    Last Post: 11-19-2002, 06:49 PM

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