endl and flushing the buffer

前端 未结 5 644
被撕碎了的回忆
被撕碎了的回忆 2020-11-27 14:55

In the C++ primer book, in chapter (1), it mentions the following:

endl is a special value, called a manipulator, that when written t

5条回答
  •  鱼传尺愫
    2020-11-27 15:17

    When using std::cout, the operand used after the output operator ( << ) are stored in a buffer and are not displayed onto the stdin (usually terminal, or the command prompt) until it comes across std::endl or std::cin, which causes the buffer to be flushed, in the sense, display/output the contents of the buffer onto the stdin.

    Consider this program:

    #include 
    #include 
    
    int main(void)
    {
        std::cout << "Hello, world";
        sleep(2);
        std::cout << std::endl;
    
        return 0;
    }
    

    The output obtained will be:

    after 2 seconds

    Hello, World

提交回复
热议问题