Govt. Exams
Entrance Exams
Advertisement
Topics in C Programming
What will be the output?
int counter() { static int count = 0; return ++count; }
int main() { printf("%d ", counter()); printf("%d ", counter()); printf("%d", counter()); }
int counter() { static int count = 0; return ++count; }
int main() { printf("%d ", counter()); printf("%d ", counter()); printf("%d", counter()); }
Correct Answer:
B. 1 2 3
EXPLANATION
Static variable 'count' retains its value. First call: 0+1=1, second: 1+1=2, third: 2+1=3.
What are static local variables in functions used for?
Correct Answer:
A. To store values between function calls
EXPLANATION
Static local variables retain their value between function calls, initialized only once, and are stored in data segment.
What is the output of this code?
int* getPointer(int *p) { return p; }
int main() { int a = 10; int *ptr = getPointer(&a); printf("%d", *ptr); }
int* getPointer(int *p) { return p; }
int main() { int a = 10; int *ptr = getPointer(&a); printf("%d", *ptr); }
Correct Answer:
A. 10
EXPLANATION
The function receives the address of 'a', returns it, and dereferencing gives 10. However, returning pointers to local variables is risky in other contexts.
How can you modify a variable inside a function and reflect the change in the calling function?
Correct Answer:
D. All of the above
EXPLANATION
All three methods work: return value, pass pointers, or use global variables. Each has different use cases and implications.