title: Why is '\n' preferred over "\n" for output streams?
body: <p>In <a href="https://stackoverflow.com/a/8311177/7151494">this</a> answer we can read that:</p>
  <blockquote>
  <p>I suppose there's little difference between using <code>'\n'</code> or using <code>&quot;\n&quot;</code>, but the latter is an array of (two) characters, which has to be printed character by character, for which a loop has to be set up, <strong>which is more complex than outputting a single character</strong>.</p>
  </blockquote>
  <p><sup><em>emphasis mine</em></sup></p>
  <p>That makes sense to me. I would think that outputting a <code>const char*</code> requires a loop which will test for null-terminator, which <em>must</em> introduce more operations than, let's say, a simple <code>putchar</code> (not implying that <code>std::cout</code> with <code>char</code> delegates to calling that - it's just a simplification to introduce an example).</p>
  <p>That convinced me to use</p>
  <pre><code>std::cout &lt;&lt; '\n';
  std::cout &lt;&lt; ' ';
  </code></pre>
  <p>rather than</p>
  <pre><code>std::cout &lt;&lt; &quot;\n&quot;;
  std::cout &lt;&lt; &quot; &quot;;
  </code></pre>
  <p>It's worth to mention here that I am aware of the performance difference being pretty much negligible. Nonetheless, some may argue that the former approach carries intent of actually passing a single character, rather than a string literal that just happened to be a one <code>char</code> long (<em>two</em> <code>char</code>s long if you count the <code>'\0'</code>).</p>
  <p>Lately I've done some little code reviews for someone who was using the latter approach. I made a small comment on the case and moved on. The developer then thanked me and said that he hadn't even thought of such difference (mainly focusing on the intent). It was not impactful at all (unsurprisingly), but the change was adopted.</p>
  <p>I then began wondering <em>how exactly</em> is that change significant, so I ran to godbolt. To my surprise, it showed the <a href="https://godbolt.org/z/p21Yxb" rel="nofollow noreferrer">following results</a> when tested on GCC (trunk) with <code>-std=c++17 -O3</code> flags. The generated assembly for the following code:</p>
  <pre><code>#include &lt;iostream&gt;
  
  void str() {
      std::cout &lt;&lt; &quot;\n&quot;;
  }
  
  void chr() {
      std::cout &lt;&lt; '\n';
  }
  
  int main() {
      str();
      chr();
  }
  </code></pre>
  <p>surprised me, because it appears that <code>chr()</code> is actually generating exactly twice as many instructions as <code>str()</code> does:</p>
  <pre><code>.LC0:
          .string &quot;\n&quot;
  str():
          mov     edx, 1
          mov     esi, OFFSET FLAT:.LC0
          mov     edi, OFFSET FLAT:_ZSt4cout
          jmp     std::basic_ostream&lt;char, std::char_traits&lt;char&gt; &gt;&amp; std::__ostream_insert&lt;char, std::char_traits&lt;char&gt; &gt;(std::basic_ostream&lt;char, std::char_traits&lt;char&gt; &gt;&amp;, char const*, long)
  chr():
          sub     rsp, 24
          mov     edx, 1
          mov     edi, OFFSET FLAT:_ZSt4cout
          lea     rsi, [rsp+15]
          mov     BYTE PTR [rsp+15], 10
          call    std::basic_ostream&lt;char, std::char_traits&lt;char&gt; &gt;&amp; std::__ostream_insert&lt;char, std::char_traits&lt;char&gt; &gt;(std::basic_ostream&lt;char, std::char_traits&lt;char&gt; &gt;&amp;, char const*, long)
          add     rsp, 24
          ret
  </code></pre>
  <p>Why is that? Why both of them eventually call the same <code>std::basic_ostream</code> function with <code>const char*</code> argument? Does it mean that the <code>char</code> literal approach is not only <em>not better</em>, but actually <em>worse</em> than string literal one?</p>