執行下列 C++ 程式碼後,螢幕印出的數字為何? int f(int m){ static int n=0; n++; return (m+n); } int main( ) { cout << f(f(f(3))) << endl; return 0; }
A7
B8
C9正確答案
D10
答案與詳解
由內而外:f(3) 時 n=1,回傳 4;f(4) 時 n=2,回傳 6;f(6) 時 n=3,回傳 6+3=9。static n 跨呼叫累積。
Examly 收錄 38 萬+ 道歷屆題目,每題都有像這樣的精選詳解。免費下載,立即開練。
