Reputation: 395
int main(int argc, char* argv[])
{
while(1)
{
cout<<"123";
}
return 0;
}
I wrote this small program which would print "123" and then go in an infinite loop. But it does not print anything on the screen. What is the reason for this?
Upvotes: 4
Views: 1689
Reputation: 10007
There can be two reasons.
Firstly, the output is most probably buffered. That is, the text sent to cout
is not printed immediately, but kept in a buffer and printed only on flushing the buffer (which happens by cout.flush()
or by printing endl
).
Secondly, I suppose that an empty infinite loop is undefined behavior. That is, a program with an infinite loop can in fact do absolutely anything; in particular, an optimizer is allowed to optimize anything out of the program.
Upvotes: 8
Reputation: 234885
Most likely the process CPU burn (due to the tight loop) has blocked the streaming to the console.
Technically though the behaviour of your program is undefined as, essentially, the loop does not have any input /output or side effects.
A compiler is permitted to optimise out your function body, which would also yield no output.
Upvotes: 1