Problem with order of evaluation

F

Felipe Ribeiro

Hello everyone.

I have a small program here that has caused me to have some doubts.
Here's the code:
===========================================
#include <stdio.h>

#define NUM_RATES ((int) (sizeof(value) / sizeof(value[0])))
#define INITIAL_BALANCE 100.00

int main(void)
{
int i, j, low_rate, num_years, year;
double value[5];

printf("Enter interest rate: ");
scanf("%d", &low_rate);
printf("Enter number of years: ");
scanf("%d", &num_years);

printf("\nYears");
for (i = 0; i < NUM_RATES; i++) {
printf("%10d%%", low_rate + i);
value = INITIAL_BALANCE;
}
printf("\n");

for (year = 1; year <= num_years; year++) {
printf("%3d ", year);
while (1); /* Stop here. */
for (i = 0; i < NUM_RATES; i++) {
for (j = 0; j < 12; j++)
value += (low_rate + i) / 100.0 * value;
printf("%11.2f", value);
}
printf("\n");
}

return 0;
}
===========================================
As you can see, I put an infinite loop in the code so that the program
would stop there. What has caused me doubts is the fact that the line
right above it never got executed. That is, I never received the
output of <<printf("%3d ", year);>> when I think I should have.

Why did this happen? Is there anything obscure in the code that caused
this or are there any rules for order of evaluation that I'm not aware
of?

If I do something as simple as:
=======================
int main(void)
{
int i;

for (i = 0; i < 8; i++) {
printf("i'm here!\n");
while (1);
}

return 0;
}
======================
No problem. printf() is called normally as it should.
What's the difference between the two examples?

Thanks in advance for any help!

Felipe
 
F

Felipe Ribeiro

Felipe Ribeiro said:



Firstly, let's just say that this problem has nothing to do with order
of evaluation in the usual sense of the term (cf Subject line).

I didn't actually know how to name the thread. :)
<snip>

(non-working code)


<snip>

(working code)




I see two main differences. The easiest to spot (and probably *not*
your problem) is that, in the second fragment, you are printing a
newline to stdout, which is line-buffered, so you're forcing the I/O
subsystem to flush the output to the standard output stream. In the
first fragment, you're not doing this. If you don't want a newline
there, you can get the same flushing effect with fflush(stdout).

In fact that's exactly my doubt. I didn't know why the first wasn't
being printed while the second was. So it's all about the new-line
character? I didn't know about it. Thank you very much!
The other difference is that, in the second fragment, you are
executing the loop a fixed number of times: 8. In the first, however,
you are comparing your loop counter against a value that you read in
at runtime. If that value is 0 (or less) on the first run through,
you'll never see any output from that loop.

I wasn't even considering this.
You see, it's just an exercise and I assumed that the loop would be
executed at least once. My real problem was the new-line character
thing that you mentioned above. Thanks once again! :)

<snip>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top