+ 1

Why is this program changing the value of variable B even if it is declared as static?

/* Why is this program changing the value of variable B even if it is declared as static?*/ #include <stdio.h> using namespace std; void fn() { int a=10; static int b=20; printf("a=%d b=%d",++a,++b); } int main() { fn(); fn(); return 0; }

5th Apr 2017, 4:02 AM
Rajesh Kumar (Rj)
Rajesh Kumar (Rj) - avatar
6 Answers
+ 3
i think you are confusing static with a constant
5th Apr 2017, 4:18 AM
Edward
+ 3
Static => initialize only once. constant​ => once declared, can't modify it's value I think u looking for is constant​.
5th Apr 2017, 4:22 AM
Eranga
Eranga - avatar
+ 3
@Rajesh use the keyword const instead of static
5th Apr 2017, 4:32 AM
Edward
+ 2
Use const instead.
5th Apr 2017, 4:30 AM
CalviŐ˛
CalviŐ˛ - avatar
+ 1
@Edward yes sir I am confused with static variable 😔
15th Apr 2017, 5:10 AM
Rajesh Kumar (Rj)
Rajesh Kumar (Rj) - avatar
+ 1
@Eranga thanks
15th Apr 2017, 5:10 AM
Rajesh Kumar (Rj)
Rajesh Kumar (Rj) - avatar