using printf and write together

328 Views Asked by At

I would like to understand why when I execute this code

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

int main()
{
    write(1, "6", 1);
    printf(" | ");
    write(1, "6", 1);
}

I get this output

66 | %

Instead of

6 | 6%

even with sleep before and after the printf it doesn't fix it. Thanks

1

There are 1 best solutions below

0
On BEST ANSWER

Try fflush(stdout); The file I/O is buffered to improve performance. Fflush writes the buffers out. Mixing the 2 may still give you surprises, so I would not recommend doing it.