void foo() {
static int x;
}
void bar() {
static int x;
}
int main() {
foo();
bar();
}
Therefore it is possible.
Yes it is. Each of your b variables is private to the function in which they are declared. Show activity on this post. b in func and b in main are two different variables, they are not related, and their scope is inside each function that they are in.
Can two functions declare variables(non static) with the same name? Explanation: We can declare variables with the same name in two functions because their scope lies within the function.
Static local variables: variables declared as static inside a function are statically allocated while having the same scope as automatic local variables. Hence whatever values the function puts into its static local variables during one call will still be present when the function is called again.
They each see only their own one. A variable cannot be "seen" from outside the scope that it's declared in.
If, on the other hand, you did this:
static int x;
void foo() {
static int x;
}
int main() {
foo();
}
then foo()
only sees its local x
; the global x
has been "hidden" by it. But changes to one don't affect the value of the other.
The variables are distinct, each function has it's own scope. So although both variables last for the lifetime of the process, they do not interfere with each other.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With