tags:

views:

449

answers:

6

Hi!

Does anyone know why if i put a printf just before a delay it waits until the delay is finished before it prints de message?

Code1 with sleep():

int main (void)
{
    printf ("hi world");
    system("sleep 3");    
}

Code2 with a self implemented delay:

void delay(float sec)
{
    time_t start;
    time_t current;
    time(&start);
    do{
        time(&current);
    }while(difftime(current,start) < sec);
}
int main (void)
{
    printf ("hi world");
    delay(3);    
}

And if:

printf ("hi world");
delay(3);    
printf ("hi world");
delay(3);

it waits until the sum of sleeps and then it prints the messages at the same time

Why does this happen?

UPDATE: I writed delay("sleep 3") when i called delay, i meant delay(3). Corrected

+5  A: 

the standard output is not flush until you output a '\n' char.

try printf ("hi world\n");

Ben
You are right ;).You bith are right (Ben )
Xidobix
I don't think simply adding a \n is a guarantee that the output will be flushed. I just happens to work for you, in this case. Better to use fflush() as FigBug suggests.
mch
+15  A: 

printf buffers it's output until a newline is output.

Add a fflush(stdout); to flush the buffers on demand.

FigBug
A: 

Because printf goes to stdout, which is buffered. The buffer is only flushed after it either fills up (not likely in your case) or when the program exits (after the sleep). Try fprintf to stderr instead. You can also change stdout to be line buffered with setlinebuf(stdout).

Sonja
The buffer will be flushed if you call `fflush`. If `stdout` is connected to a terminal, the buffer will also be flushed following a newline.
Sinan Ünür
You are right; I missed the fact that he had no newline on his printf and therefore assumed he wasn't connected to a terminal.
Sonja
+5  A: 

Normally, standard output is buffered until you either:

  • output a \n character
  • call fflush(stdout)

Do one of these things before calling delay() and you should see your output.

Greg Hewgill
Note that writing a newline typically only flushes the output if stdout is a terminal; if it's being redirected to a file, writing a newline will not cause it to flush.
Adam Rosenfield
A: 

When you call printf, you don't print anything until really necessary: until either the buffer fulls up, or you add a new line. Or you explicitly flush it.

So, you can either do

printf("Something\n");
delay();

or

printf("Something");
fflush(stdout);
delay();
Tordek
A: 

Technically that shouldn't even compile. In the delay("sleep 3") call you're trying to convert a const char * to a float. It should be:

void delay (float sec)
{
    // ...
}

delay(3);
Nick Bedford
It will compile, but it will treat a pointer as a float, which is probably going to do bad things. It won't compile if you crank up your compiler's warnings sufficiently.
Chris Lutz
@Chris yeah i figured it might even just convert the pointer to a float, but like you said, it ain't a good thing!
Nick Bedford
The compiler would probably at least complain about it anyway
Nick Bedford
i meant delay(3). Type error ;), i just updated the question. Thx for make me note it
Xidobix
why the negative vote?
Nick Bedford