Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

uaecasher

macrumors 65816
Original poster
Jan 29, 2009
1,289
0
Stillwater, OK
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
 
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.
 
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

It should be zero. Buy a C book for beginners.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.