Home > Blockchain >  Why is \r not giving the same results on mac vscode as windows vscode
Why is \r not giving the same results on mac vscode as windows vscode

Time:07-05

The following code should print the number of seconds passing from 0-10 and works as intended on windows vscode, however on mac's vscode the output takes 10 seconds as it should but only outputs

10 second has passed

However, if I change the \r to \n , the code works but i don't want to print a new line every time.

0 second has passed

1 second has passsed

etc...

I've been searching for some time about this and i don't know what seems to be the issue...

The Code :

for(int i = 0;i<=10;i  )
{
    printf("\r %d second has passed",i);
    sleep(1);
}

CodePudding user response:

You are suffering from buffering. By disabling buffering on stdout, the text will be output immediately on calling printf. Here is the code:

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


int main() {
    setbuf(stdout, NULL);
    for(int i = 0;i<=10;i  )
    {
        printf("\r %d second has passed",i);
        sleep(1);
    }
    return 0;
}
  • Related