简体   繁体   中英

printing updating variable in c

I need to make a counter in c and Im having kind of a noobish question. Here is my code :

 time_t start = time((time_t *) NULL);
  time_t finish = start;
  while((finish-start) < 5){
        finish=time((time_t *)NULL);
        printf("TIME : %d\n", (finish-start));
  }   
  printf("TIMER HAS STOPED !");

What i whant is to show the value of (finish-start) Like : TIMER : value, with the "value" beeing updated according to the variables, but i what "TIMER" to remain on screen and only the value to update. Like it is now it writes TIME: value about 1 million times on the screen. Thx in advance :X !

try to replace

printf("TIME : %d\\n", (finish-start));

by

printf("TIME : %d\\r", (finish-start));

Perhaps you want

printf("TIME : %d\r", (finish-start));
fflush (stdout);

But for more complex command & terminal oriented I/O, consider using ncurses

If you are on Windows, take a look at SetConconsoleCursorPosition .

If you are on systems that support ncurses , use it.

Or, more hackish, but if it works on your system, it is very easy, use \\b escape in printf:

printf("%2d", i);

delay(400);

printf("\\b\\b");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM