uaecasher macrumors 65816 Original poster Jun 15, 2009 #1 Hello, i have written this code: Code: #include <stdio.h> int main (void) { int a = 15; int b = 35; printf("a / b * b = %i\n", a / b * b); } the result i get is zero, but it should be 15, why I'm getting a 0 thanks
Hello, i have written this code: Code: #include <stdio.h> int main (void) { int a = 15; int b = 35; printf("a / b * b = %i\n", a / b * b); } the result i get is zero, but it should be 15, why I'm getting a 0 thanks
4409723 Suspended Jun 15, 2009 #2 You need to think about the order this is being evaluated, see: http://www.difranco.net/cop2220/op-prec.htm a / b * b becomes (a / b) * b where: a = 15; b = 35; (15/35)*35 0*35 Remember these are integers so 15/35 will be 0. If you want to get the fractional part you'll need to use floats or doubles.
You need to think about the order this is being evaluated, see: http://www.difranco.net/cop2220/op-prec.htm a / b * b becomes (a / b) * b where: a = 15; b = 35; (15/35)*35 0*35 Remember these are integers so 15/35 will be 0. If you want to get the fractional part you'll need to use floats or doubles.
S Sander macrumors 6502a Jun 16, 2009 #4 EMT said: Use parenthesis in your expressions to make it safe.: Click to expand... That wouldn't help in this case - it's the integer division that's the problem here.
EMT said: Use parenthesis in your expressions to make it safe.: Click to expand... That wouldn't help in this case - it's the integer division that's the problem here.
gnasher729 Suspended Jun 16, 2009 #5 uaecasher said: Hello, i have written this code: Code: #include <stdio.h> int main (void) { int a = 15; int b = 35; printf("a / b * b = %i\n", a / b * b); } the result i get is zero, but it should be 15, why I'm getting a 0 thanks Click to expand... It should be zero. Buy a C book for beginners.
uaecasher said: Hello, i have written this code: Code: #include <stdio.h> int main (void) { int a = 15; int b = 35; printf("a / b * b = %i\n", a / b * b); } the result i get is zero, but it should be 15, why I'm getting a 0 thanks Click to expand... It should be zero. Buy a C book for beginners.