靜態變量在函數中的妙用


變量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