ProgrammingVery hardGATE CS · Product company OA
For ints a and b with b != 0 and no overflow, which statement is always true in C99 and later?
- A. a % b is never negative
- B. a / b rounds toward negative infinity
- C. (a / b) * b + a % b == a
- D. a % b always has the sign of b
Correct answer: C. (a / b) * b + a % b == a
C99 makes integer division truncate toward zero and defines % so that this identity holds. As a result a % b takes the sign of a (−7 % 2 is −1), not of b, and it can be negative.
More C Programming questions
- Why should a header file shared by several .c files not contain the line int counter = 0;?
- With 4-byte int, what is printed? c int n = 0; int f(void) { n++; return 1; } int main(void) { sizet s = sizeof(f()); printf("%zu %d", s…
- What is printed? c int n = 3, s = 0; while (n--) s += n; printf("%d %d", s, n);
- Assume ASCII. What is printed? c int cmp(const void a, const void b) { return strcmp((const char )a, (const char )b); } int main(void) {…
- What is printed? c int g[3][3] = {{1, 3, 1}, {1, 5, 1}, {4, 2, 1}}; int mp(int r, int c) { if (r == 0 && c == 0) return g[0][0]; if (r < 0…
- What is printed? c unsigned next(unsigned x) { unsigned c = x & -x, r = x + c; return (((r ^ x) >> 2) / c) | r; } int main(void) {…