小编che*_*n92的帖子

Printf被for循环阻止

我是一个电枢程序员,我编写了一个代码来计算执行for语句所花费的时间.代码如下:

#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;
}
Run Code Online (Sandbox Code Playgroud)

for循环需要大约13秒才能完成.但是,只有在for循环完成执行后才执行printf语句.为什么会发生这种情况.

c printf for-loop

2
推荐指数
1
解决办法
150
查看次数

标签 统计

c ×1

for-loop ×1

printf ×1