general c question

hi there
just a i think im getting somewhere in c im stuck when this compiles x comes out as 6 it should be 9 can someone please tell me what is going wrong
thanks

#include <stdio.h>

main()
{
    int x,y,z;

    x = 9;
    y = (x=6)/3;
    z = x * y + 1 + 2 - 3 * 4;
     printf("x = %d, y = %d, z = %d\n",  x,y,z);
     printf("answer is %d\n", 3 * 17);

 //   fflush(stdin);
  //  getchr();

y = (x=6)/3;
You tell x to be 6

y = (x=6)/3;

Change that line.

thanks for that

jonwhite:

    z = x * y + 1 + 2 - 3 * 4;

And exactly what are you expecting from this? Unless you know exactly what the order of operations will be, you should use parentheses to clearly define the order.

Consider.. moving strictly from left to right...
x*y = 27
+1 = 28
+2 =30
-3 = 27
*4 = 108

However, running the code and printing it, the result is 18, because C++ does not operate from left to right. Multiply has precidence over add, so it boils down to:

27 + 1 + 2 -12 = 18

Of course, proper order of operation is multiplication and division first, then addition and subtraction. As lar3ry says, better to use parentheses to remove any ambiguity.

im trying to learn c, my results are as follows,

x = 9
y = 5 (5.3 rounds it to 5)
z = 36

It doesn't "round" y.
y is declared as an int:
int x,y,z;
so it can only be an integer number.
if you want decimals, you need to use data type "float".

im just copying an example of a lesson

i thought intergers are whole numbers so if the result is say 5.4 it gets rounded down to 5

"Rounded off" and "rounded down" are two different things. 5.4 gets chopped off to 5. So 2/3 = 1.

Not 0?

i mean rounded down to 5.0 thats correct?

i dont understand 2/3 = 1 in my mind thats 0.6 what does this get rounded down to?

so not sure what that gets rounded down to ?

integer math: 2/3 = 0

jonwhite:
i mean rounded down to 5.0 thats correct?

i dont understand 2/3 = 1 in my mind thats 0.6 what does this get rounded down to?

so not sure what that gets rounded down to ?

I'm guessing that 2 divided 3 is not equal to 1 in your mind it is equal to two thirds.
And then you are assuming that in C that would be one
But it is really zero.

Rounding is not what is happening here.
There is no rounding going on, it is truncation.

Since these are integers the values are integers which have no decimal component
so there is no way to express anything but an integer.
Imagine covering up everything on your calculator to the right of the decimal
point and including the decimal point.
That is what an integer is.
And when calculating integer expressions, the truncation will happen along
every step in the case of multiple divides.

lar3ry:
C++ does not operate from left to right. Multiply has precidence over add

Which, as we learn in about the 4th grade, is exactly the same as normal math:

--- bill

thanks for helping me its is sinking in just lots to learn

Erp! I screwed up... I meant to type 3/2 = 1.

2/3 does indeed return a value of 0 when using integers.