0

I entered the below code:

float myFloatValue1=5/2;

For this I got a message by hovering on 5/2 . It said " '5/2': integer division in floating-point context " and its result gave the value : 2.0 . I understand the resultant number is still being treated as an int. But this only occurs when we perform operation on a variable. When we enter 5/2 its just operation being performed on two literal values. Why is it defaulting to be int? And what is the meaning of the message which occurred, highlighted in italics?

Rhythm
  • 1
  • 1
  • Both `5` and `2` are `int` literals. if you do division on two `int` values, it is assumed you want an `int` result. – Joachim Sauer Sep 24 '21 at 13:48
  • 1
    `5/2` is integer division. The result is `2`, an `int`. That's then widened to a float, `2.0f`, to allow it to be assigned to a `float` variable. – Andy Turner Sep 24 '21 at 13:49

0 Answers0