ProgrammingHardIBM · Deloitte pattern
What is printed?
What is printed?
int f(void) {
static int x = 10;
x += 5;
return x;
}
int main(void) {
int a = f();
int b = f();
int c = a + b;
printf("%d %d %d", a, b, c);
}- A. 15 15 30
- B. 15 20 35
- C. 10 15 25
- D. 20 20 40
Correct answer: B. 15 20 35
static int x = 10; initialises x only ONCE across the whole program; each call to f() adds 5 to the SAME persistent x. First call: x becomes 15, a=15. Second call: x becomes 20, b=20. c = 15+20 = 35.
More C Programming questions
- What happens when these two files are linked together? c / file1.c / extern int shared; void show(void) { printf("%d", shared); } /…
- What is printed? c void f(void) { static int calls = 0; int local = 0; calls++; local++; printf("%d %d ", calls, local); } int main(void)…
- What is printed? c int x = 5; int getX(void) { return x; } int main(void) { int x = 20; printf("%d %d", x, getX()); }
- What is printed? c static int privateCounter = 0; void bump(void) { privateCounter++; } int getCounter(void) { return privateCounter; }…
- What is printed? c int value = 100; void modify(int value) { value = 200; } int main(void) { modify(value); printf("%d", value); }
- What is printed? c int a = 1; int b = a++ + a++; int c = ++a + ++a; printf("UB UB");