2

In the following code, why does the compiler evaluates result variable as 3(integer) but not as 3.5(float or double)?

void Main()
{
    var result = 7/2;
    Console.WriteLine(result);
}

Thanks

Pawan Mishra
  • 7,212
  • 5
  • 29
  • 39
  • 7
    You are dividing an int by an int. That always results in an int. The var evaluation just follows that. – BoltClock Dec 12 '14 at 06:51
  • because both 7 and 2 are int so compiler will try to return the result as int. just convert any one as double, you will get the result as double. – Deepak Sharma Dec 12 '14 at 06:51
  • For the same reason that `7/2` is `3` http://ideone.com/QrtMep – ta.speot.is Dec 12 '14 at 07:03
  • Why this question reopened? o.O Isn't [this question](http://stackoverflow.com/questions/10851273/why-integer-division-in-c-sharp-returns-an-integer-but-not-a-float) a duplicate? Am I voted it wrong? – Soner Gönül Dec 12 '14 at 07:16
  • I would expect someone that has been answering c# questions since 2010 to be able to research this question very easily, especially since its one of the first things most will probably learn. – Sayse Dec 12 '14 at 07:21
  • @Soner, I voted to re-open based on the fact it was using `var` which made it subtly different. _Very_ subtly because, now that I reflect more deeply, I think it _is_ actually a dupe, the `var` having little to do with the root problem. So, either as a reformed voter or hypocrite, I may have to vote to close :-) – paxdiablo Dec 12 '14 at 07:24
  • @paxdiablo Thanks for response. Yeah, you are quite right because `var` doesn't do _anything_ about how `7 / 2` expression evaluated and returned type or value. It might be better to close it again `:)` – Soner Gönül Dec 12 '14 at 07:38

4 Answers4

10

Because, if you divide one int by another, the result is an int. That's specified here.

When you divide two integers, the result is always an integer. For example, the result of 7 / 3 is 2.

Then var just uses the expression type to create an int because that's what you've told it you want (albeit implicitly). If you want a double, you need to force the type of the expression to a double:

var result = 7.0 / 2;

or, if you're using int variables where you can't just tack on a .0:

int seven = 7;
int two = 2;
var result = (double)seven / two;
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
6

Because you are doing Integer division.

You need to convert one of the value to float/double to get the expected result.

Try This:

void Main()
{
  var result = 7/2.0; //convert 7 or 2 to double
  Console.WriteLine(result);
}
Sudhakar Tillapudi
  • 25,935
  • 5
  • 37
  • 67
1

Operators like / are also like functions. Signature for int/int is

public static int operator /(int numerator, int denominator)
{
    //
}

So the return value is int.

serdar
  • 1,564
  • 1
  • 20
  • 30
0

As others have pointed out, you're getting 3 rather than 3.5 because you're performing integer division. What most answers have implied but not stated explicitly is why it's integer division. It's integer division because both operands are integers. They are both integers because 7 and 2 are integer literals. If you add a decimal point to either literal, it would become a double literal, resulting in double division being performed rather than integer division. Double division is used when dividing a double by an int or an int by a double.

adv12
  • 8,443
  • 2
  • 24
  • 48