what is the value of y after the following statements?
-
what is the value of y after the following statements? float y; y = 17 / 5;
-
what is the value of y after the following statements? float y; y = 17 / 5;
Probably
3
, because you're dividing two integers and then storing the resulting integer in afloat
. But why don't you run it for yourself and find out?
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
what is the value of y after the following statements? float y; y = 17 / 5;
As Richard said, the expression only contains integers and so you should get the result of an integer division (3). However, if at least one of the operands / variables are of float type, all integers will be automatically converted to float type. Say, for example -
y = (float)17 / 5;
Having said this, the answer may be slightly different from the expected 3.4 because of how floating point numbers are represented. Please check this - Floating point inaccuracy examples - Stack Overflow[^]«_Superman_» _I love work. It gives me something to do between weekends.
_Microsoft MVP (Visual C++) (October 2009 - September 2013)