0

For what i know, the differences between this codes:

#include <iostream>
//CODE 1
int main(){
    std::cout << "Hi there" << std::endl;
    while(1);
    return 0;
}
#include <iostream>
//CODE 2
int main(){
    std::cout << "Hi there \n";
    while(1);
    return 0;
}

Is that, in the 'CODE 1', the endl introduces a newline, but also flushes the output buffer (cout), so, whenever the 'cout' statement gets executed, i will have 'Hi there' printed on the terminal

But, for what i know, in the 'CODE 2', when i have a newline '\n', i dont have a flush in the output buffer, and this output buffer flushing happens whenever i have a 'cin' statement or the program finish its execution, so, why both codes flushes the output buffer (i mean both codes print the messages "Hi there") if only the code 1 shoould do it

Is this a GNU/GCC thing? so if i change to a C++ standard, now, only code 1 should print the message right?

Thanks in advance!

14

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.