2

I'm trying to make a simple progress bar like FreeBSD does in its booting screen , displaying / , | , \ , - recursively , but the following code got now output at all

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

int main ( int argc , char **argv )
{
    char arrows[4] = { '/' , '|' , '\\' , '-' };
    int i = 0;

    while (1)
    {
        printf ( "%c" , arrows[i] );

        if ( i > 3 ) 
            i = 0;
        else
            i ++;

        sleep (1);
        printf ( "\b" );
    }

    return 0;
}
daisy
  • 22,498
  • 29
  • 129
  • 265
  • 1
    possible duplicate of [Why does printf not flush after the call unless a newline is in the format string? (in C)](http://stackoverflow.com/questions/1716296/why-does-printf-not-flush-after-the-call-unless-a-newline-is-in-the-format-strin) – DarkDust Nov 14 '11 at 15:00

2 Answers2

3

You do not flush the output, so it will only be buffered and not flushed to the terminal until the buffer is full.

Add the following line after the first printf:

fflush(stdout);
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
2

Hard to tell right away, but keep in mind that arrays in size go from 0 to size - 1. Which means this:

if ( i > 4 ) 
    i = 0;
else
    i ++;

Is an off-by-one error. It should be either i > 3 or i >= 4.

As for your actual problem, it's probably because the stream isn't flushed.

Etienne de Martel
  • 34,692
  • 8
  • 91
  • 111