0
#include <stdio.h>
#include <unistd.h>
int main(void)
{
    int i;
    for (i = 0; i < 2; i++) {
        fork();
        printf("-");
    }
return 0;
}

The result of this program is 8"-" : "--------". But if I change 'printf("-");' into 'printf("-\n");', the result of this program will become 6"-": "-\n-\n-\n-\n-\n-\n". Can anyone tell me why?

Jack
  • 1

1 Answers1

1

printf writes to stdout stream which is line buffered. Buffer is a block of memory which belongs to a stream and is used to hold stream data temporarily. This is done to increase efficiency, as file and console I/O is slow in comparison to memory operations. Line buffered means that characters are saved up in the buffer only till a newline is output.

ajay
  • 9,402
  • 8
  • 44
  • 71