i want print seconds elapsed in real time since program began. first output "0". after 1 second, "0" replaced "1", , on. here code wrote initially.
#include<stdio.h> #include<time.h> void main () { long int time; printf("hello, let measure time!\n"); time=clock(); printf("%ld", 0); while(time/clocks_per_sec<7) { time=clock(); if(time%clocks_per_sec==0) { printf("\r"); printf("%ld", time/clocks_per_sec); } } }
this gave output "7" @ end of 7 seconds only.
if make following replacements, code works fine.
printf("%ld", 0);
by
printf("%ld\n", 0);
and
printf("\r"); printf("%ld", time/clocks_per_sec);
by
printf("\33[a"); //vt100 char, moves cursor printf("\33[2k"); //vt100 char, erases current line printf("%ld\n", time/clocks_per_sec);
the problem seems output wont sent until current line determined. happening here?
i using gcc compiler on ubuntu.
the output stream writing printf() buffer until newline received. since aren't sending newline no flush until application exits or buffer fills up.
you can flush output buffer after each printf(), hyde said in comment above using fflush(stdout).
or can disable buffering using setbuf( stdout, null );
Comments
Post a Comment