ProgrammingHardWipro · Cognizant pattern

What happens?

What happens?
int divide(int a, int b) {
    return a / b;
}
int main(void) {
    printf("%d", divide(10, 0));
}
  1. A. It prints a value of infinity, like floating-point division
  2. B. Undefined behaviour: integer division by zero (unlike floating-point division by zero, which yields inf/nan) typically raises a hardware trap (e.g. SIGFPE on most platforms), crashing the program
  3. C. It always prints 10
  4. D. It always prints 0

Correct answer: B. Undefined behaviour: integer division by zero (unlike floating-point division by zero, which yields inf/nan) typically raises a hardware trap (e.g. SIGFPE on most platforms), crashing the program

Integer division by zero is undefined behaviour in C and, unlike IEEE floating-point division (which defines inf/nan results), there is no well-defined numeric outcome; on most real hardware (x86, ARM) this traps as a hardware exception, typically terminating the program (e.g. with SIGFPE on Unix-like systems), rather than silently producing any particular number.

More C Programming questions

Exams that ask this topic