A static integer variable is declared inside a function. The variable increments by 1 when the function is called. The function is called and returns 3 times. What is the value of the variable on the third function call and what is the value when it returns?
int foo() {
static int x = 0;
x++;
return x;
}
The keyword static
acts to extend the lifetime of a variable to the lifetime of the program; e.g. initialization occurs once and once only and then the variable retains its value - whatever it has come to be - over all future calls to foo().
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments