2

I am using a loop to delete characters from a string using printf(\b) 1 by 1 and sleep 0.1 seconds in between. But when I run the code it happens simultaneously.

I literally tried typing each printf because I thought the loop might be the cause but still, the output was the same

#include <stdio.h>
#include <unistd.h>

void coolIntro(){
int i;

printf("A Game by Hideo Kojima");

Sleep(800);

for(i=0;i<12;i++){

    printf("\b");

    Sleep(100);

        }

printf("my_name_here");
}

I want the letters to disappear with a 0.1 seconds time interval.

1 Answers1

6

printf output is buffered. Use fflush(stdout) to force it to output immediately. Also, \b moves the cursor left but doesn't erase the character under a cursor. Print "\b \b" to move it back, print a space, and then move it back again.

printf("\b \b");
fflush(stdout);
John Kugelman
  • 349,597
  • 67
  • 533
  • 578