Home Ask Login Register

Developers Planet

Your answer is one click away!

mkel23 February 2016

C - \r does not work in printf() with nanosleep()

I was trying to use the '\r' character to write over the previous line; however, when used in conjunction with nanosleep(), the program does not output until the end. Here is a code example:

int i;
struct timespec t = {1, 0};
for (i = 0; i < 10; ++i) {
    nanosleep(&t, NULL);
    printf('\ri: %d', i);

This will wait for 10 seconds, and then print out i: 9. I would like it to show the updates, not just the end. I'm not sure if the line is being flushed or something?

Any and all help is greatly appreciated!


recycler February 2016

The console is caching your line until the char \n (newline) appears. You can use "fflush" to explicitly write the content to screen. Maybe there is a little impact on performance.

Post Status

Asked in February 2016
Viewed 2,488 times
Voted 8
Answered 1 times


Leave an answer

Quote of the day: live life