Ok, I´ll tell you guys what I did to make it work. But first let me add that I began by following Timmmay´s suggestion. I substituted the (MAX_CHILDREN - children_served) calculation with a literal to narrow down where the error was occurring:
Code:
printf("The maximum number of children served is %d\n", MAX_CHILDREN);
printf("Apples served per child: %d\n", APPLES_PER_CHILD);
printf("Number of children unserved: %ld\n", (MAX_CHILDREN - children_served));
printf("Number of apples needed to serve unserved children: %ld\n", 199 * APPLES_PER_CHILD);
So the only thing I changed was the last line. Believe it or not, I was still getting the following results:
Code:
The maximum number of children served is 200
Apples served per child: 25
Number of children unserved: 199
Number of apples needed to serve unserved children: 619
So it appears that the problem is with the pre-processor constant multiplication 199 * APPLES_PER_CHILD.
What was the work-around I did? I created two new variables, and initialized them to the constants, as below:
Code:
long children_served;
long maximum_num_children = MAX_CHILDREN;
long apples_per_head = APPLES_PER_CHILD;
I then used those new variables in the logs:
Code:
printf("The maximum number of children served is %ld\n", maximum_num_children);
printf("Apples served per child: %ld\n", apples_per_head);
printf("Number of children unserved: %ld\n", (maximum_num_children - children_served));
printf("Number of apples needed to serve unserved children: %ld\n", (maximum_num_children - children_served) * apples_per_head);
The results came out correct this time:
Code:
The maximum number of children served is 200
Apples served per child: 25
Number of children unserved: 199
Number of apples needed to serve unserved children: 4975
Creating those two new variables and populating them with the constants would seem to me to be unnecessary, and kind of destroys the purpose of pre-processor statements in your code. But that´s the only solution I found to the problem. Something´s happening with that pre-processor statement that´s causing it to produce strange results. So I found a solution, but I would greatly appreciate being able to understand what went wrong.
Thanks everyone for the posts.