简体   繁体   中英

Why the command \r in printf() doesn't work?

I'm trying to update a text on the terminal without have to print again the text. Right now I'm trying to do it on a simple code:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(int argc, char *argv[]){
    for(int i=0;i<=100;++i){
        printf("\r[%3d%%]",i);
        sleep(1);
    }
    printf("\n");
    return 0;
}

The code literally print nothing, with the pointer blinking at the start of the line.
Can someone help me?

The standard output stream is typically line buffered, so if you don't print a newline (ie \n ) then the output will remain in the buffer.

After calling printf , call fflush(stdout); . This will flush the standard output stream so that you can see the text.

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