Home Subjects C Programming Functions

C Programming
Functions

C language from basics to advanced placement prep

24 Q 10 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 21–24 of 24
Topics in C Programming
Q.21 Hard Functions
What will be the output?
int counter() { static int count = 0; return ++count; }
int main() { printf("%d ", counter()); printf("%d ", counter()); printf("%d", counter()); }
A 0 0 0
B 1 2 3
C 1 1 1
D 3 3 3
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.

Test
Q.22 Hard Functions
What are static local variables in functions used for?
A To store values between function calls
B To make the function faster
C To reduce memory usage
D To prevent the function from being called
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.

Test
Q.23 Hard Functions
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); }
A 10
B Address of a
C Garbage value
D Error: Returning pointer to local variable
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.

Test
Q.24 Hard Functions
How can you modify a variable inside a function and reflect the change in the calling function?
A Return the value
B Use pointers
C Use global variables
D All of the above
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.

Test
IGET
IGET AI
Online · Exam prep assistant
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips