0

I recently decided to satisfy a long time curiosity of mine. What happens when you divide by zero in C? I had always simply avoided this situation with logic, but was curious how a language like C, which cannot throw a catchable exception, handles this situation.

Here was my very simple test (compiled with GCC on Windows 10):

int main()
{
    double test1 = 1.0/0.0;
    printf( "%f", test1 );
    int test2 = 1/0;
    printf( "%d", test2 );
}

The operation done with the double types gave me a lovely little indication that the result was not a number: 1.#INF00. All's fine so far..

However, when performing a divide by zero with int types, the program, less than eloquently, "stopped working." I'm running this on Windows, so I was alerted with that lovely dialog.

I am curious about this behavior. Why is crashing the program the choosen solution for an integer division by zero? Is there really no other solution, say akin to the double way, to handle division by zero? Is this the same behavior on every compiler, or just GCC?

DeathByTensors
  • 901
  • 1
  • 10
  • 16
  • 1
    I would recommend you to this answer here http://stackoverflow.com/questions/3004095/division-by-zero-undefined-behavior-or-implementation-defined-in-c-and-or-c Unfortunately I'd say this was essentially a duplicate ... – davidhood2 Jun 04 '16 at 12:22
  • GCC OSX 10.7.5: warning `main.c:6: warning: division by zero` printout `inf0` CLANG OSX 10.7.5: `main.c:6:18: warning: division by zero is undefined [-Wdivision-by-zero] int test2 = 1/0;` printout `inf0` – user3078414 Jun 04 '16 at 12:28
  • 1
    I don't understand the downvotes. – Bathsheba Jun 04 '16 at 12:31
  • I don't either! I marked as duplicate, have mercy! – DeathByTensors Jun 04 '16 at 12:33
  • I didn't downvote, but I have sympathy for it. Did you at least look around a bit before asking here? – Jens Gustedt Jun 04 '16 at 12:34
  • @JensGustedt Yes, there were a lot of questions on the subject of division by zero, but I didn't see anything that talked about the reason for the behavior. I must've glanced over the one that this is now linked with, which does indeed address what I was looking for (i.e. C specification explanation) – DeathByTensors Jun 04 '16 at 12:38

3 Answers3

4

Dividing by zero invokes undefined behavior. Anything can happen when undefined behavior is invoked.

Quote from N1570 6.5.5 Multiplicative operators:

5 The result of the / operator is the quotient from the division of the first operand by the second; the result of the % operator is the remainder. In both operations, if the value of the second operand is zero, the behavior is undefined.

Quote from N1570 3. Terms, definitions, and symbols:

3.4.3

1 undefined behavior behavior, upon use of a nonportable or erroneous program construct or of erroneous data, for which this International Standard imposes no requirements

2 NOTE Possible undefined behavior ranges from ignoring the situation completely with unpredictable results, to behaving during translation or program execution in a documented manner characteristic of the environment (with or without the issuance of a diagnostic message), to terminating a translation or execution (with the issuance of a diagnostic message).

MikeCAT
  • 73,922
  • 11
  • 45
  • 70
2

It's undefined behavior.

C11 §6.5.5 Multiplicative operators

The result of the / operator is the quotient from the division of the first operand by the second; the result of the % operator is the remainder. In both operations, if the value of the second operand is zero, the behavior is undefined.

Community
  • 1
  • 1
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
1

The behaviour on integer division by zero is undefined in c. So the output can depend on anything, including the compiler. Essentially this is because there is no specific bit pattern to represent infinity for an integral type.

Floating point division by zero is defined: and should return the floating point's best representation of infinity.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483