Reputation: 111
When I compile the program:
#include <stdio.h>
int main(void)
{
int x, y = 0;
x = 1 / y;
printf("x = %d\n", x);
return 0;
}
It gives "Floating point exception (core dumped)"
However, when I compile:
#include <stdio.h>
int main(void)
{
double x, y = 0;
x = 1 / y;
printf("x = %f\n", x);
return 0;
}
It prints "x = inf"
Why does it return x as an infinite value if you're using double but returns an error if you're using int?
Upvotes: 5
Views: 5913
Reputation: 8204
A floating point variable can actually store a value representing infinity. (It is the value INFINITY
defined in math.h.) But there is no integer representation of infinity, so the only thing to do is fail.
Upvotes: 3
Reputation: 35164
C standard defines that the behaviour of a division by zero on operands of arithmetic types is undefined (cf., for example, this online C standard draft):
6.5.5 Multiplicative operators
2 Each of the operands shall have arithmetic type. The operands of the % operator shall have integer type.
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.
And this rule explicitly includes floating point values, since the term arithmetic types means integral values and floating point values:
6.2.5 Types
18 Integer and floating types are collectively called arithmetic types.
Hence, both of your examples are actually undefined behaviour, and each compiler may specify on its own how to treat your statements. And thus it is standard-conform if a compiler treats the integral-division-by-zero as an exception, whereas the floating-point-division-by-zero gives the special value inf
. Note: the standard does not define that this has to be the case.
Upvotes: 4
Reputation: 263617
The C standard explicitly states that dividing by zero has undefined behavior for either integer or floating-point operands.
C11 6.5.5 paragraph 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.
Undefined behavior means that the standard says nothing about what happens. It could yield a meaningful or meaningless result, it could crash, or it could, as the standard joke goes, make demons fly out of your nose. (Of course the latter isn't going to happen, but it wouldn't violate the C standard if it did.)
Floating-point types, unlike integer types, often have special values that don't represent numbers. The IEEE floating-point standard specifies that division by zero can yield a result of Infinity, which is what your implementation is doing. There is no "Infinity" value for integers. (Note that C implementations may or may not conform to the IEEE floating-point standard.)
This question discusses the semantics of division by zero in IEEE floating-point.
Upvotes: 8