ProgrammingHardHCL · Tech Mahindra pattern
What happens when this runs?
What happens when this runs?
int divide(int a, int b) {
return a / b;
}
int main(void) {
printf("%d", divide(10, 0));
}- A. Undefined behaviour: integer division by zero
- B. Infinity, like floating-point division by zero
- C. Compile-time error: division by a literal 0 is caught by the compiler
- D. 0, since C treats x/0 as x
Correct answer: A. Undefined behaviour: integer division by zero
Unlike floating-point division (which has a well-defined IEEE-754 notion of infinity/NaN for x/0.0), integer division by zero has no such definition in C and is undefined behaviour — in practice it typically raises a hardware trap (SIGFPE on many systems), terminating the program, but this is implementation-specific, not guaranteed by the standard.
More C Programming questions
- What happens when this runs? c int p = (int )malloc(3 sizeof(int)); if (p == NULL) { return 1; } p[0] = 1; p[1] = 2; p[2] = 3; free(p)…
- What happens when this runs? c int arr[10]; for (int i = 0; i <= 10; i++) { arr[i] = i; } printf("done");
- What is printed? c #define CONCAT(a, b) a##b int main(void) { int xy = 100; printf("%d", CONCAT(x, y)); }
- What is printed? c #if defined(FEATUREX) && !defined(FEATUREY) #define MODE 1 #elif !defined(FEATUREX) #define MODE 2 #else #define MODE 3…
- What is printed? c #define INCREMENT(x) x = x + 1 int main(void) { int a = 5, b = 10; if (a > 0) INCREMENT(a); else INCREMENT(b)…
- What happens? c int getDangling(void) { int local = 42; return &local; } int main(void) { int p = getDangling(); printf("%d", p); }