I am working on a homework question and I have been able to do everything but this one question. I think I have an idea of how to go about it, I just want to make sure.
Assume the following declarations:
int a =1, b = 2, c = 3;
Fully parenthesize and evaluate each of the following expressions assuming they are evaluated in the order in which they are written.
So I have 2 expressions:
(double)a / b * c
a / (double)(b + c)
which I parenthesized to
((double)a) / (b * c)
(a) / ((double)(b + c))
I am just not sure on how to go about solving the double part of the question, does this just mean that it the evaluation of this will be printed in such a fashion of like 3.000 or 5.000 not just like a regular int such as 3 or 5?
I guess you just need to understand that any time there is an operation between an integral type and a floating point type, the integral type is promoted to the floating type so ...
Example 1:
(double)a/b * c
>>> 1.0/2*3 = (0.5) * 3 = 1.5
which is different than a non casted version that integer division would drop the remainder and you would end up with 0... 1/2*3 -> 0 * 3 -> 3
Example 2:
a/(double)(b+c)
>>> 1/ (double) (2+3) = 1/(5.0) = .20
so when you then assign these values to a float or a double, they will be those values, if they are assigned to an integral type they will be 1, and 0 respectively, because it drops the remainder...
as to what part the cast applies to (which I think is part of your question)... there is a very set order of operations in C, see (operator precedence in c)... you will see that a cast is higher precedence than division... otherwise...
(double)1/2 * 3 -> (double)(1/2) * 3
>>> 0.0 * 3 = 0.0