静态变量在函数中的妙用


变量a会从运行的程序上叠加,因此输出a++的值为9,10,11,12,13,14等
#include "stdafx.h"
#include
using namespace std;
int A(){
static int a=9; //去掉static  程序将会不一样
cout<< a++<<endl;
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
for (int i=0;i<10;i++){
A();
}
while(1);
return 0;
}
变量a会从运行的程序上不会叠加,因此输出a++的值全为9
#include "stdafx.h"
#include
using namespace std;
int A(){
 int a=9;
cout<< a++<<endl;
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
for (int i=0;i<10;i++){
A();
}
while(1);
return 0;
}
 
赋值代码自行验证
 
 
 
 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM