I'm an armature programmer, and I wrote a code to calculate the time taken by a for statement to execute. The code is as follows:
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <unistd.h>
int main()
{
unsigned long long int i;
int a,b;
struct timeval st,end;
printf("\nEnter two numbers to multiply");
scanf("%d",&a);
scanf("%d",&b);
printf("\nOh, wait! I have to count upto 4294967295 before i multiply");
gettimeofday(&st,NULL);
for(i=1;i<4294967295;i++);
printf("count : %lld",i);
gettimeofday(&end,NULL);
printf("Time elapsed: %lu:%lu\n",end.tv_sec-st.tv_sec,end.tv_usec-st.tv_usec);
printf("\n");
return 0;
}
The for loop takes ~13 seconds to finish. However the printf statement is executed only after for loop completes execution. Why is this happeneing.
aandbsomewhere?