I know the use of static variables in classes, but I am confused what the behavior of a static variable declared in a function is, and also in the main function.
int main()
{
static int numer;
}
also
void func()
{
static int variable;
}
Three answers:
John H
2013-03-16 11:37:51 UTC
In each case, the scope of the variable is the function in which it is declared. The fact that one of the functions is main() makes no difference. It's the same behavior as if they were not declared static, except because they are static, they exist both before and after the functions are called. They would exist in the data or bss segment, rather than on the stack, like an automatic variable.
mcarthur
2016-12-01 10:49:57 UTC
From a Java/C++ viewpoint, you're conversing approximately the place Dynamic programming comes into play with inheritance. making use of inheritance and digital applications, i will comprehend which you're possibly coping with a shape, yet no longer what variety of shape, circle, sq., despite the fact that, yet perhaps I even have made applications to do some undemanding products for each shape, including drawing it to the reveal screen. i could write code that asserts yourshape.draw() without understanding in improve what variety of shape i'm definitely working with because of the fact the inheritance will enable the suitable draw function to be pronounced as.