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++
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++
"\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.
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:
Part of the assembly code:Code:#include <iostream> int main() { std::cout << '\n'; std::cout << "\n"; }
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
Interesting how the original thread topic '\n' broke the forum (in displaying that thread). I wonder if the vBulletin developers are aware of this.
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.
i have the same issue