printf waits until main returns to display
I am using the most up-to-date eclipse IDE for C/C++. I have the below code. It seems simple enough. My problem is that no printf will display on the console until the main has finished (return 0). This means I get a prompt in console asking for input before I get the message telling me what to do. I fill in a number like 10 and I see 10 is now displayed in stdout followed by the first and second printf. Can anyone explain why this is? What am I doing wrong?:confused:
Code:
#include <stdio.h>
int main(void) {
int n = 0;
int i = 0;
int left = 1;
int right = 1;
int temp = 1;
printf("Enter an index: ");
scanf("%d", &n);
printf("Index: %d", n);
if (n < 0 || n == 0) {
printf("\nFibonacci index: %d\n", -1);
}
if (n == 1) {
printf("\nFibonacci index: %d\n", 1);
}
for (i = 0; i < (n - 1); i++) {
temp = right;
right = left + temp;
left = temp;
}
printf("\nFibonacci index: %d\n", left);
return 0;
}