N = 4
x = N - 2 = 2
y = N + 3 = 7
a = N = 4
b = ( N + x ) / 2
= ( 4 + 2 ) / 2
= 6 / 2
= 3
So,
c = ( N + x + y ) / 3
= ( 4 + 2 + 7 ) / 3
= 13 / 3
= 4.3333........ (should be)
But Computer does not work like this.
float
variables has 32 bits and among those 8 is used for storing the decimical place of a numbers or a real number.
So, float
can store number with fractions.
But all of the bits in an integer
variable is used for storing the non fractional part of a number (except a bit that stores the sign of the number which is positive
or negative
).
Thus, integer
type variables cannot store fractional numbers.
So,
int / int = int // and,
float / float = float
and,
(float) 13 / (float) 4 = (float) 4.3333333... // and,
(int) 13 / (int) 4 = (int) 4 // integers cannot store fractional parts of a number.
// you can even see things like
(int) 3 / (int) 4 = (int) 0 // What?
Or, you can simply say that when you divide an integer
with an integer
, the fractional part is vanished.
To know how data inside integer
and float
variables is stored, see this.
However, if one of the arguments of the /
operator is float
, the other will be typecasted to float
and the result will be a float
type data.
(float) 13 / (int) 4 = (float) 4.3333333... // float 13 was typecasted.
(int) 3 / (float) 4 = (int) 0.3333333... // int 3 was typecasted.
check this out yourself. ideone link: TIlMp2 - Online C Compiler & Debugging Tool - Ideone.com
printf("%f\n", (float)(13 / 4));
printf("%f\n", (float)(13.0 / 4));
printf("%f\n", (float)(13 / 4.0));
printf("%f\n", (float)(13.0 / 4.0));