Take the 2-minute tour ×

In linux g++ compiler, the third number is not printed, and "reached" is not printed. But I expected that "reached" will be printed, after that it would go into infinite loop. It executes as expected on windows using Codeblocks

#include <iostream>
int main()
    int a;
        std::cin >> a;
        std::cout << a;
    std::cout << "reached";
    while (1) {}
    return 0;
share|improve this question
What do you mean that the third number is not printed? cin>>a isn't printing any numbers –  David Robinson Jan 30 '13 at 18:32
I'm not sure why you put an infinite loop in there, but that's not a good idea. –  SShaheen Jan 30 '13 at 18:32
Suggest title change to "What isn't wrong with this piece of code?" –  Justin ᚅᚔᚈᚄᚒᚔ Jan 30 '13 at 18:34
I don't really understand the downvoting here, but aside from that please do not use using namespace std;! It might appear easier at first, but it totally undermines the concept of namespaces and will cause trouble if used in a header file other programmers might use later! Do not do this, even if many examples include this line. –  Simon Lehmann Jan 30 '13 at 18:37
First, I think that is not important IDE that you use, but is important the tool chain that you use in the compilation process. The for loop have just 3 steps and after that you putted an infinite loop. In used g++ compiler you can obtain "reached" printed after the terminated of for loop. –  user1929959 Jan 30 '13 at 18:52
show 3 more comments

closed as not a real question by Mooing Duck, 0x499602D2, BЈовић, Konrad Viltersten, hjpotter92 Jan 30 '13 at 23:36


3 Answers

up vote 4 down vote accepted

Because you are never ending the program, and thus never flushing your stdout (cout) output.

You can either change:



 cout<<"reached" << endl;


 cout<<"reached" << flush;

Or simply remove your forever loop.

Another alternative is to use cerr << "reached"; - that will be printed immediately, since cerr is not buffered.

share|improve this answer
Technically: it's "unit buffered". The implementation won't push each individual character to the system, but it will flush at the end of each << operator (or an unformatted write like std::ostream::put). –  James Kanze Jan 30 '13 at 19:10
add comment

It has to do with the buffering of cout. Write std::endl or std::flush to cout to flush the buffer before you enter the infinite loop.

share|improve this answer
add comment

"reached" is written into an internal buffer, but not written to the console. Usually, the buffer is written to the console every newline. If you don't want a newline, flush the buffer (i.e. write it visible to the console):


If you want a newline, use endl:

share|improve this answer
I was once told that the system will flush every now and then. Is that true? –  Caesar Jan 30 '13 at 18:48
@Ceasar: Some streams will flush when given a newline (cout will on most implementations, not all). Buffered streams will flush when they have "enough" characters stored up, but I have no idea how big that is on any implementation, though I'd expect it to be between 80 and 4096 bytes. Obviously, streams that don't flush (stringstream) won't flush ever. –  Mooing Duck Jan 30 '13 at 18:56
@Caesar All systems will flush when the output buffer is full. Very few will flush otherwise, unless you tell them too. One of the reasons we use std::endl is because it flushes (where as just outputting a '\n' doesn't). –  James Kanze Jan 30 '13 at 19:08
add comment
