Question

1.
2.
3.
4.
Answer (Detailed Solution Below)

Option 4 : 3 / 3

Advertisement

Detailed Solution

सही उत्तर 3 / 3 है।

Key Points
Helpful?
PrepSnapprepsnap.in • Part -A

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()); }`

1.
1 / 1
2.
2 / 2
3.
0 / 0
4.
3 / 3

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 समझना है।

Downloaded from PrepSnap (prepsnap.in)