user1968030
user1968030

Reputation:

Why does the compiler not show an error when we try to divide a variable by zero

If we try to run this code:

int d = 10/0;

We get a compiler error. So we cannot divide by zero.

Now consider this code:

  int d = 10;
  int o = d / 0;

d can have anything and dividing anything by zero is not ok

We don't get a compiler error. The variable has anything we cannot divide by zero. Why does the compiler not give an error when we try to divide a variable by zero?

Upvotes: 5

Views: 1658

Answers (5)

Hans Passant
Hans Passant

Reputation: 941218

You'll want to take a look at the C# Language Specification, chapter 7.18. It talks about constant expressions. I'll just summarize the basics.

The C# compiler makes an effort to try to evaluate an expression at compile time. As long as the operands all have a known value and the operators are simple ones then the compiler can compute the value of the expression at compile time and use the result directly, instead of generating the code to evaluate the expression at runtime. This sounds like an optimization, but it is not quite like that, constant expressions are required in a number of places. Like the value of a case statement, declarations that use the const keyword, the values of an enum declaration and the arguments of an [attribute].

So no trouble with 10 / 0, that's an expression with literal values and a simple operator so the compiler can directly compute the result and see that it will trigger an exception so complains about it at compile time.

The d / 0 is not a constant expression because of the d variable. You could argue that the compiler could well know the value of d since it got assigned in the statement above it. But it doesn't do this, optimizing such code is the job of the jitter optimizer.

Upvotes: 9

No Idea For Name
No Idea For Name

Reputation: 11577

the c# compiler is only doing constants value arithmetic check, and there for can tell that you can't do 10/0. it's a lot more then you think for a compiler to do that.

even more then that: the c# compiler allows:

1.0 / 0 // Infinity

because:

Floating-point arithmetic overflow or division by zero never throws an exception, because floating-point types are based on IEEE 754 and so have provisions for representing infinity and NaN (Not a Number).

Upvotes: 0

Martin Schlott
Martin Schlott

Reputation: 4547

The first:

int d=10/0;

will be resolved due compile time, because it is constant. The second will not be resolved because the compiler do only syntax checks, not math check.

Upvotes: 1

Sinkingpoint
Sinkingpoint

Reputation: 7624

In the first instance, you have two constants, and so the compiler resolves this at compile time, thus finding the error.

In the second instance, as you have a non const variable(d), the compiler does not resolve this at compile time as it has no way of ensuring the value of d and thus will not spot the error. This will be evaluated at run time, where it will error out.

Upvotes: 1

MarcinJuraszek
MarcinJuraszek

Reputation: 125610

Because your d variable is not constant.

Compiler does only basic (let say trivial) math checks. Your is not basic enough to be done by compiler, because it uses variable that is not const.

Upvotes: 6

Related Questions