what is static variables?
I know global variables and local variables .. but static not sure..
Is it the same as local variables?
Printable View
what is static variables?
I know global variables and local variables .. but static not sure..
Is it the same as local variables?
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();
}
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..
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--;
}