Reputation: 67
I'm not sure my understanding is right here
int i = 5;
float f = 3.9;
int result = i + f;
then when int and float are being added, 8.9 becomes 8? does this mean when adding smaller data type to bigger datatype gives the answer in smaller data type?
or is this pringting int because it's type casted because result is declared in int? if so how's this different from putting (int)in front of i + f?
Upvotes: 1
Views: 536
Reputation: 222362
In C, when two real1 arithmetic operands are added, they are converted to a common type:
long double
, the other is converted to long double
, and the result is long double
.double
, the other is converted to double
, and the result is double
.float
, the other is converted to float
, and the result is float
.So, in your i + f
, i
is converted to a float
, the values are added, and the result is a float
.
You then assign it to an int
. In assignment, the value is converted to the type of the destination. So the float
sum is converted to int
.
The rules for converting arithmetic operands are called “the usual arithmetic conversions” and are specified in C 2018 6.3.1.8. The rules for assignment are specified in C 6.5.16.1.
1 Complex numbers are handled similarly.
Upvotes: 2
Reputation: 61
In your specific case the result is 8 because the result of the addition is put into an int.
You can imagine the data types as boxes, each data type has a box size associated with it, while adding a float and an int their corresponding box sizes do not match, an int can be placed inside the box of a float. This is why logically the result of the addition is 8.9 . Unfortunately you want to put this result into a smaller box and this leads to the casting that you mention. Basically bigger boxes have a problem when put into smaller ones, you have to get rid of something.
Answering your questions in order:
Q1:when int and float are being added, 8.9 becomes 8?
A:depends in what you will store it, if you define your result as float you will get the 8.9 result otherwise in your case the part after the deciaml point is thrown away because it can't be stored inside an int "box".
Q2: does this mean when adding smaller data type to bigger datatype gives the answer in smaller data type?
A: not really, again depends in what you will store your result.
Q3:is this pringting int because it's type casted because result is declared in int?
A: yes this assumption is right.
Q4: if so how's this different from putting (int)in front of i + f?
A: while their result are the same for this specific scenario, the difference makes more sense in other scenarios. Imagine having instead of int result a float result, then the value in result would be 8.9 as expected, but if you are to use this casting (int)(i+f)
you would put the result of the addition into a smaller box thus losing data, later on you would put it back into a bigger box but you had already lost some bits and the value in result would be 8.0.
Generally you are safe to cast numeric data types from smaller to bigger ones, this way you won't lose any bits along the way. The other way around if you cast float to int or int to char and so on, you may obtain bad results.
Upvotes: 1