ProgrammingHardWipro · Cognizant pattern
What happens?
What happens?
int divide(int a, int b) {
return a / b;
}
int main(void) {
printf("%d", divide(10, 0));
}- A. It prints a value of infinity, like floating-point division
- 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
- C. It always prints 10
- 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
- What is printed? c #define DEBUG 0 int main(void) { #if DEBUG printf("debug moden"); #else printf("release moden"); #endif return 0; }
- Two files are compiled and linked together. What happens? c / file1.c / static int counter = 0; void inc(void) { counter++; } / file2.c /…
- What is printed? c int x = 10; void f(void) { int x = 20; { int x = 30; printf("%d ", x); } printf("%d ", x); } int main(void) { f()…
- What is printed? c void f(void) { register int i; int p = &i; p = 5; printf("%d", i); }
- What is printed? c int x = 100; void f(void) { extern int x; printf("%d", x); } int main(void) { f(); }
- What is printed? c void f(void) { printf("%d ", x); } int x = 42; int main(void) { f(); }