This question already has an answer here:
- fork() and output 8 answers
#include <iostream>
#include <unistd.h>
#include <stdlib.h>
int main() {
std::cout << 1;
fork();
exit(0);
}
The fork
is located after streaming into cout
, but this code prints 11.
Why? And why does the code only print 1 if std::endl
is added to cout
?
#include <iostream>
#include <unistd.h>
#include <stdlib.h>
int main() {
std::cout << 1 << std::endl;
fork();
exit(0);
}