Question
Option 4 : 3 / 3
Detailed Solution
सही उत्तर 3 / 3 है।
Question:
What is the output of the following C code? `int f() { static int n = 0; return ++n; } int main() { f(); f(); printf("%d", f()); }` / निम्नलिखित C कोड का आउटपुट क्या होगा? `int f() { static int n = 0; return ++n; } int main() { f(); f(); printf("%d", f()); }`
Correct Answer: 3 / 3
The `static` variable `n` persists across calls and is initialized only once, so the three calls return 1, 2, 3 and the printed value is 3. The trap is treating `n` as an automatic variable re-initialized to 0 on every call. / `static` variable `n` कॉलों के बीच बना रहता है और केवल एक बार initialize होता है, अतः तीनों कॉल 1, 2, 3 लौटाते हैं और प्रिंट हुआ मान 3 है। जाल `n` को हर कॉल पर 0 से पुनः initialize होने वाला automatic variable समझना है।