ProgrammingHardWipro · Cognizant pattern

What is printed?

What is printed?
#define SAFE_DIV(a, b) ((b) != 0 ? (a) / (b) : 0)
int main(void) {
    int x = 10, y = 0;
    printf("%d", SAFE_DIV(x, y));
}
  1. A. Undefined behaviour: division by zero still occurs
  2. B. 0
  3. C. 10
  4. D. Compile-time error: cannot divide by a variable that might be zero

Correct answer: B. 0

SAFE_DIV(x, y) expands to ((y) != 0 ? (x) / (y) : 0). The ternary operator only evaluates ONE branch: since y != 0 is false, the division (x)/(y) is never actually performed at all, so there's no division-by-zero UB here — the macro safely returns 0 instead.

More C Programming questions

Exams that ask this topic