- using namespace std;
- int count = 3;
- int main()
- {
- int i, sum, count = 2;
- //输出main函数的count即为2
- //cout<<count<<endl;
- for(i = 0, sum = 0; i < count; i += 2,count++)
- {
- //输出main函数的count即为每次循环加1
- //cout<<count<<endl;
- //该语句只执行一次,即只开辟一次内存空间所以每次循环
- //改变count都不会被重置为4
- static int count = 4;
- //static中的count
- //cout<<count<<endl;
- //static中的count
- count++;
- //static中的count
- //cout<<count<<endl;
- if(i % 2 == 0)
- {
- //全局的count,即为main函数上面的count
- extern int count;
- //全局的count,即为main函数上面的count
- count++;
- //全局的count,即为main函数上面的count
- //cout<<count<<endl;
- //全局的count,即为main函数上面的count
- sum += count;
- }
- //static中的count
- //cout<<count<<endl;
- //static中的count
- sum += count;
- }
- //main函数中的count
- cout<<count<<' '<<sum<<endl;
- return 0;
- }
运行结果是4 20
具体的原因见代码注释,也可将注释掉的输出代码释放,查看运行的结果。