ProgrammingHardTCS NQT · Infosys
What is printed?
What is printed?
#include <stdio.h>
int f(void) {
static int s = 5;
return s++;
}
int main(void) {
int a = f(), b = f(), c = f();
printf("%d %d %d", a, b, c);
}- A. 7 6 5
- B. 5 5 5
- C. 4 5 6
- D. 5 6 7
Correct answer: D. 5 6 7
The static variable is initialised once to 5, and each call returns the current value and then increments it. Separate statements fix the order of the three calls, so the values are 5, 6 and 7.
More C Programming questions
- What is printed? Assume char is 1 byte, int is 4 bytes with 4-byte alignment. c #include <stdio.h> struct S { char a[5]; int b; }; int…
- What is printed? c #include <stdio.h> enum { A, B = 5, C }; int main(void) { printf("%d %d", C, A + B); }
- What is printed? c #include <stdio.h> int main(void) { int i = 0, n = 0; while (i++, i < 5) n++; printf("%d", n); }
- What is printed? c #include <stdio.h> int main(void) { int i = 0, c = 0; do { i++; if (i % 2) continue; c++; } while (i < 6); printf("%d"…
- After sorting by squared distance from the origin, with ties broken by x ascending, what is printed? c #include <stdio.h> #include…
- How many integers x in 1..255 have exactly four set bits? Compute with this program. c #include <stdio.h> int main(void) { int c = 0; for…