Thread: About newlines

  1. #1
    Registered User
    Join Date
    Jun 2018
    Posts
    2

    About newlines

    I am reading Allain's book and at some point he ends two similar lines, one with double quotes:

    cout << "Enter user name: " << "\n";

    and a similar line with single quotes:

    getline (cin, username, '\n';

    Why the difference?
    I enjoy learning C++

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    "\n" is a string containing one character.
    '\n' is a single character.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    1,631
    It is more efficient to say cout << '\n' than to say cout << "\n".
    The former is stored as a single number (probably the number 10) in the machine code itself, whereas the latter is stored as two numbers (probably 10 followed by 0) in memory somewhere and the machine code loads the address of that "string" to print it.

    Example:
    Code:
    #include <iostream>
    
    int main() {
        std::cout << '\n';
        std::cout << "\n";
    }
    Part of the assembly code:
    Code:
    .LC0:
        .string    "\n"     ### The string {'\n', '\0'} is stored in memory
    main:
        ### cout << '\n';
        movl    $10, %esi   ### The number 10 is stored in the machine code
        movl    $_ZSt4cout, %edi
        call    _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_c
    
        ### cout << "\n";
        movl    $.LC0, %esi  ### Loading the address of the string
        movl    $_ZSt4cout, %edi
        call    _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
    A little inaccuracy saves tons of explanation. - H.H. Munro

  4. #4
    Guest
    Guest
    Interesting how the original thread topic '\n' broke the forum (in displaying that thread). I wonder if the vBulletin developers are aware of this.

  5. #5
    Registered User
    Join Date
    Jun 2018
    Posts
    1
    I also want to learn c++. please tell me some good books to read. I took online help from <<spam link removed>> if I stuck in anything. But now I am thinking that I need to read some book.

  6. #6
    Registered User
    Join Date
    Jul 2018
    Posts
    4
    i have the same issue

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newlines
    By ntwrk_ovrflow in forum C Programming
    Replies: 4
    Last Post: 04-22-2009, 11:28 AM
  2. gah newlines!
    By keira in forum C Programming
    Replies: 8
    Last Post: 12-02-2007, 02:23 PM
  3. question about newlines
    By Silvercord in forum C++ Programming
    Replies: 5
    Last Post: 01-22-2003, 10:28 AM
  4. newlines in textBox
    By SuperNewbie in forum C# Programming
    Replies: 3
    Last Post: 07-27-2002, 01:47 AM
  5. newlines from read file :(
    By loobian in forum C++ Programming
    Replies: 12
    Last Post: 10-15-2001, 03:20 PM

Tags for this Thread