1. ## Simple math

I have this program:

Code:
```#include <stdio.h>

int main()
{
float x;

x = 1+2-3*4/5-6+7*8/9;
printf("%.2lf\n", 1+2-3*4/5-6+7*8/9);
printf("%.2lf\n", x);

system("PAUSE");
return 0;
}```
I know in reality the answer could be .82 rounding 2 decimal places.
however, the output of my current program is

0.00
1.00

I expected the outputs to be the same. Could someone guide me to understanding these outputs, and how I could manipulate the program more to get actual values?

I hope someone can help, Thank you!

2. You are using integer division, e.g., 4/5 == 0. Use floating point division by using floating point literals instead, e.g., 4.0f / 5.0f

3. On float numbers You have to put ".0" if is division between numbers

#include <stdio.h>

int main()
{
float x;

x = 1+2-3*4.0/5.0-6+7*8.0/9.0;
printf("%.2lf\n", 1+2-3*4.0/5.0-6+7*8.0/9.0);
printf("%.2lf\n", x);

system("PAUSE");
return 0;
}

4. Is 1+2-3*4/5-6+7*8/9 by itself assumed to be solved using only integers?

if x is assigned as a float, wouldn't the equation be solved using float numbers?

It is still a little confusing

5. Just remember this: C/C++ works on what it currently was!

EDIT: I mean that when dividing two integers, it produces an integer. The compiler doesn't read your mind!

6. Ah, I found a mistake in the codeing:

Code:
`printf("%.2lf\n", 1+2-3*4/5-6+7*8/9);`
produces an integer, and using %lf is the wrong syntax for an integer. I assume, when you display an integer as a float, it will always produce a 0. My outputs infact, were 1 and 1.00 after the edit

7. Here is another interesting problem:

Code:
```#include <stdio.h>

int main()
{
float x;
float y;

y = 5;
x = 1+2-3*4/5.0-6+7*8/9.0;

printf("%.2lf\n", 1+2-3*4/5-6+7*8/9);
printf("%.2lf\n", x);

printf("%.2lf\n", 5);
printf("%.2lf\n", y);

system("PAUSE");
return 0;
}```
The outputs are:

0.00
0.82
0.82
5.00

The third printf statement is an unexpected result. Why is it displaying 0.82?

8. So far my idea is that if there is a statement that has conflicting syntax, but is still compiliable like :

Code:
```printf("%.2lf\n", x);
printf("%.2lf\n", 1+2-3*4/5-6+7*8/9);```
it will merely display the last value that worked out.

Tell me if this is wrong, of if you agree

9. You're still printing two integer expressions using %.2lf.
Perhaps it's just the compiler protesting.

10. Maybe... it really is compiler-dependant on how printf will act if you pass it a wrong parameter.

11. I tried to program a test to see which variable gets assigned a float in the code:

Code:
`printf("%.2lf\n", (float)(A+B-C*D/E-F+G*H/I));`
however, I couldn't figure out how to test for this.
I know not all of them are assigned to floats since the output is 1.00 when using my previous values in the same equation.

My question now, is which variables is the float casted onto?

12. Originally Posted by Ludicrous
I tried to program a test to see which variable gets assigned a float in the code:

Code:
`printf("%.2lf\n", (float)(A+B-C*D/E-F+G*H/I));`
however, I couldn't figure out how to test for this.
I know not all of them are assigned to floats since the output is 1.00 when using my previous values in the same equation.

My question now, is which variables is the float casted onto?
In the example you gave the calculation is completed then the result is cast to a float value.

13. I'm going to make a guess here as to what the compiler is doing. When you push an int onto the stack there was still some more on the stack from the last time a double was pushed. The %lf format forces the recent int as well as some of the other data there to be used. So in effect, you are printing garbage left from when more of the stack was used by you. The fact that you pushed a 5 seems to have had little effect on the double... but it probably did of you were to print more decimal places.

This behaviour is compiler dependent. Also depends on the size differential of double vs. int.

14. Originally Posted by Ludicrous
I tried to program a test to see which variable gets assigned a float in the code:

Code:
`printf("%.2lf\n", (float)(A+B-C*D/E-F+G*H/I));`
however, I couldn't figure out how to test for this.
I know not all of them are assigned to floats since the output is 1.00 when using my previous values in the same equation.

My question now, is which variables is the float casted onto?
You don't yet understand. When you do the formula, the compiler will take each constant or variable for what it is. If two ints are added, multiplied, etc., the intermediate result will be int.

You cast the result of the expression to float. But by then its value was probably zero anyhow.
In your case none of the variables get promoted to float... only the RESULT, just before it's handed to the printf is.

15. Thank you everyone for answering my questions.

I don't fully understand the issue with printing an integer as a float (Not that I would intentionally do this), however I know what to look for with my current compiler, if I run across this type of error. I still have a lot of studying to do.

The order of operations is fairly clear, I have plenty of practice problems to test myself.

Thanks again