ProgrammingVery hardProduct company OA · GATE CS
What is printed?
What is printed?
int inc(int x) { return x + 1; }
int dec(int x) { return x - 1; }
int (*pick(int c))(int) { return c ? inc : dec; }
int main(void) { printf("%d", pick(0)(pick(1)(pick(1)(10)))); }- A. 11
- B. 12
- C. 9
- D. Compile error: a function cannot return a function pointer without a typedef
Correct answer: A. 11
pick(c) returns inc for nonzero c and dec for 0, so the chain is dec(inc(inc(10))) = 11; the declarator int (*pick(int c))(int) is valid without a typedef. 12 forgets the outer dec, and 9 swaps the ternary so that pick(0) gives inc.
More C Programming questions
- What is printed? c #include <limits.h> printf("%u %d", UINTMAX + 1u, UINTMAX == -1);
- What is printed? c int ways(int a, int n, int t) { if (t == 0) return 1; if (n == 0 || t < 0) return 0; return ways(a + 1, n - 1, t…
- What value does this call to printf return? c printf("%5.1f|%-5d|", 3.14159, 42);
- What is printed? c const char w[] = {"red", "green", "blue", "cyan"}; const char p[] = {w + 2, w, w + 3, w + 1}; const char q = p…
- What is printed? c int m(int n) { return n > 100 ? n - 10 : m(m(n + 11)); } int main(void) { printf("%d %d", m(50), m(105)); }
- What is printed? c int h(unsigned a, unsigned b) { int c = 0; for (unsigned x = a ^ b; x; x >>= 1) c += x & 1; return c; } int main(void)…