local static in c 2b 2b

Solutions on MaxInterview for local static in c 2b 2b by the best coders in the world

showing results for - "local static in c 2b 2b"
Paul
27 Nov 2020
1#include<iostream>
2
3void Increment() {
4	int i = 0;//The life time of variable is limited to the function scope
5	i++;
6	std::cout << i << std::endl;
7};//This will increment i to one and when it will reach the end bracket the lifetime of var will get  destroyed
8void IncrementStaticVar() {
9	static int i = 0;//The life time of this var is = to program
10	i++;
11	std::cout << i << std::endl;
12}//This will var i increment i till the program ends and i will get destroyed when program ends
13int main() {
14	Increment();//output 1
15	Increment();//output 1
16	Increment();//output 1
17	IncrementStaticVar();// output 2
18	IncrementStaticVar();// output 3
19	IncrementStaticVar();// output 4
20	IncrementStaticVar();// output 5
21	std::cin.get();
22}
23