0

I have an issue with the command sleep in C.

I have this function which display a cow like cowsay in terminal

void vacheQuiMange(){


update();
affiche_vache("00", "__");
sleep(1);

gotoxy(3,22);
printf("*");
sleep(1);


gotoxy(3,22);
printf("U*");
sleep(1);

gotoxy(3,23);
printf(" *");
sleep(1);

gotoxy(3,24);
printf(")*");
sleep(1);

gotoxy(3,25);
printf("\\*");
sleep(1);


for(int i = 26; i < 34; i++){
    gotoxy(3,i);
    printf(" *");
    sleep(1);
}

gotoxy(8,0);}

and this two function

void update () { printf ( "\033[H\033[J" ) ;}
void gotoxy (int x, int y) { printf ("\033[%d;%dH",x,y ) ;}

The problem is that the terminal don't display any update when there is a sleep, but show the final result after few second.

Lacoudasse
  • 19
  • 6
  • 3
    Add `fflush(stdout);` after each `printf`. Needed because `stdout` to the terminal is line buffered by default. Which means it will not output until a newline is encountered or the stream is flushed. – kaylum May 01 '22 at 11:31
  • Unrelated: for more complicated *stuff* you may want to research [`ncurses`](https://invisible-island.net/ncurses/man/ncurses.3x.html) (see [the Wikipedia article](https://en.wikipedia.org/wiki/Ncurses)) – pmg May 01 '22 at 11:37
  • `sleep()` is a function not a command - C does not have "commands" as such. It is not part of the C language, but POSIX API call. – Clifford May 01 '22 at 12:10

0 Answers0