What is the value assigned to the variable in each of these cases? Explain curious results. Be careful!
What is the value assigned to the variable in each of these cases? Explain curious results. Be careful!
int x, y;a) x = 1/2;
b) y = 3.0/2.0;
double z, w, t;
c) z = 1/2;
d) w = 3/2;
e) t = 3.0/2.0;
Answer:
a) 0 is assigned.1/2 is computed using truncating integer divide which gives 0.
b) 1.5 is calculated and assigned. The fractional part is discarded; 1 is stored.
c) 0 is calculated because 1/2 is done with truncating integer divide, gives 0. The floating point value 0.0 is stored.
d) 1 is calculated, because truncating int divide, 1.0 is stored
e) 1.5 is calculated and stored.
Comments
Post a Comment