0

Ok so I am working through examples thought the Bjarne's book. And I understand that single characters are delimited by single quotes, such as 'x'.

Then there are double quotes which delimit strings, such as "Hello,!"

In this book there are moment when this makes sense and times when they don't make sense.

For example:

cout << "Hello, " << first <<" << second << '\n';   //doesn't /n have single quotes?

cout << "Hello, " << first_name << " (age " << age << ")\n"; // why does /n have double quotes around it?

If anyone can explain the syntax differences between the two examples, that would greatly be appreciated.

EDIT-----------------------

cout << "Hello, " << first <<" << second << '\n'; //doesn't /n have single quotes? Okay so /n is accounted for as a single character in the first example and as two in the second....... ) + /n My follow up question is with the first example. If the single quotes delimit the /n. Why is there only 3 sets of ""s? Shouldn't it be like this: <

  • 1
    a single char can be a string. \n is no special case. "a" and 'a' both are valid – Shankhoneer Chakrovarty Feb 04 '16 at 17:08
  • 2
    In the second example you need double quotes because you have a `)` in the quote too. single quotes are only for characters. So since `\n` is one character you can contain it in single quotes but `)\n` is two characters so you need double quotes which indicates a string – TheQAGuy Feb 04 '16 at 17:08
  • If you want to principled then you'd use std::endl – Hans Passant Feb 04 '16 at 17:11
  • See another [related question](http://stackoverflow.com/questions/4026887/what-is-the-difference-between-n-or-n-in-c). – Daniel Daranas Feb 04 '16 at 17:13
  • in '\n', the '\' is an [escape character](https://en.wikipedia.org/wiki/Escape_character). It means the next character has special significance. In this case `\n` means 'n' is not an 'n', it is an end of line. The compiler will always attempt to interpret the character after '\' as a special character. If you really want a '\', you have to escape the '\' and use '\\'. – user4581301 Feb 04 '16 at 17:41

3 Answers3

3

The compiler recognizes the sequence \n in a character (or string) literal as a single character, a newline. That's why it can be used in single quotes as a character literal.

In the second line, the newline is inside double quotes because it's not a single character literal, it's a string containing the characters ) and \n (plus the string terminator).

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
2

The difference is, that 'x' is a char and "x" is a c-string (const char*) of length 2 (the character x and the null). In your example, this makes no difference because the ostream operator<< can handle both, char and const char*, but for c-functions like strlen, strcmp and so on, this makes a huge difference if you're passing a single char or a string with only one char in it (and the nullterminator).

tkausl
  • 13,686
  • 2
  • 33
  • 50
  • 2
    It's also important to realize that `\n` is a single character, even though it's written out as two characters. – Mark Ransom Feb 04 '16 at 17:13
0

\n is considered one character, even though it is two. It is short for creating a new line. You can also use the endl; command. I think it is easier and looks nicer in the code.

It is in double quotes in this situation because it is including more than one character with the ). In a normal situation the \n could be put in singles '' or doubles "".

*Note you can only use endl; if you have declared you are using namespace std; Otherwise it is std::endl;