Note that there are some explanatory texts on larger screens.

plurals
  1. POHow the buffer of cout work?
    primarykey
    data
    text
    <p>I know that cout have buffer several days ago, and when I google it, it is said that the buffer is some like a stack and get the output of cout and printf from right to left, then put them out(to the console or file)from top to bottem. Like this,</p> <pre><code>a = 1; b = 2; c = 3; cout&lt;&lt;a&lt;&lt;b&lt;&lt;c&lt;&lt;endl; buffer:|3|2|1|&lt;- (take “&lt;-” as a poniter) output:|3|2|&lt;- (output 1) |3|&lt;- (output 2) |&lt;- (output 3) </code></pre> <p>Then I write a code below,</p> <pre><code>#include &lt;iostream&gt; using namespace std; int c = 6; int f() { c+=1; return c; } int main() { int i = 0; cout &lt;&lt;"i="&lt;&lt;i&lt;&lt;" i++="&lt;&lt;i++&lt;&lt;" i--="&lt;&lt;i--&lt;&lt;endl; i = 0; printf("i=%d i++=%d i--=%d\n" , i , i++ ,i-- ); cout&lt;&lt;f()&lt;&lt;" "&lt;&lt;f()&lt;&lt;" "&lt;&lt;f()&lt;&lt;endl; c = 6; printf("%d %d %d\n" , f() , f() ,f() ); system("pause"); return 0; } </code></pre> <p>Under VS2005, the output is</p> <pre><code>i=0 i++=-1 i--=0 i=0 i++=-1 i--=0 9 8 7 9 8 7 </code></pre> <p>It seems that the stack way is right~ However, I read C++ Primer Plus yesterday, and it is said that the cout work from left to right, every time return an object(cout), so "That’s the feature that lets you concatenate output by using insertion". But the from left to right way can not explain cout&lt; <p>Then <a href="https://stackoverflow.com/users/6782/alnitak">Alnitak</a> tell me that, "The &lt;&lt; operator is really ostream&amp; operator&lt;&lt;(ostream&amp; os, int), so another way of writing this is: operator&lt;&lt; ( operator&lt;&lt; ( operator&lt;&lt; ( cout, a ), b ), c )",</p> <p>If the rightest argument is first evaluated, it can be some explained.</p> <p>Now I'm confused about how cout's buffer work, can somebody help me?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload