ProgrammingVery hardGATE CS
What is printed? Assume C99 or later (division truncates towards zero).
What is printed? Assume C99 or later (division truncates towards zero).
#include <stdio.h>
int main(void) {
printf("%d %d %d %d", -17 / 5, -17 % 5, 17 / -5, 17 % -5);
return 0;
}- A. -4 -2 -4 2
- B. -3 -2 -3 2
- C. -4 3 -4 -3
- D. -3 3 -3 -2
Correct answer: B. -3 -2 -3 2
Division truncates towards zero, and a % b has the sign of the dividend so that (a / b) * b + a % b == a. Hence -17 / 5 = -3 with remainder -2, and 17 / -5 = -3 with remainder 2.
More C Programming questions
- What is printed? Assume unsigned int is 32 bits and unsigned char is 8 bits. c #include <stdio.h> int main(void) { unsigned int x = 1u <<…
- What is printed? c #include <stdio.h> int main(void) { int i, j, k = 0; for (i = 0, j = 20; i < j; i += 3, j -= 2) k += j - i; printf("%d…
- What is printed? (sizet is an unsigned type.) c #include <stdio.h> #include <string.h> int main(void) { char s[] = "ab", t[] = "abcd"; if…
- What is printed? c #include <stdio.h> int f(int x) { int r = 0; switch (x) { case 1: r += 1; default: r += 10; case 2: r += 2; break; case…
- What is the exact output? c #include <stdio.h> void p(int n) { if (n <= 0) return; printf("%d", n); p(n - 1); printf("%d", n); p(n - 2); }…
- What is printed? Assume int is 4 bytes and the array is a 2-D array object of automatic storage. c #include <stdio.h> int main(void) { int…