ProgrammingVery hardProduct company OA · GATE CS
With a C99 (or later) compiler, what is printed?
With a C99 (or later) compiler, what is printed?
int ds(int n) { return n == 0 ? 0 : n % 10 + ds(n / 10); }
int main(void) { printf("%d", ds(-123) + ds(45)); }- A. 3
- B. 9
- C. Nothing: the recursion never ends
- D. 15
Correct answer: A. 3
C99 division truncates toward zero, so -123 / 10 = -12 and -123 % 10 = -3: the digits come out as -3, -2, -1, summing to -6, and n still reaches 0. Adding ds(45) = 9 gives 3. '15' assumes the digits are positive; endless recursion is what floor division (as in Python) would cause.
More C Programming questions
- What is printed? c int f(unsigned x) { int c = 0; while (x) { c += x & 1; x >>= 2; } return c; } int main(void) { printf("%d", f(0xFF) +…
- What is printed? c #define VAR(n) v##n #define PRINT(n) printf("%d ", VAR(n)) int v1 = 10, v2 = 20, v12 = 30; PRINT(1); PRINT(1 + 1)…
- Assume 32-bit unsigned int. What is printed? c unsigned int n = 5; int count = 0; for (unsigned int i = n; i < n + 3; i -= 2) count++…
- What is printed? c char w[][6] = {"red", "green", "blue"}; printf("%zu %zu %zu", sizeof(w), sizeof(w[1]), strlen(w[1]));
- What is printed? c int m = 0; long p(long b, int e) { if (e == 0) return 1; long h = p(b, e / 2); m++; if (e % 2) { m++; return h h b; }…
- What is printed? c enum Flag { RED = 1, GREEN = RED << 1, BLUE = GREEN << 1, ALL = RED | GREEN | BLUE }; printf("%d %d", ALL, ALL & ~GREEN);