What is the value of X in the following code segments? Justify your answers. i) int a,b; float x; a=4; b=5; ii) int a,b; float x; a=4; b=5; x = (float) b/a;x=b/a;
3 years ago
C Programming
1) x = b/a;
x = 5/4; // x = int/int
x= 1.0 // since 5/4(int/int) results as 1 but x is of type float so 1 is converted to float i.e. 1.0
2) x = (float) b/a; x = (float) 5/4;
x = 5.0/4; // 5 is converted to 5.0(float type) because of cast operator
x = 5.0/4.0; // 4 is also converted to 4.0 because of implicit type conversion
x = 1.25 // hence, the result is 1.25
Sanisha Maharjan
Jan 20, 2022