ProgrammingModerateTCS NQT · Wipro

What is printed? Assume int is 32 bits and integer division truncates toward zero.

What is printed? Assume int is 32 bits and integer division truncates toward zero.
#include <stdio.h>
int main(void) {
    int v[] = {-7, 2};
    printf("%d", (v[0] + v[1]) / 2);
}
  1. A. -3
  2. B. -2
  3. C. -1
  4. D. -5

Correct answer: B. -2

The sum is -5, and dividing by 2 truncates toward zero to -2, not the floor -3. Floor division is a common assumption that gives the wrong answer here.

More C Programming questions

Exams that ask this topic