Question 2
Back to Exercise
What is the output of the following C program?
#include <stdio.h>
int counter(void) {
static int c = 0;
int d = 0;
c++;
d++;
return c * 10 + d;
}
int main(void) {
printf("%d ", counter());
printf("%d ", counter());
printf("%d\n", counter());
return 0;
}
- A 11 11 11
- B 11 22 33
- C 10 20 30
- D 11 21 31 Correct
Correct Answer: Option D
c is static, so it keeps counting: 1, 2, 3. d is a new variable set to 0 on every call, so it is always 1. Results: 11, 21, 31.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!