0

Have a look at the following programme.

// Example program
#include <iostream>
#include <string>

int main()
{
  int n=7;  
  std::cout <<"n/2 = "<< n/2 << std::endl;
  std::cout <<"n/3.3 = "<< n/3.3 << std::endl;
}

output :

n/2 = 3
n/3.3 = 2.12121

In above example,

  • Expression "n/2" have been evaluated using integer division.
  • Expression "n/3.3" have been evaluated using real-number division.

What are the rules for determine which division is used?

Sneftel
  • 40,271
  • 12
  • 71
  • 104
  • Here are [C implicit conversions](https://en.cppreference.com/w/c/language/conversion) and here [C++ implicit conversions](https://en.cppreference.com/w/cpp/language/implicit_conversion) (C refrence explains it easier) – Robert Andrzejuk Sep 23 '18 at 17:49

1 Answers1

2

Arithmetic operations with two integer operands are evaluated in an integer context; arithmetic operations with at least one floating point operand are evaluated in a floating point context. (Beyond that there are more specific type conversion rules, but the basic idea is, if one of the operands is float or double it turns the other one into a float or double if it isn't already.)

Sneftel
  • 40,271
  • 12
  • 71
  • 104