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
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