vikum sri wijesinghe
vikum sri wijesinghe

Reputation: 143

how to determine data type of arithmetic expressions involving divisions in c++

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,

What are the rules for determine which division is used?

Upvotes: 0

Views: 122

Answers (1)

Sneftel
Sneftel

Reputation: 41503

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.)

Upvotes: 2

Related Questions