what is static variables?
I know global variables and local variables .. but static not sure..
Is it the same as local 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
Static variables will remain the same after a fuction had been exited and then reentered.
also check out This
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]
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
a better example
that will just show that statics are local variables, and the remain the same after a function ends.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--; }